Selenium2+java-based UI Automation (8)-Explicit wait and implicit wait

Source: Internet
Author: User
Tags xpath

One, implicit waiting
Package com.automation.waits;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.chrome.ChromeDriver;/** * Class Description: Implicit wait * <br/> * @version 1.0 * November 22, 2016 8:56:14 * *PublicClassimplictwait {Publicstatic void Main (String[] (args) {1. Open the browser;System.setproperty ("Webdriver.chrome.driver","D:\\workspace\\a_test\\resource\\chromedriver.exe");Webdriver Driver = newChromedriver ();2. Browser maximization driver.manage (). window (). Maximize ();/** * 3. Set the global implicit wait time; * <br/> Use the Implicitlywait method to set the wait time for the lookup element; * <br/> when the Findelement method is called, the element is not immediately found, Will wait for the set implicit wait time to go down; * <br/> if the set wait times are exceeded and no elements have been found, the nosuchelementexception exception is thrown; */driver.manage (). Timeouts (). Implicitlywait (10,Timeunit.SECONDS); //Set implicit wait time of 10 seconds;//3. Open Sogou homepage; driver.  Get ("http://www.sogou.com/");  try {//4. Locate Sogou Home: Input Box object, search button;webelement inputBox = driver.findelement (by.id ("Query"));  Webelement SearchButton = driver.findelement (by.id ("STB"));  5. Enter the element in the input box and click the Search button. Inputbox.sendkeys ("The input box element was found. "); Searchbutton.click ();} catch (nosuchelementexception e) { //throws an Nosuchelementexception exception if the element is not found. E.printstacktrace ();                  }}}

Implicit wait can be set, but there is one drawback:
Cons: If we set the implicit wait time in the code, when we use the Driver.findelement (by.*) method to find the elements of the page, if there is no first time to find the element, the program will wait. For example, setting an implicit wait time of 10 seconds, an element does not appear in the beginning, but at the time of the first 5 seconds, the program will still wait 10 seconds before execution down;
Therefore, it is recommended to use display waiting.

Second, explicit waiting

Show wait more time than implicit wait, more economical test execution;
Advantage:
1. There are many methods of waiting, all of which are in the Expectedconditions class, and can be judged by different waits.
2. Wait for the flexibility, wait for 10 seconds, and the 5th second element appears, then it will be executed immediately, and will not continue to wait, only more than 10 seconds to throw nosuchelementexception exception;

Expectedconditions class comes with a wait method:

pasted image 816x232 3.37 KB

The most common is the third one, judging whether the element exists in the page: presenceofelementlocated (by locator)

Package com.automation.waits;Import Org.openqa.selenium.By;Import org.openqa.selenium.NoSuchElementException;Import Org.openqa.selenium.WebDriver;Import Org.openqa.selenium.chrome.ChromeDriver;Import org.openqa.selenium.support.ui.ExpectedConditions;Import org.openqa.selenium.support.ui.WebDriverWait;/** * Class Description: Display wait * <br/> * @version 1.0 * November 22, 2016 9:38:12 * *PublicClassexplictwait {Publicstatic void Main (String[] (args) {1. Open the browser;System.setproperty ("Webdriver.chrome.driver","D:\\workspace\\a_test\\resource\\chromedriver.exe");Webdriver Driver = newChromedriver ();2. Browser maximization driver.manage (). window (). Maximize ();3. Open the Sogou homepage; driver.Get"http://www.baidu.com/");/* * 4. Set display wait Duration: 10 seconds;webdriverwait wait = newWebdriverwait (Driver,10);5. Show wait: Whether the title appears:try {wait.until (Expectedconditions.titlecontains ("Baidu a bit, you will know");System.out.println"Baidu Homepage title appeared: Baidu a bit, you Know");}catch (Nosuchelementexception e) {If the caption is not found, the nosuchelementexception exception is thrown. E.printstacktrace ();System.out.println"Baidu Homepage title did not find");}6. Display wait: Whether the search box appears-------most commonly used display waits;try {wait.until (Expectedconditions.presenceofelementlocated (By.id ("Su")));System.out.println"Baidu homepage Search input box appeared");}catch (Nosuchelementexception e) {//If the caption is not found, the nosuchelementexception exception is thrown. E.printstacktrace (); System.out. println ("Baidu homepage input box not found");} //7. Display wait: Whether the page Search button can be clicked;try {wait.until ("expectedconditions.elementtobeclickable (by.id (" kw ")) ); System.out. println ("Baidu homepage Search button can be clicked");} catch (nosuchelementexception e) { //throws a Nosuchelementexception exception if the caption is not found. E.printstacktrace (); System.out. println ("Baidu homepage Search button not found");               }}} 
Three, custom explicit wait
Package com.automation.waits;Import Org.openqa.selenium.By;Import Org.openqa.selenium.WebDriver;Import org.openqa.selenium.WebElement;Import Org.openqa.selenium.chrome.ChromeDriver;Import org.openqa.selenium.support.ui.ExpectedCondition;Import org.openqa.selenium.support.ui.WebDriverWait;/** * Class Description: Custom display wait * <br/> *@version 1.0 * November 22, 2016 10:00:12 * *PublicClasscustomexplictwait {PublicStaticvoidMain(string[] args) {1. Open the browser; System.setproperty ("Webdriver.chrome.driver","D:\\workspace\\a_test\\resource\\chromedriver.exe"); Webdriver Driver =New Chromedriver ();2. Browser maximization driver.manage (). window (). Maximize ();3. Open the Sogou homepage; Driver.get ("http://www.baidu.com/");/* * 4. Customize the display wait to find an element in the wait code; */webelement Textinputbox = (New Webdriverwait (Driver,). Until (New Expectedcondition<webelement> () {@OverridePublic webelementApply(Webdriver driver) {return driver.findelement (By.xpath ("//*[@type = ' text ']");}); Textinputbox.sendkeys ("Custom explicit wait 1");  /* * 5. Custom display wait, get page element//a[text () = ' about Sogou '] text value */string Text = (new webdriverwait (Driver, Ten)). Until (new Expectedcondition<string> () {@Override publicString apply(Webdriver driver) {return Driver.findelement (By.xpath ("//a[text () = ' about Sogou ')"). GetText ();}); System.out.println (text); //Print text value}}               

To customize an explicit wait, be aware that:

pasted image 915x200 9.17 KB

The expected value is the webelement type, then the return must also be the webelement type;

我们专注于持续集成,更多原创请关注:www.hordehome.com

Selenium2+java-based UI Automation (8)-Explicit wait and implicit 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.