{"id":3486,"date":"2025-01-16T19:15:45","date_gmt":"2025-01-16T19:15:45","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3486"},"modified":"2025-01-17T10:49:57","modified_gmt":"2025-01-17T10:49:57","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><em>In this blog we will be utilizing Playwright Java to create a TestNg Test.<\/em><\/p>\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&nbsp;<\/p>\n<p><a href=\"https:\/\/mvnrepository.com\/artifact\/org.testng\/testng\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/mvnrepository.com\/artifact\/org.testng\/testng<\/a> &nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3489\" width=\"546\" data-init-width=\"1198\" height=\"357\" data-init-height=\"784\" title=\"Screenshot 2025-01-17 at 12.47.03\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.47.03\u202fAM.png\" data-width=\"546\" data-height=\"357\" style=\"aspect-ratio: auto 1198 \/ 784;\"><\/span><\/p>\n<p>&nbsp;Paste in pom.xml file<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3490\" width=\"521\" data-init-width=\"1198\" height=\"572\" data-init-height=\"1316\" title=\"Screenshot 2025-01-17 at 12.48.01\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.48.01\u202fAM.png\" data-width=\"521\" data-height=\"572\" style=\"aspect-ratio: auto 1198 \/ 1316;\"><\/span><\/p>\n<p>&nbsp;<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&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3491\" width=\"457\" data-init-width=\"1198\" height=\"359\" data-init-height=\"940\" title=\"Screenshot 2025-01-17 at 12.48.41\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.48.41\u202fAM.png\" data-width=\"457\" data-height=\"359\" style=\"aspect-ratio: auto 1198 \/ 940;\"><\/span><\/p>\n<p>&nbsp;<\/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: <em>@BeforeMethod, @Test, @AfterMethod<\/em><\/p>\n<p>We will also create 3 methods: <em>setUp(), test_1(), tearDown()<\/em><\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3492\" width=\"538\" data-init-width=\"1198\" height=\"482\" data-init-height=\"1074\" title=\"Screenshot 2025-01-17 at 12.49.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.49.26\u202fAM.png\" data-width=\"538\" data-height=\"482\" style=\"aspect-ratio: auto 1198 \/ 1074;\"><\/span><\/p>\n<p>&nbsp;We would initialize playwright instance, browser and page instance etc inside <em>@BeforeMethod<\/em><\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3493\" width=\"772\" data-init-width=\"1198\" height=\"207\" data-init-height=\"322\" title=\"Screenshot 2025-01-17 at 12.50.01\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.50.01\u202fAM.png\" data-width=\"772\" data-height=\"207\" style=\"aspect-ratio: auto 1198 \/ 322;\"><\/span><\/p>\n<p>&nbsp;We will write our actual Test within <em>@Test<\/em><em>&nbsp;<\/em><\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3494\" width=\"609\" data-init-width=\"1198\" height=\"183\" data-init-height=\"360\" title=\"Screenshot 2025-01-17 at 12.50.42\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.50.42\u202fAM.png\" data-width=\"609\" data-height=\"183\" style=\"aspect-ratio: auto 1198 \/ 360;\"><\/span><\/p>\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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3495\" width=\"588\" data-init-width=\"1198\" height=\"356\" data-init-height=\"726\" title=\"Screenshot 2025-01-17 at 12.51.21\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.51.21\u202fAM.png\" data-width=\"588\" data-height=\"356\" style=\"aspect-ratio: auto 1198 \/ 726;\"><\/span><\/p>\n<p>&nbsp;<em>@Test<\/em> does not throw any error now&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3496\" width=\"587\" data-init-width=\"1198\" height=\"521\" data-init-height=\"1064\" title=\"Screenshot 2025-01-17 at 12.52.23\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.52.23\u202fAM.png\" data-width=\"587\" data-height=\"521\" style=\"aspect-ratio: auto 1198 \/ 1064;\"><\/span><\/p>\n<p>&nbsp;Our code looks like below:<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3497\" width=\"620\" data-init-width=\"1198\" height=\"858\" data-init-height=\"1658\" title=\"Screenshot 2025-01-17 at 12.53.07\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.53.07\u202fAM.png\" data-width=\"620\" data-height=\"858\" style=\"aspect-ratio: auto 1198 \/ 1658;\"><\/span><\/p>\n<p>&nbsp;Notice that the desired text gets typed inside the frame<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3498\" width=\"568\" data-init-width=\"1198\" height=\"592\" data-init-height=\"1248\" title=\"Screenshot 2025-01-17 at 12.53.50\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.53.50\u202fAM.png\" data-width=\"568\" data-height=\"592\" style=\"aspect-ratio: auto 1198 \/ 1248;\"><\/span><\/p>\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>\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>\n<p><strong>public<\/strong><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><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/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><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><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>\n<p>Thanks.<\/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&nbsp; https:\/\/mvnrepository.com\/artifact\/org.testng\/testng &nbsp; &nbsp;Paste in pom.xml file &nbsp;Note: The dependency (see below) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","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":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3486\/revisions"}],"predecessor-version":[{"id":3741,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3486\/revisions\/3741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"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}]}}