{"id":3026,"date":"2025-01-14T13:48:51","date_gmt":"2025-01-14T13:48:51","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3026"},"modified":"2025-01-17T10:25:12","modified_gmt":"2025-01-17T10:25:12","slug":"write-and-run-the-first-playwright-java-test","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/write-and-run-the-first-playwright-java-test\/","title":{"rendered":"Write and run the first Playwright-Java Test"},"content":{"rendered":"<p><em>In this blog post, you will learn how to create the first playwright-Java test, execute the test and optimise the code.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Create playwright java program<\/li>\n<li>Execute the first Playwright Test<\/li>\n<li>Refactor (refine) the code<\/li>\n<li>Source code (without refactoring)<\/li>\n<li>Source code (with refactoring)<\/li>\n<\/ul>\n<p><strong>Create playwright java program&nbsp;<\/strong><\/p>\n<p>Create a simple java class under src\/test\/java&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3031\" width=\"924\" data-init-width=\"1198\" height=\"295\" data-init-height=\"382\" title=\"1\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/1-2.png\" data-width=\"924\" data-height=\"295\" style=\"aspect-ratio: auto 1198 \/ 382;\"><\/span><\/p>\n<p>\u200b<\/p>\n<p>Create @Test and import required packages.&nbsp;<\/p>\n<p>We than create an object of Playwright. Ensure there are no compile errors<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3032\" width=\"889\" data-init-width=\"1198\" height=\"426\" data-init-height=\"574\" title=\"2\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/2-2.png\" data-width=\"889\" data-height=\"426\" style=\"aspect-ratio: auto 1198 \/ 574;\"><\/span><\/p>\n<p>If there are no errors, we can continue.&nbsp;<\/p>\n<p>The code below is self-explanatory where we are creating the Chromium Browser type object (line #13) and then launching the Chromium Browser (line #14).<\/p>\n<p>Next, we create a new page (line #15) on this launched browser and navigate to the webpage (line #16).&nbsp;<\/p>\n<p>Finally, we print the page title (line #17) &nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3033\" width=\"876\" data-init-width=\"1198\" height=\"605\" data-init-height=\"828\" title=\"3\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-7.24.27\u202fPM.png\" data-width=\"876\" data-height=\"605\" style=\"aspect-ratio: auto 1198 \/ 828;\"><\/span><\/p>\n<p><strong>Execute the first Playwright Test<\/strong><\/p>\n<p>Run the test.&nbsp;<\/p>\n<p>When we execute the playwright test for the first time, the browsers get downloaded as can be seen below.&nbsp;<\/p>\n<p>We can see chromium, webkit, firefox etc browsers getting downloaded to our local machine&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3034\" width=\"871\" data-init-width=\"1198\" height=\"1099\" data-init-height=\"1512\" title=\"4\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/4-2.png\" data-width=\"871\" data-height=\"1099\" style=\"aspect-ratio: auto 1198 \/ 1512;\"><\/span><\/p>\n<p>The title of the page is printed as can be seen above.&nbsp;<\/p>\n<p>This page title (that gets printed) matches with the actual page title, as can be seen below&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3035\" width=\"909\" data-init-width=\"1198\" height=\"951\" data-init-height=\"1254\" title=\"5\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/5-2.png\" data-width=\"909\" data-height=\"951\" style=\"aspect-ratio: auto 1198 \/ 1254;\"><\/span><\/p>\n<p><strong>Refactor (refine) the code<\/strong><\/p>\n<p>To refactor the code, comment lines#13 and 14.&nbsp;<\/p>\n<p>We now append the respective methods (method to launch chromium) in line#15<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3036\" width=\"909\" data-init-width=\"1198\" height=\"467\" data-init-height=\"616\" title=\"6\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/6-2.png\" data-width=\"909\" data-height=\"467\" style=\"aspect-ratio: auto 1198 \/ 616;\"><\/span><\/p>\n<p>Run the code.&nbsp;<\/p>\n<p>The title of the page gets printed<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3037\" width=\"892\" data-init-width=\"1164\" height=\"136\" data-init-height=\"178\" title=\"7\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/7-2.png\" data-width=\"892\" data-height=\"136\" style=\"aspect-ratio: auto 1164 \/ 178;\"><\/span><\/p>\n<p>&nbsp;<strong>Source code (without refactoring)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.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.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> PlaywrightJavaTest {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch();<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/&#8221;);<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(pg.title());<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (with refactoring)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<\/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> PlaywrightJavaTest {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>\/\/BrowserType btype = pt.chromium();<\/p>\n<p>\/\/Browser b = btype.launch();<\/p>\n<p>Page pg = pt.chromium().launch().newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/mentorship&#8221;);<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(pg.title());<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>So this is how we execute the playwright code.<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, you will learn how to create the first playwright-Java test, execute the test and optimise the code. Topics that we will cover: Create playwright java program Execute the first Playwright Test Refactor (refine) the code Source code (without refactoring) Source code (with refactoring) Create playwright java program&nbsp; Create a simple java [&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-3026","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\/3026","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=3026"}],"version-history":[{"count":8,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3026\/revisions"}],"predecessor-version":[{"id":3689,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3026\/revisions\/3689"}],"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=3026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}