Explicit timeouts in Playwright
Java
October 25, 2022 No Comments
Playwright JAVA
Tutorial 11 – Explicit timeouts in
Playwright Java
What you will Learn in this blog:
• Explicit Timeout in Playwright
Java
• Code snippets
Explicit Timeout in Playwright
Java
In the previous article we had seen
how to setup global default
timeout.
Let us now see how to setup
explicit timeout for a specific
element in playwright-java.
Launch
http://uitestingplayground.com/ajax
Read the scenario description that
says “after we press the button, the
ajax data will appear after 15
seconds”. Thus the wait time here
is 15 seconds
This means that, on clicking the
button, the ajax data gets loaded
after 15 seconds
Inspect the button
Inspect the AJAX data. We can
identify the AJAX data using the
class ‘bg-success’ or by the text
‘Data loaded with AJAX get
request’
DataloadedwithAJAXgetrequest.
Elements Console Sources PerformanceinsightsA
div1d="content">
‹pclass="bg-success">DataloadedwithAJAXgetrequest.:
Below is the snippet from our
previous article. Let us comment
default timeout (line#18)
10e publicstaticvoidmain(String[]args){
11 Playwrightpw=Playwright.create();
121 Browserbrowser=pw.chromium).launchnewBrowserType.Launc
13
BrowserContextbrowserContext=browser.newContext();
16 Pagepage=browserContext.newPage();
17
18| //page.setDefaultTimeout(16000);//testpasses
19
20 page.navigate("http://uitestingplayground.com/ajax");
21
22 page.locator("text=ButtonTriggeringAJAXRequest").click();
23
24 Stringajmsg=page.locator(".bg-success").innerText();
25 System.out.println(ajxmsg);
We will now set the explicit wait
timeout. The idea here is that,
before we fetch the innertext of
output ajax data (line#24 seen
above), we will first wait explicitly
for the output ajax data viz
Once the data gets loaded, we will
fetch the innertext.
There is a ‘waitForSelector()
method that accepts 2 arguments:
the selector and the timeout
The selector argument will
basically hold the locator of ajax
data output (since this is the
element we want to wait for)
The syntax of second argument
(viz timeout) is as shown below.
Let us set the timeout of 16
seconds
So our code looks like below
16Pagepage=browserContext.newpage();
17
18//page.setDefaultTimeout(16000);//testpasses
19
20 page.navigate("http://uitestingplayground.com/ajax");
21
22page.locator("text=ButtonTriggeringAJAXRequest").click();
23
24setexplicittimeout
25page.waitForSelector".bg-success",newPage.WaitForSelectorOptions)-setTimeout(16000)):
26
27Stringajmsg=page.locator("-bg-success").innerText();
28System.out.println(ajxmsg);
Save and execute the test.
As expected, the test will pass and
the ajxmsg gets printed in the
console
•ExplicitWaitPW.javaX iProblems@JavadocleDeclarationEConsole23ECoverage ■X*1師感物園は旦・⽇
3*ortcom.microsoft.playExplicitWaitPW[JavaApplication]C:/ProgramFilesVava\jre1.8.0_191\bin\javaw.exe(25-Oct-2022,3:09:58PM)
8 DataloadedwithAJAXgetrequest.
9licclassExplicitWait
10epublicstaticvoidma
11 Playwrightpw=P
12 Browserbrowser=
13|
14 BrowserContextbr
15
16 Pagepage=brows
17|
18 //page.setDefault
19
20 page.navigate("ht
21
22. page.locator("tex. ..---.• ...00!・-・・0.......!!!!・・ニー!・・・・こ
23
24 //setexplicittimeout
25 page.waitForSelector(".bg-success",newPage.WaitForSelectorOptions).setTimeout(16000));
26 Stringajmsg=page.locator(".bg-success").innerText();
27 System.out.println(ajxmsg);
Let us now comment 16sec timeout
and add 14sec timeout as shown
below
Save and execute.
The test fails this time because the
ajax data gets loaded only after 15
seconds, however we have setup
timeout of 14 seconds
LExplicitWaitPW.javaX PlProblems@JavadocEDeclarationEConsole83ECoverage 熱愛⾯窗⽇回、⼝,
3*ortcom.microsoft.playExplicitWaitPWJavaApplication]C:ProgramFilesJavajre1.8.0_191\binjavaw.exe(25-Oct-2022,3:13:07PM)
8 Exceptioninthread"main"com.microsoft.playwright.TimeoutError:Errorf
9licclassExplicitWait message='Timeout14000msexceeded.
10epublicstaticvoidma ===========================10gS===========================
11 Playwrightpw=P waitingforselector".bg-success"tobevisible
12 Browserbrowser= ニニニニニニニニニニニニニニニニニニニ ミニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニ
13 name='TimeoutError
14 BrowserContextbr stack='TimeoutError:Timeout14000msexceeded.
15 ニニニニ10ニミニ
gs
16 Pagepage=brows waitingforselector".bg-success"tobevisible
17 :ニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニニ
18 //page.setDefault atProgressController.run(C:\Users\DELL\AppData\Local\Temp\playwright
19 atFrame.waitForSelector(C:\Users\DELL\AppData\Local\Temp\playwright-
20 page.navigate("ht atFrameDispatcher.waitForSelector(C:\Users\DELL\AppData\Local\Temp\p
21 atDispatcherConnection.dispatch(C:\Users\DELL\ApData\Local\Temo\bla
22 page.locator("tex.
23
24 //setexplicittimeout
25 //page.waitForSelector(".bg-success",newPage.WaitForSelectorOptions().setTimeout(16000));
26 pagewaitForSelector("'.bg-success",newPage.WaitForSelectorOptions).setTimeout(14000));
27 Stringajxmsg=page.locator(".bg-success").innerText();
28 System.out.println(ajxmsg);
So this is how the explcit timeout
on a specific element works.
Code snippet
package com.w2a.pwjava;
import
com.microsoft.playwright.Browser;
import
com.microsoft.playwright.BrowserContext;
import
com.microsoft.playwright.BrowserType;
import
com.microsoft.playwright.Page;
import
com.microsoft.playwright.Playwright;
public class ExplicitWaitPW {
public static void main(String[]
args) {
Playwright pw =
Playwright.create();
Browser browser =
pw.chromium().launch(new
BrowserType.LaunchOptions().setHeadless(false));
BrowserContext
browserContext =
browser.newContext();
Page page =
browserContext.newPage();
//page.setDefaultTimeout(16000);
//test passes
page.navigate("http://uitestingplayground.com/ajax");
page.locator("text=Button
Triggering AJAX
Request").click();
//set explicit timeout
//page.waitForSelector(".bg-
success", new
Page.WaitForSelectorOptions().setTimeout(16000));//test
passes
page.waitForSelector(".bg-
success", new
Page.WaitForSelectorOptions().setTimeout(14000));//test
fails
String ajxmsg =
page.locator(".bg-
success").innerText();
System.out.println(ajxmsg);
}
}
Thanks for reading!
Share On
Leave a Comment
Your email address will not be published.
Required fields are marked *
Type here..
Name*
Email*
Website
Save my name, email, and website in this
browser for the next time I comment.
Post Comment »
Search
Recent Posts
Mouse Hover element using Selenide
Download a File using Selenide
Upload a file using Selenide
Handle dropdowns in Selenide
Usage of texts() method and ‘List’ in Selenide
Archives
March 2023
February 2023
January 2023
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
March 2022
February 2022
January 2022
December 2021
November 2021
October 2021
September 2021
Categories
Behave BDD
Cucumber BDD
Cypress
DevOps
Playwright JAVA
Playwright JS
Playwright Python
Selenide
Selenium 4.0
Selenium C#
Selenium Java
Worked with various CMM level
orgranizations. Involved in setting up of
manual and automation testing teams.
Implemented various automation projects
using Selenium, Webservices REST APIs,
QTP, SOAP UI, Cypress, Robot Framework,
Protractor, JMeter etc.
IMPORTANT LINKS
Home
About Us
Member Login
Lifetime Membership
Buy Now
Careers
Contact Us
Privacy Policy
Cancellation / Refund & Return policy
Terms and Conditions
COURSES
Selenium Java
Selenium Python
API Testing
DevOps
Jmeter
Manual Testing
Appium Java
Appium Python
Robot Framework
ABOUT US
Way2Automation
CDR Complex, 3rd Floor, Naya Bans Market,
Sector 15, Noida, Near sec-16 Metro Station
+91 97111-11-558
+91 97111-91-558
trainer@way2automation.com
seleniumcoaching@gmail.com
Ⓒ Way2Automation - All Rights Are Reserved