[Selenium webdriver Java] using custom conditions to synchronize tests

Source: Internet
Author: User

Selenium Webdriver can be combined with the Expectedcondition class to define their own desired conditions

To create a new expectedcondition interface, you must implement the Apply method

Wait for the element to appear

1  Public voidtestwithimplicitwait () {2System.setproperty ("Webdriver.chrome.driver", "Chromedriver.exe");3Webdriver Driver =Newchromedriver ();4Driver.get ("http://map.baidu.com");5 6         //Click to expand the current city7Webelement curcity = driver.findelement (By.id ("Curcity"));8 Curcity.click ();9 Ten         //set wait time 10 seconds Onewebdriverwait wait =NewWebdriverwait (driver,10); A     //To create a new expecctedcondition interface, you must implement the Apply method -Webelement message =Wait.until ( -             NewExpectedcondition<webelement>(){ the                  Publicwebelement Apply (webdriver d) { -                     returnD.findelement (By.id ("Selcityhotcityid")); -                 } -             } +             ); -  + driver.quit (); A}
Sample Code

Wait for element property value to change

property values for an element may change after an operation based on certain events. For example, a text box that cannot be entered becomes an input state. The custom wait can be implemented on the attributes of the element.

In this example, the Expectedcondition class waits to return a Boolean value

1 (new webdriverwait (driver). Util (new expectedcondition<boolean>() { 2      Public Boolean Apply (webdriver d) {3       return d.findelement (by.id ("username")). 4 getattribute ("ReadOnly"). Contains ("true");   5     }(6 }));

Wait for the element to become visible

The developer hides or displays an element after a series of actions. The specified element initially already exists in the DOM, but is hidden, and becomes visible when the user passes the specified action. Then the custom expectation criteria should be as follows:

1 (new webdriverwait (driver). Util (new expectedcondition<boolean>() { 2      Public Boolean Apply (webdriver d) {3       return d.findelement (by.id ("xxx")). isdisplayed ();   4     }(5 }));

Waiting for DOM events

A custom wait can be done by executing a piece of JavaScript code and checking the return value

1 (new webdriverwait (driver,10)). Until (new expectedcondition<boolean>() {  2 public Boolean apply (webdriver d) {3 javascriptexecutor js = ( Javascriptexecutor) D; 4 return (Boolean) Js.executescript ("return jquery.active = = 0"); 5 }6 });

[Selenium webdriver Java] using custom conditions to synchronize tests

Related Article

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.