Webdriver api--delay operation and element wait

Source: Internet
Author: User

In the process of automated testing, by the network, testing equipment and many other factors, we often need to add some delay in the automated test script to better locate elements to do a series of operations.

There are generally several ways to do this:

1.implicitlyWait. Time-out when the object is recognized. After this time, if the object hasn't been found, it throws a Nosuchelement exception.

2.setScriptTimeout. The time-out period for the asynchronous script. Webdriver can execute the script asynchronously, which sets the time-out for the asynchronous execution script to return results.

3.pageLoadTimeout. The time-out period when the page loads. Because Webdriver will wait for the page to load after the completion of the operation, so if the page is not loaded within this timeout time, then Webdriver will throw an exception.

4.thread.sleep (). This is a way to suspend a thread and then wake up again, not recommended, that consumes system thread resources.

Package Com.testngdemo;import Java.util.concurrent.timeunit;import Org.openqa.selenium.by;import Org.openqa.selenium.nosuchelementexception;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.support.ui.expectedcondition;import Org.openqa.selenium.support.ui.webdriverwait;public Class Demo_timecontrol {public static void main (String args[]) {webdriver driver=new firefoxdriver ();d river.get ("http:/ /www.baidu.com ");//Set delay Operation Driver.manage (). Timeouts (). Pageloadtimeout (timeunit.seconds);//----- Page load Time driver.manage (). Timeouts (). implicitlywait (timeunit.seconds);//------element Wait time (implicit wait) driver.manage (). Timeouts (). Setscripttimeout (Ten, timeunit.seconds);//----Script Execution time//explicitly wait for webelement e = (new webdriverwait (Driver, 10)). Until (new expectedcondition< webelement> () {@Overridepublic webelement apply (Webdriver D) {return d.findeleme        NT (by.id ("kw"));}   }    ); }/** * Custom Explicit wait * @param DrIver * @param by * @return */private static webelement webelementexplicitwait (Webdriver Driver,by by) {return (new webdrive  Rwait (Driver, ten)). Until (new expectedcondition< webelement> () {@Overridepublic webelement apply (webdriver D)        {return d.findelement (by);} }    );} /** * Determine if an element exists * @param driver * @param by * @return */private Static Boolean ispresentelement (Webdriver Driver,by by) {TR y {driver.findelement (by); return true;} catch (Nosuchelementexception e) {return false;} catch (Exception e) {return false;}}}

We can according to our own needs to encapsulate some elements of the wait, the method of element lookup, such as the above to determine whether the existence of elements, if not found to throw an exception, sometimes it is possible to throw the wrong element, sometimes it may be the wrong elements are not visible errors, can be used excpetion to capture.

In the timeout interface introduced the first three kinds of time to wait for the method, here is not carefully studied.

And then explicitly wait for explicitwait, according to our needs can be interpreted in a time frame whether this element can be located, this role is relatively large, like the above method is only to the overall delay, in the end can be positioned to the element also do not know. But we can also according to our own project needs to encapsulate these methods, so that they better serve us.

Webdriver api--delay operation and element wait

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.