Webdriver synchronization Settings Common wait classes are shown in the main
Note: Support.ui packet class mainly realizes the explicit wait function, and the timeouts () method mainly realizes the recessive wait function.One. Thread hibernation
Thread.Sleep (long Millis)
Two. Stealth Waiting
implicit wait: Set once, driver throughout the life cycle, no need to explicitly set for elements
Driver.manage (). Timeouts (). implicitlywait (long outtime, timeunit unit);
Global setting, setting the time-out for driver to perform all positioning element operations
Parameters: Outtime – Timeout wait time; Unit – time units.
Ex.
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); // Timeout Wait time is 10S
Three. Explicit wait
Explicit wait: The waiting method needs to be explicitly called when the element to be awaited is positioned
Figure 1 Support.ui package Common class UML
[ Common wait template ]
- Wait for element to load
1) webdriverwait
New Long timeoutinseconds). Until (expectedconditions.presenceofelementlocated (by))
Annotations :
- Parameters:driver-webdriver; timeoutinseconds- timeout wait time ( s); by- element Locator
- timeoutinseconds within the range, waiting for the conditions in the until () method to be met and immediately jump out of the waiting.
- beyond timeoutinseconds, throws an exception.
2) fluentwait
New Fluentwait<webdriver>(Driver) . Withtimeout (long timeOut, timeunit unit) . Pollingevery (long duration, timeunit unit) . Ignoring (exceptiontype); Wait.until ( Expectedconditions.presenceofelementlocated (by.by));
Annotations :
2. Wait and get elements-just modify the until method body
webelement element = wait.until ( new expectedcondition<webelement>() {@Override
public
webelement apply (webdriver driver) {
return
driver.findelement (by); }});
Annotations :
- Parameters:driver-webdriver, from Webdriverwait| Fluentwait in the instantiation method ; by- element Locator
- Return : returns webelement if the element is positioned;
[ case Study ]
- Wait for page element to load
New Webdriverwait (driver,10); Wait.until ( expectedconditions presenceofelementlocated (by.id(" Mydynamicelement ")));
method function: Positioning id= ' mydynamicelement ' the element, timeout wait time is 10S
wait<webdriver> Wait = new fluentwait<webdriver> (driver). Withtimeout ( 6010 class ) Mydynamicelement ")));
method function: Positioning id= ' mydynamicelement ' the element, timeout wait time is 60S , every $ locate once and encounter nosuchelementexception error ignored, continue to wait until more than 60s .
- Wait and get page elements
New Webdriverwait (Driver, ten= wait.until ( new expectedcondition< webelement>( { @Override public webelement apply (webdriver d) { return D.findelement (By.id ("Mydynamicelement") ); } );
method function: Positioning id= ' mydynamicelement ' the element, timeout wait time is 10S , if you navigate to an element, return the element directly
wait<webdriver> Wait = new fluentwait<webdriver> (driver). Withtimeout ( 6010 class ); webelement element = Wait.until ( new
expectedcondition<webelement> () {@Override public Webelement apply (Webdriver d) { return d.findelement (by.id ("mydynamicelement"
method function: Positioning id= ' mydynamicelement ' the element, timeout wait time is 60S , every $ position once, 60s encountered in nosuchelementexception error ignored, continue to wait, if you navigate to the element, directly return the page element.
If you just want to tell if the page is loaded somewhere, , You can use the first method ; But if you need to get a Webelement, two different ways to , It's just the first way you need to get the action one more step .
Four. Wait
The Fluentwait class is an implementation of the wait interface, and you can use the wait interface directly to perform the waiting functions you need, such as
New Wait () { @Override publicboolean until () { return Webelement.isdisplayed (); }};
this way of waiting , It is said that loading JS It's easier to make judgments when you code. , no debugging is currently successful.
V. Other1. Wait for the page to load
Driver.manage (). Timeouts (). Pageloadtimeout (SECONDS);
Annotations:
- The Pageloadtimeout method applies only to Firefox browsers, Chrome and other browsers do not work, but we can implement a timer on our own
- This method sets the page load time-out to 100s
2. Wait for the asynchronous script to load
Driver.manage (). Timeouts (). Setscripttimeout (100,seconds);
Not yet debugged by
"Slenium topic" Webdriver Sync settings