{"id":3122,"date":"2025-01-14T16:01:11","date_gmt":"2025-01-14T16:01:11","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3122"},"modified":"2025-01-17T10:32:15","modified_gmt":"2025-01-17T10:32:15","slug":"timeouts-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/timeouts-in-playwright-java\/","title":{"rendered":"Timeouts in Playwright Java"},"content":{"rendered":"<p><em>In this blog, we will customize and set the default global timeouts in Playwright Java to suit our needs.<\/em><em>&nbsp;<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Playwright Java&#8217;s Default Global Timeouts<\/li>\n<li>Personalized timeout<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Playwright Java&#8217;s Default Global Timeouts<\/strong><\/p>\n<p>Explore the official webpage that specifically focuses on explaining the different Timeouts in Playwright<\/p>\n<p><a href=\"https:\/\/playwright.dev\/docs\/test-timeouts\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/playwright.dev\/docs\/test-timeouts<\/a>&nbsp;<\/p>\n<p>Below, it can be observed that there is no specified limit for &#8216;Global Timeout&#8217;&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3125\" width=\"572\" data-init-width=\"1196\" height=\"355\" data-init-height=\"742\" title=\"Screenshot 2025-01-14 at 9.33.24\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.33.24\u202fPM.png\" data-width=\"572\" data-height=\"355\" style=\"aspect-ratio: auto 1196 \/ 742;\"><\/span><\/p>\n<p>Let&#8217;s explore the practical implementation of &#8216;timeout&#8217; to gain a better understanding of its usage.<\/p>\n<p>Navigate to <a href=\"http:\/\/uitestingplayground.com\/ajax\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">http:\/\/uitestingplayground.com\/ajax<\/a>&nbsp;<\/p>\n<p>According to the scenario description, after pressing the button, the ajax data will become visible after a 15-second delay, implying a wait time of 15 seconds<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3126\" width=\"621\" data-init-width=\"1196\" height=\"181\" data-init-height=\"348\" title=\"Screenshot 2025-01-14 at 9.34.19\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.34.19\u202fPM.png\" data-width=\"621\" data-height=\"181\" style=\"aspect-ratio: auto 1196 \/ 348;\"><\/span><\/p>\n<p>&nbsp;In other words, by clicking the blue button, the ajax data will be loaded after a delay of 15 seconds<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3127\" width=\"547\" data-init-width=\"1196\" height=\"452\" data-init-height=\"988\" title=\"Screenshot 2025-01-14 at 9.35.01\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.35.01\u202fPM.png\" data-width=\"547\" data-height=\"452\" style=\"aspect-ratio: auto 1196 \/ 988;\"><\/span><\/p>\n<p>We can use the above text to locate this button<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3128\" width=\"640\" data-init-width=\"1196\" height=\"319\" data-init-height=\"596\" title=\"Screenshot 2025-01-14 at 9.35.45\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.35.45\u202fPM.png\" data-width=\"640\" data-height=\"319\" style=\"aspect-ratio: auto 1196 \/ 596;\"><\/span><\/p>\n<p>&nbsp;Once the button is clicked, the playwright will wait until the ajax data is received, utilizing its default timeout.&nbsp;<\/p>\n<p>Next, the AJAX data output can be identified using class \u2018bg-success\u2019 or by text \u2018Data loaded with AJAX get request\u2019&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3129\" width=\"627\" data-init-width=\"1196\" height=\"229\" data-init-height=\"436\" title=\"Screenshot 2025-01-14 at 9.36.19\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.36.19\u202fPM.png\" data-width=\"627\" data-height=\"229\" style=\"aspect-ratio: auto 1196 \/ 436;\"><\/span><\/p>\n<p>&nbsp;We will next locate the ajax output data by using its classname, retrieve its innertext in a variable and finally print the same<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3130\" width=\"509\" data-init-width=\"1196\" height=\"125\" data-init-height=\"294\" title=\"Screenshot 2025-01-14 at 9.36.56\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.36.56\u202fPM.png\" data-width=\"509\" data-height=\"125\" style=\"aspect-ratio: auto 1196 \/ 294;\"><\/span><\/p>\n<p>&nbsp;Execute the code.&nbsp;<\/p>\n<p>The ajax output message gets printed in the console, see below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3131\" width=\"529\" data-init-width=\"1196\" height=\"295\" data-init-height=\"666\" title=\"Screenshot 2025-01-14 at 9.37.25\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.37.25\u202fPM.png\" data-width=\"529\" data-height=\"295\" style=\"aspect-ratio: auto 1196 \/ 666;\"><\/span><\/p>\n<p>&nbsp;Despite not specifying a timeout, the playwright automatically waited for the ajax output data for 15 seconds without throwing any exception.&nbsp;<\/p>\n<p><strong>Personalized timeout<\/strong><\/p>\n<p>There is a <em>setDefaultTimeout()<\/em> method using which we will now update the default timeout to 14 seconds<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3132\" width=\"578\" data-init-width=\"1196\" height=\"216\" data-init-height=\"446\" title=\"Screenshot 2025-01-14 at 9.38.04\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.38.04\u202fPM.png\" data-width=\"578\" data-height=\"216\" style=\"aspect-ratio: auto 1196 \/ 446;\"><\/span><\/p>\n<p>&nbsp;Execute the code.&nbsp;<\/p>\n<p>This time, the test is unsuccessful because we specified a 14-second wait for the playwright, but the ajax data is loaded after 15 seconds<\/p>\n<p>&nbsp;<\/p>\n<p>Let us modify the timeout parameters by commenting the 14-second timeout and implementing a default timeout of 16 seconds<\/p>\n<p>&nbsp;<\/p>\n<p>Execute the code.&nbsp;<\/p>\n<p>The test will be successful as the default timeout of 16 seconds exceeds the 15-second timeout required to show the ajax data<\/p>\n<p>&nbsp;<\/p>\n<p>Let us modify the timeout parameters by commenting the 16-second timeout and implementing a default timeout of exact 15 seconds<\/p>\n<p>&nbsp;<\/p>\n<p>Execute the code.&nbsp;<\/p>\n<p>The test is expected to fail because the default timeout of 15 seconds matches the timeout required to display ajax data within 15 seconds<\/p>\n<p>&nbsp;<\/p>\n<p>This is how timeouts work in playwright java.<\/p>\n<p><strong>Code snippet (default wait)<\/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.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.options.SelectOption;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog8_Timeouts {<\/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>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;http:\/\/uitestingplayground.com\/ajax&#8221;);<\/p>\n<p>&nbsp; &nbsp; pg.locator(&#8220;text=Button Triggering AJAX Request&#8221;).click();<\/p>\n<p>&nbsp; &nbsp;<\/p>\n<p>&nbsp; &nbsp; String ajmsg =&nbsp; pg.locator(&#8220;.bg-success&#8221;).innerText();<\/p>\n<p>&nbsp; &nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(ajmsg);<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Code snippet (personalized wait)<\/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.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.options.SelectOption;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog8_Timeouts {<\/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>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>\/\/pg.setDefaultTimeout(14000);<\/p>\n<p>\/\/pg.setDefaultTimeout(16000);<\/p>\n<p>pg.setDefaultTimeout(15000);<\/p>\n<p>pg.navigate(&#8220;http:\/\/uitestingplayground.com\/ajax&#8221;);<\/p>\n<p>&nbsp; &nbsp; pg.locator(&#8220;text=Button Triggering AJAX Request&#8221;).click();<\/p>\n<p>&nbsp; &nbsp;<\/p>\n<p>&nbsp; &nbsp; String ajmsg =&nbsp; pg.locator(&#8220;.bg-success&#8221;).innerText();<\/p>\n<p>&nbsp; &nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(ajmsg);<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will customize and set the default global timeouts in Playwright Java to suit our needs.&nbsp; Topics that we will cover: Playwright Java&#8217;s Default Global Timeouts Personalized timeout Source code Playwright Java&#8217;s Default Global Timeouts Explore the official webpage that specifically focuses on explaining the different Timeouts in Playwright https:\/\/playwright.dev\/docs\/test-timeouts&nbsp; Below, it [&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-3122","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\/3122","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=3122"}],"version-history":[{"count":8,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3122\/revisions"}],"predecessor-version":[{"id":3701,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3122\/revisions\/3701"}],"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=3122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}