{"id":3026,"date":"2025-01-14T13:48:51","date_gmt":"2025-01-14T13:48:51","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/write-and-run-the-first-playwright-java-test\/"},"modified":"2025-01-14T13:48:51","modified_gmt":"2025-01-14T13:48:51","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><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=\"3026\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<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<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>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\u00a0<\/strong><\/p>\n<p>Create a simple java class under src\/test\/java\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946516bbdd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3031\" data-css=\"tve-u-19465172c8f\" data-height=\"295\" data-id=\"3031\" data-init-height=\"382\" data-init-width=\"1198\" data-width=\"924\" decoding=\"async\" height=\"295\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/1-2.png\" style=\"aspect-ratio: auto 1198 \/ 382;\" title=\"1\" width=\"924\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><\/p>\n<p>Create @Test and import required packages.\u00a0<\/p>\n<p>We than create an object of Playwright. <br \/>Ensure there are no compile errors<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946517e6d6\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3032 tcb-moved-image\" data-css=\"tve-u-1946517f1d0\" data-height=\"426\" data-id=\"3032\" data-init-height=\"574\" data-init-width=\"1198\" data-width=\"889\" decoding=\"async\" height=\"426\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/2-2.png\" style=\"aspect-ratio: auto 1198 \/ 574;\" title=\"2\" width=\"889\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>If there are no errors, we can continue.\u00a0<\/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).\u00a0<\/p>\n<p>Finally, we print the page title (line #17) \u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946518b37e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3033 tcb-moved-image\" data-css=\"tve-u-1946518bcc4\" data-height=\"605\" data-id=\"3033\" data-init-height=\"828\" data-init-width=\"1198\" data-width=\"876\" decoding=\"async\" height=\"605\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-7.24.27\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 828;\" title=\"3\" width=\"876\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Execute the first Playwright Test<\/strong><\/p>\n<p>Run the test.\u00a0<\/p>\n<p>When we execute the playwright test for the first time, the browsers get downloaded as can be seen below.\u00a0<\/p>\n<p>We can see chromium, webkit, firefox etc browsers getting downloaded to our local machine\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946519afa3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3034 tcb-moved-image\" data-css=\"tve-u-1946519b58e\" data-height=\"1099\" data-id=\"3034\" data-init-height=\"1512\" data-init-width=\"1198\" data-width=\"871\" decoding=\"async\" height=\"1099\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/4-2.png\" style=\"aspect-ratio: auto 1198 \/ 1512;\" title=\"4\" width=\"871\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The title of the page is printed as can be seen above.\u00a0<\/p>\n<p>This page title (that gets printed) matches with the actual page title, as can be seen below\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194651ab398\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3035 tcb-moved-image\" data-css=\"tve-u-194651ac9d6\" data-height=\"951\" data-id=\"3035\" data-init-height=\"1254\" data-init-width=\"1198\" data-width=\"909\" decoding=\"async\" height=\"951\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/5-2.png\" style=\"aspect-ratio: auto 1198 \/ 1254;\" title=\"5\" width=\"909\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Refactor (refine) the code<\/strong><\/p>\n<p>To refactor the code, comment lines#13 and 14.\u00a0<\/p>\n<p>We now append the respective methods (method to launch chromium) in line#15<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194651b8c8f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3036 tcb-moved-image\" data-css=\"tve-u-194651b9114\" data-height=\"467\" data-id=\"3036\" data-init-height=\"616\" data-init-width=\"1198\" data-width=\"909\" decoding=\"async\" height=\"467\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/6-2.png\" style=\"aspect-ratio: auto 1198 \/ 616;\" title=\"6\" width=\"909\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Run the code.\u00a0<\/p>\n<p>The title of the page gets printed<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194651cae31\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3037\" data-css=\"tve-u-194651cccd0\" data-height=\"136\" data-id=\"3037\" data-init-height=\"178\" data-init-width=\"1164\" data-width=\"892\" decoding=\"async\" height=\"136\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/7-2.png\" style=\"aspect-ratio: auto 1164 \/ 178;\" title=\"7\" width=\"892\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Source code (without refactoring)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> PlaywrightJavaTest {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<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><\/p>\n<p><strong>Source code (with refactoring)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><\/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> PlaywrightJavaTest {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<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><\/p>\n<p>So this is how we execute the playwright code.<\/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 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\u00a0 Create a simple java [&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],"tags":[],"class_list":["post-3026","post","type-post","status-publish","format-standard","hentry","category-java","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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3026\/revisions"}],"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}]}}