{"id":3137,"date":"2025-01-15T19:00:20","date_gmt":"2025-01-15T19:00:20","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/understand-explicit-timeout-in-playwright-java\/"},"modified":"2025-01-15T19:00:20","modified_gmt":"2025-01-15T19:00:20","slug":"understand-explicit-timeout-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/understand-explicit-timeout-in-playwright-java\/","title":{"rendered":"Understand Explicit timeout 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=\"3137\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog, we will study Playwright Java&#8217;s ability to set a timeout explicitly for actions.<\/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\u2019s explicit timeout for specific actions\u00a0<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Playwright Java\u2019s explicit timeout for specific actions\u00a0<\/strong><\/p>\n<p>We have already seen setting up of global default timeout.<\/p>\n<p>We will examine the procedure for establishing an explicit timeout for a specific element in playwright-java.<\/p>\n<p>Let us again go 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-1946b58ee8e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3140\" data-css=\"tve-u-1946b58ffd1\" data-height=\"179\" data-id=\"3140\" data-init-height=\"392\" data-init-width=\"1196\" data-width=\"547\" decoding=\"async\" height=\"179\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.31.48\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 392;\" title=\"Screenshot 2025-01-16 at 12.31.48\u202fAM\" width=\"547\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>In 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-1946b5995c0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3141\" data-css=\"tve-u-1946b59ac1a\" data-height=\"466\" data-id=\"3141\" data-init-height=\"1038\" data-init-width=\"1196\" data-width=\"537\" decoding=\"async\" height=\"466\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.32.37\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 1038;\" title=\"Screenshot 2025-01-16 at 12.32.37\u202fAM\" width=\"537\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Next, 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-1946b5a3687\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3142\" data-css=\"tve-u-1946b5a4692\" data-height=\"536\" data-id=\"3142\" data-init-height=\"1076\" data-init-width=\"1196\" data-width=\"596\" decoding=\"async\" height=\"536\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.33.19\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 1076;\" title=\"Screenshot 2025-01-16 at 12.33.19\u202fAM\" width=\"596\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Before we set the explicit wait timeout, let us first comment default timeout (line#27 in above snapshot).\u00a0<\/p>\n<p>In this use case, the idea of explicit wait timeout is that, we will wait explicitly for ajax data output element.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5b7f83\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3143\" data-css=\"tve-u-1946b5b9d62\" data-height=\"43\" data-id=\"3143\" data-init-height=\"100\" data-init-width=\"1196\" data-width=\"519\" decoding=\"async\" height=\"43\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.34.38\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 100;\" title=\"Screenshot 2025-01-16 at 12.34.38\u202fAM\" width=\"519\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Once the element is visble, we will than fetch the innertext of ajax data output (line#32 in above snapshot).<\/p>\n<p>You can utilize the <em>waitForSelector()<\/em> method by providing the selector and the timeout as its arguments<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5c3d67\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3144\" data-css=\"tve-u-1946b5c528b\" data-height=\"217\" data-id=\"3144\" data-init-height=\"432\" data-init-width=\"1196\" data-width=\"602\" decoding=\"async\" height=\"217\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.35.31\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 432;\" title=\"Screenshot 2025-01-16 at 12.35.31\u202fAM\" width=\"602\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The locator of the ajax data output will be stored in the selector argument, as it represents the element we need to wait for<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5cd44b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3145\" data-css=\"tve-u-1946b5ce92d\" data-height=\"205\" data-id=\"3145\" data-init-height=\"432\" data-init-width=\"1196\" data-width=\"567\" decoding=\"async\" height=\"205\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.36.13\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 432;\" title=\"Screenshot 2025-01-16 at 12.36.13\u202fAM\" width=\"567\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Below is the syntax for the second &#8216;timeout&#8217; argument. We will specify a timeout duration of 16 seconds<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5d77c4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3146\" data-css=\"tve-u-1946b5d9971\" data-height=\"244\" data-id=\"3146\" data-init-height=\"494\" data-init-width=\"1196\" data-width=\"591\" decoding=\"async\" height=\"244\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.36.53\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 494;\" title=\"Screenshot 2025-01-16 at 12.36.53\u202fAM\" width=\"591\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>The test will be successful. The console prints the ajax output message<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5e471e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3147\" data-css=\"tve-u-1946b5e6818\" data-height=\"312\" data-id=\"3147\" data-init-height=\"628\" data-init-width=\"1196\" data-width=\"595\" decoding=\"async\" height=\"312\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.37.36\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 628;\" title=\"Screenshot 2025-01-16 at 12.37.36\u202fAM\" width=\"595\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>The test fails this time, because the ajax data is loaded after 15 seconds, exceeding the timeout limit of 14 seconds that we have set up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5f157c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3148\" data-css=\"tve-u-1946b5f3430\" data-height=\"459\" data-id=\"3148\" data-init-height=\"938\" data-init-width=\"1196\" data-width=\"585\" decoding=\"async\" height=\"459\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.38.34\u202fAM.png\" style=\"aspect-ratio: auto 1196 \/ 938;\" title=\"Screenshot 2025-01-16 at 12.38.34\u202fAM\" width=\"585\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Execute the code.<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b5fd7b1\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3149\" data-css=\"tve-u-1946b5ff2ba\" data-height=\"447\" data-id=\"3149\" data-init-height=\"784\" data-init-width=\"996\" data-width=\"568\" decoding=\"async\" height=\"447\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-12.39.28\u202fAM.png\" style=\"aspect-ratio: auto 996 \/ 784;\" title=\"Screenshot 2025-01-16 at 12.39.28\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how the explicit timeout functions for a specific element.<\/p>\n<p><strong>Source code\u00a0<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog9_ExplicitTimeout {<\/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 \/\/set explicit timeout<\/p>\n<p>\u00a0 \u00a0 \/\/pg.waitForSelector(&#8220;.bg-success&#8221;, new Page.WaitForSelectorOptions().setTimeout(16000));<\/p>\n<p>\u00a0 \u00a0 \/\/pg.waitForSelector(&#8220;.bg-success&#8221;, new Page.WaitForSelectorOptions().setTimeout(14000));<\/p>\n<p>\u00a0 \u00a0 pg.waitForSelector(&#8220;.bg-success&#8221;, <strong>new<\/strong> Page.WaitForSelectorOptions().setTimeout(15000));<\/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 study Playwright Java&#8217;s ability to set a timeout explicitly for actions. Topics that we will cover: Playwright Java\u2019s explicit timeout for specific actions\u00a0 Source code Playwright Java\u2019s explicit timeout for specific actions\u00a0 We have already seen setting up of global default timeout. We will examine the procedure for establishing an [&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-3137","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\/3137","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=3137"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3137\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}