Foreword
In automated testing, we often encounter when writing scripts during the operation of an element, you need to wait for page loads, in order to operate the elements, otherwise it will error, suggesting abnormal page elements do not exist, we need to wait for the elements loaded after,
in order to continue operating, while waiting for Selenium provides a corresponding method for us to determine whether the element exists.
An example will now, waiting for the operator to do explain each element
actual case
Scene: Click [create div] button, after three seconds, the page a green div block will appear, displaying the words "I am a div, I appeared, ha ha!", We need the code to judge this div exists, then high bright, and display the text properly.
Html code tested as follows:
var pElement = document.createElement('p'); document.body.appendChild (DivElement); divElement.appendChild (pElement); } </ Script> <body> <the Button onclick = "wait_show ()" > create a div </ the Button> </ body> </ HTML>
1, forced to wait
Forced to wait, waiting is hard, use Thread.sleep (int sleeptime), using the method currently executing process will pause for a time (you set the pause time). The drawbacks is that you can not determine how long the elements can be loaded completely, if two seconds to load elements out, you took 30 seconds, resulting in a waste of script execution time.
Specific examples of code is as follows:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestWaitDemo { WebDriver driver; @BeforeClass public void beforeClass(){ System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); driver = new ChromeDriver(); } @Test public void testByThread() { //打开测试页面 driver.get("file:///C:/Users/Administrator/Desktop/waitDemo.html"); driver.manage().window().maximize(); driver.findElement(By.id("wait")).click(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //获得div块级元素 WebElement element = driver.findElement(By.id("green_box")); // Get the element css style background-color property value String cssValue = element.getCssValue ( "background-color" ); // output attribute value System.out.println ( "cssValue:" + cssValue); } @AfterClass public void AfterClass () { driver.quit (); } }
2, page wait
Sometimes we open a web page, the page itself loading speed is relatively slow, can only wait for the page is fully loaded in order to perform an operation, it can be used pageLoadTimeout (pageLoadTime, TimeUnit.SECONDS) of this method, such as within the set time, web has not yet fully loaded would be an error, the rest of the time will not wait.
Specific examples of code is as follows:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; public class TestWaitDemo { WebDriver driver; @BeforeClass public void beforeClass(){ System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); Driver = new new ChromeDriver (); } @Test public void testByPageLoad () { // Open the test page driver.get ( " File: /// C: /Users/Administrator/Desktop/waitDemo.html " ); // set the wait time is 3 seconds, three seconds if the page does not come out fully loaded, it will error, if less than 3 seconds to load up all the rest of the time will not wait to continue to the next step driver.manage (). timeouts () .pageLoadTimeout (. 3 , TimeUnit.SECONDS); .. driver.manage () window () Maximize (); } @AfterClass public void AfterClass () { driver.quit (); } }
3, implicit wait
WebDriver provides three implicit wait method:
- implicitlyWait
Timeout when identifying objects. After this time, then if the object is not found NoSuchElement will throw an exception.
- setScriptTimeout
Timeout asynchronous script. WebDriver script can be executed asynchronously, this is to set the asynchronous execution of the script, the script returns the result of the timeout.
- pageLoadTimeout
Page timeout when loading. Because WebDriver will wait for page load before proceeding back, so if the page exceeds the set time is still not loaded, then WebDriver will throw an exception.
Implicit wait (Implicit), the method implicitlyWait (long time, TimeUnit.SECONDS), i.e. global settings for the entire driver have a role, such as within the set time, the completion of a specific element is not loaded, an exception is thrown if the loading element completed, the remaining time will not wait.
Specific examples of code is as follows:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; public class TestWaitDemo { WebDriver driver; @BeforeClass public void beforeClass(){ System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); Driver= New new ChromeDriver (); } @Test public void testByImplicitlyWait () { // Open the test page driver.get ( "File: /// C: /Users/Administrator/Desktop/waitDemo.html" ); // set the waiting time 3 seconds, three seconds if it does not load the element, will be given, if the loading element out of less than 3 seconds, and the remaining wait time will not continue to the next step driver.manage (). timeouts (). implicitlyWait ( . 3 , TimeUnit.SECONDS); driver.manage () window () Maximize ();.. driver.findElement (By.id ( "the wait" ).) the Click (); // get div block elements WebElement element = driver .findElement (By.id ( "green_box" )); //Get background-color attribute value of the element css style String cssValue = element.getCssValue ( "background-color" ); // output attribute value System.out.println ( "cssValue:" + cssValue); } @AfterClass public void AfterClass () { driver.quit (); } }
4, waiting for an explicit
Display of waiting is clear to wait until an element or an element clickable wait until other conditions, will continue to perform follow-up operations, can not wait, we have been waiting for, never unless within the stipulated time of find, then thrown up
method one:
Specific code as follows:
package com.brower.demo; 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; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestWaitDemo { WebDriver driver; @BeforeClass public voidBeforeClass () { System.setProperty ( "webdriver.chrome.driver", "Driver / chromedriver.exe" ); Driver = new new ChromeDriver (); } @Test public void testByShowWaiting () { // Open the test page driver.get ( "File: /// C: /Users/Administrator/Desktop/waitDemo.html" ); driver.manage () window () the Maximize ();.. driver.findElement (By.id ( "the wait" )) the Click. (); / ** * wait time is 3 seconds, WebDriverWait default would be called once every 500ms ExpectedCondition until you locate the div, if within 3 seconds div show up, then continue to the next, * If more than three seconds does not show up, then it until () will throw an exception org.openqa.selenium.TimeoutExceptionn * / WebDriverWait the wait = new newWebDriverWait (Driver, 3 ); // element exists, and if it exceeds the set time does not detect an exception is thrown. wait.until ( new new ExpectedCondition <WebElement> () { @Override public WebElement Apply (the WebDriver Driver) {
// override method return driver.findElement (By.id ( "green_box" )); } }); // get div block elements WebElement = driver.findElement element (By.id ( "green_box" )); // Get the style element css background-color property value String cssValue = element.getCssValue ( "background-color" ); // output property value System.out.println ( "cssValue:" + cssValue); } @AfterClass public void AfterClass () { driver.quit (); } }
Method Two
Sample code is as follows:
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.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestWaitDemo { WebDriver driver; @BeforeClass public void BeforeClass () { System.setProperty ( "webdriver.chrome.driver", "Driver / chromedriver.exe" ); Driver = new new ChromeDriver (); } @Test public void testByShowWaiting () { // Open the test page driver.get ( "File: /// C: /Users/Administrator/Desktop/waitDemo.html" ); driver.manage () window () the Maximize ();.. driver.findElement (By.id ( "the wait" )). the Click (); / ** * wait time is 3 seconds, WebDriverWait default would be called once every 500ms ExpectedCondition until you locate the div, if within 3 seconds div show up, then continue to the next, * If not displayed more than three seconds, then an until then () will throw an exception org.openqa.selenium.TimeoutExceptionn * / WebDriverWait the wait = new new WebDriverWait (Driver, 3 ); wait.until (ExpectedConditions.presenceOfElementLocated (By.id ( " green_box " ))); // get div block elements WebElement = driver.findElement element (By.id (" green_box " )); // Get the style element css background-color property value String cssValue = element.getCssValue ( "Color-background" ); // output attribute value System.out.println ( "cssValue:" + cssValue); } @AfterClass public void AfterClass () { driver.quit (); } }
Waiting to use explicit class comes ExpectedConditions method, explicit determination waiting judgment condition used in the following table:
Conditions of the method name waiting | description |
elementToBeClickable(By locator) | Whether page elements on the page is available and can be clicked |
elementToBeSelected (WebElement element) | Page elements in the selected state |
presenceOfElementLocated(By locator) | Page elements exist in the page |
textToBePresentInElement(By locator) | In the page elements contain specific text |
textToBePresentInElementValue(By locator, java.lang.String text) | Page element values |
titleContains(java.lang.String title) | Title (title) |
Explicit waiting is often used in conjunction with the following three methods for determining elements
- Whether isEnable () checking is enabled elements
- Whether isSelected () checking element is selected
- isDisplay () to check whether the element is visible
operation result