"Slenium topic" Webdriver Sync settings

Source: Internet
Author: User

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 ]

    1. 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 :

  • Parameters: Driver-webdriver; TimeOut - timeout wait time (s); unit-time Unit; by- element locator;

    duration-Find element time interval ; exceptiontype-ignores exception types, such as default Nosuchelementexception.class

  • timeOut range,driver every dration to locate the element, if you encounter Exceptiontype continues to wait until the conditions in the until () method are met , immediately jumping out of the waiting.
  • Beyond Timeoutinseconds still until condition not met , throws an exception.

   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 ]

    1. Wait for page element to load
      • Webdriverwait
New Webdriverwait (driver,10); Wait.until (    expectedconditions presenceofelementlocated (by.id(" Mydynamicelement ")));

method function: Positioning id= ' mydynamicelement ' the element, timeout wait time is 10S

      • Fluentwait
 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 .

    1. Wait and get page elements
      • Webdriverwait
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

      •   fluentwait
 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.