{"id":3486,"date":"2025-01-16T19:15:45","date_gmt":"2025-01-16T19:15:45","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/create-testng-test-using-playwright-java\/"},"modified":"2025-01-16T19:15:45","modified_gmt":"2025-01-16T19:15:45","slug":"create-testng-test-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/create-testng-test-using-playwright-java\/","title":{"rendered":"Create TestNg Test using Playwright Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3486\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java to create a TestNg Test.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Add TestNG maven dependency<\/li>\n<li>Create TestNG test<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Add TestNG maven dependency<\/strong><\/p>\n<p>We start by adding TestNg dependency in pom.xml<\/p>\n<p>Navigate to below site\u00a0<\/p>\n<p><a class=\"\" href=\"https:\/\/mvnrepository.com\/artifact\/org.testng\/testng\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/mvnrepository.com\/artifact\/org.testng\/testng<\/a> \u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708d4beb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3489\" data-css=\"tve-u-194708d61e7\" data-height=\"357\" data-id=\"3489\" data-init-height=\"784\" data-init-width=\"1198\" data-width=\"546\" decoding=\"async\" height=\"357\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.47.03\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 784;\" title=\"Screenshot 2025-01-17 at 12.47.03\u202fAM\" width=\"546\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Paste in pom.xml file<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708e316d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3490\" data-css=\"tve-u-194708e45e1\" data-height=\"572\" data-id=\"3490\" data-init-height=\"1316\" data-init-width=\"1198\" data-width=\"521\" decoding=\"async\" height=\"572\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.48.01\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1316;\" title=\"Screenshot 2025-01-17 at 12.48.01\u202fAM\" width=\"521\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Note<\/strong>: The dependency (see below) shows the scope as \u2018test\u2019. This means, the TestNg jar will only be recognized under src\/test\/java folder\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708eb6d0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3491\" data-css=\"tve-u-194708ec7ba\" data-height=\"359\" data-id=\"3491\" data-init-height=\"940\" data-init-width=\"1198\" data-width=\"457\" decoding=\"async\" height=\"359\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.48.41\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 940;\" title=\"Screenshot 2025-01-17 at 12.48.41\u202fAM\" width=\"457\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p>Thus TestNg jar will not be recognized under src\/main\/java folder. If TestNg jar needs to be recognized under src\/main\/java folder, simply remove test scope from dependency. As of now, let us leave the scope as is.<\/p>\n<p><strong>Create TestNG Test<\/strong><\/p>\n<p>We will now create TestNg test utilizing the TestNg jar library.<\/p>\n<p>Let us create a class under under src\/test\/java and create 3 annotations: <br \/><em>@BeforeMethod, @Test, @AfterMethod<\/em><\/p>\n<p>We will also create 3 methods: <em>setUp(), test_1(), tearDown()<\/em><\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708f70f8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3492\" data-css=\"tve-u-194708f839a\" data-height=\"482\" data-id=\"3492\" data-init-height=\"1074\" data-init-width=\"1198\" data-width=\"538\" decoding=\"async\" height=\"482\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.49.26\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1074;\" title=\"Screenshot 2025-01-17 at 12.49.26\u202fAM\" width=\"538\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We would initialize playwright instance, browser and page instance etc inside <em>@BeforeMethod<\/em><\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708fdfce\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3493\" data-css=\"tve-u-194708ffc02\" data-height=\"207\" data-id=\"3493\" data-init-height=\"322\" data-init-width=\"1198\" data-width=\"772\" decoding=\"async\" height=\"207\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.50.01\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 322;\" title=\"Screenshot 2025-01-17 at 12.50.01\u202fAM\" width=\"772\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We will write our actual Test within <em>@Test<\/em><br \/>\n<em>\u00a0<\/em><\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470908de4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3494\" data-css=\"tve-u-1947090ac8b\" data-height=\"183\" data-id=\"3494\" data-init-height=\"360\" data-init-width=\"1198\" data-width=\"609\" decoding=\"async\" height=\"183\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.50.42\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 360;\" title=\"Screenshot 2025-01-17 at 12.50.42\u202fAM\" width=\"609\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The error is thrown because the \u2018page\u2019 object created under @BeforeMethod is local to @BeforeMethod. Hence @Test method does not recognize \u2018page\u2019 object created inside @BeforeMethod.<\/p>\n<p>We will create instance variables to correct the error. These variables can be accessed throught entire class<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194709133c3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3495\" data-css=\"tve-u-1947091430e\" data-height=\"356\" data-id=\"3495\" data-init-height=\"726\" data-init-width=\"1198\" data-width=\"588\" decoding=\"async\" height=\"356\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.51.21\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 726;\" title=\"Screenshot 2025-01-17 at 12.51.21\u202fAM\" width=\"588\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<em>@Test<\/em> does not throw any error now\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947092337a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3496\" data-css=\"tve-u-194709243c0\" data-height=\"521\" data-id=\"3496\" data-init-height=\"1064\" data-init-width=\"1198\" data-width=\"587\" decoding=\"async\" height=\"521\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.52.23\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1064;\" title=\"Screenshot 2025-01-17 at 12.52.23\u202fAM\" width=\"587\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Our code looks like below:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947092c96b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3497\" data-css=\"tve-u-1947092e046\" data-height=\"858\" data-id=\"3497\" data-init-height=\"1658\" data-init-width=\"1198\" data-width=\"620\" decoding=\"async\" height=\"858\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.53.07\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1658;\" title=\"Screenshot 2025-01-17 at 12.53.07\u202fAM\" width=\"620\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Notice that the desired text gets typed inside the frame<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194709387cb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3498\" data-css=\"tve-u-1947093a4d9\" data-height=\"592\" data-id=\"3498\" data-init-height=\"1248\" data-init-width=\"1198\" data-width=\"568\" decoding=\"async\" height=\"592\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.53.50\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1248;\" title=\"Screenshot 2025-01-17 at 12.53.50\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>This is how we can create and execute a TestNG test using playwright java.<\/p>\n<p><strong>Source code<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog28_TestNGTest_PWJava {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>page = browser.newPage();<\/p>\n<p>}<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> test_1() {<\/p>\n<p>page.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/iframe&#8221;);<\/p>\n<p>Locator frame1 = page.frameLocator(&#8220;#mce_0_ifr&#8221;).locator(&#8220;html&#8221;);<\/p>\n<p>frame1.click();<\/p>\n<p>frame1.type(&#8220;https:\/\/rahulshettyacademy.com&#8221;);<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> tearDown() {<\/p>\n<p>page.pause();<\/p>\n<p>browser.close();<\/p>\n<p>page.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to create a TestNg Test. Topics that we will cover: Add TestNG maven dependency Create TestNG test Source code Add TestNG maven dependency We start by adding TestNg dependency in pom.xml Navigate to below site\u00a0 https:\/\/mvnrepository.com\/artifact\/org.testng\/testng \u00a0 \u00a0Paste in pom.xml file \u00a0Note: The dependency (see below) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9,3],"tags":[],"class_list":["post-3486","post","type-post","status-publish","format-standard","hentry","category-java","category-playwright","category-testng","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3486"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3486\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}