{"id":3122,"date":"2025-01-14T16:01:11","date_gmt":"2025-01-14T16:01:11","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/14\/timeouts-in-playwright-java\/"},"modified":"2025-01-14T16:01:11","modified_gmt":"2025-01-14T16:01:11","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><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=\"3122\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog, we will customize and set the default global timeouts in Playwright Java to suit our needs.<\/em><em>\u00a0<\/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>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 class=\"\" href=\"https:\/\/playwright.dev\/docs\/test-timeouts\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/playwright.dev\/docs\/test-timeouts<\/a>\u00a0<\/p>\n<p>Below, it can be observed that there is no specified limit for &#8216;Global Timeout&#8217;\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194658f1e05\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3125\" data-css=\"tve-u-194658f2e79\" data-height=\"355\" data-id=\"3125\" data-init-height=\"742\" data-init-width=\"1196\" data-width=\"572\" decoding=\"async\" height=\"355\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.33.24\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 742;\" title=\"Screenshot 2025-01-14 at 9.33.24\u202fPM\" width=\"572\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><\/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 class=\"\" href=\"http:\/\/uitestingplayground.com\/ajax\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">http:\/\/uitestingplayground.com\/ajax<\/a>\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194658fea0e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3126\" data-css=\"tve-u-194658ffbe6\" data-height=\"181\" data-id=\"3126\" data-init-height=\"348\" data-init-width=\"1196\" data-width=\"621\" decoding=\"async\" height=\"181\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.34.19\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 348;\" title=\"Screenshot 2025-01-14 at 9.34.19\u202fPM\" width=\"621\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In other words, by clicking the blue button, the ajax data will be loaded after a delay of 15 seconds<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465906f45\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3127\" data-css=\"tve-u-19465907f51\" data-height=\"452\" data-id=\"3127\" data-init-height=\"988\" data-init-width=\"1196\" data-width=\"547\" decoding=\"async\" height=\"452\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.35.01\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 988;\" title=\"Screenshot 2025-01-14 at 9.35.01\u202fPM\" width=\"547\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>We can use the above text to locate this button<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194659123f6\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3128\" data-css=\"tve-u-194659131ea\" data-height=\"319\" data-id=\"3128\" data-init-height=\"596\" data-init-width=\"1196\" data-width=\"640\" decoding=\"async\" height=\"319\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.35.45\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 596;\" title=\"Screenshot 2025-01-14 at 9.35.45\u202fPM\" width=\"640\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Once the button is clicked, the playwright will wait until the ajax data is received, utilizing its default timeout.\u00a0<\/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\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946591a70d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3129\" data-css=\"tve-u-1946591c58c\" data-height=\"229\" data-id=\"3129\" data-init-height=\"436\" data-init-width=\"1196\" data-width=\"627\" decoding=\"async\" height=\"229\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.36.19\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 436;\" title=\"Screenshot 2025-01-14 at 9.36.19\u202fPM\" width=\"627\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We will next locate the ajax output data by using its classname, retrieve its innertext in a variable and finally print the same<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465923691\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3130\" data-css=\"tve-u-1946592464e\" data-height=\"125\" data-id=\"3130\" data-init-height=\"294\" data-init-width=\"1196\" data-width=\"509\" decoding=\"async\" height=\"125\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.36.56\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 294;\" title=\"Screenshot 2025-01-14 at 9.36.56\u202fPM\" width=\"509\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>The ajax output message gets printed in the console, see below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946592b87d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3131\" data-css=\"tve-u-1946592d02e\" data-height=\"295\" data-id=\"3131\" data-init-height=\"666\" data-init-width=\"1196\" data-width=\"529\" decoding=\"async\" height=\"295\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.37.25\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 666;\" title=\"Screenshot 2025-01-14 at 9.37.25\u202fPM\" width=\"529\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Despite not specifying a timeout, the playwright automatically waited for the ajax output data for 15 seconds without throwing any exception.\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19465937840\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3132\" data-css=\"tve-u-19465938b02\" data-height=\"216\" data-id=\"3132\" data-init-height=\"446\" data-init-width=\"1196\" data-width=\"578\" decoding=\"async\" height=\"216\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-14-at-9.38.04\u202fPM.png\" style=\"aspect-ratio: auto 1196 \/ 446;\" title=\"Screenshot 2025-01-14 at 9.38.04\u202fPM\" width=\"578\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/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>\u00a0<\/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>\u00a0<\/p>\n<p>Execute the code.\u00a0<\/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>\u00a0<\/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>\u00a0<\/p>\n<p>Execute the code.\u00a0<\/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>\u00a0<\/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><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog8_Timeouts {<\/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>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>\u00a0 \u00a0 pg.locator(&#8220;text=Button Triggering AJAX Request&#8221;).click();<\/p>\n<p>\u00a0 \u00a0<\/p>\n<p>\u00a0 \u00a0 String ajmsg =\u00a0 pg.locator(&#8220;.bg-success&#8221;).innerText();<\/p>\n<p>\u00a0 \u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(ajmsg);<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Code snippet (personalized wait)<\/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.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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog8_Timeouts {<\/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>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>\u00a0 \u00a0 pg.locator(&#8220;text=Button Triggering AJAX Request&#8221;).click();<\/p>\n<p>\u00a0 \u00a0<\/p>\n<p>\u00a0 \u00a0 String ajmsg =\u00a0 pg.locator(&#8220;.bg-success&#8221;).innerText();<\/p>\n<p>\u00a0 \u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(ajmsg);<\/p>\n<p>pg.pause();<\/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 customize and set the default global timeouts in Playwright Java to suit our needs.\u00a0 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\u00a0 Below, it [&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-3122","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\/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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3122\/revisions"}],"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}]}}