WebDriverWait study of Selenium
When running automation, you often encounter the problem that it takes time for the elements of the page to load, and this time is uncertain, but your execution has been going down step by step, if the actual page waiting time is too long so that a dom element has not come out, but your code directly uses this WebElement, then it will throw a NullPointer exception.
In order to solve this problem, especially RemoteWebDriver (where there are more uncertainties), we need to use the very advanced functions of WebDriverWait.
In short, the WebDriverWait is to wait until a certain condition is met and then move on.
Paste the sample code:
WebDriverWait wait = new WebDriverWait (getWebDriver (), 3); wait.until (new ExpectedCondition () {public Boolean apply (WebDriver webDriver) {return! justification.getAttribute ("class"). Contains ("x-form-invalid-field") })
What does this code do? it means that by default, webDriver waits for up to 3 seconds until the style of the WebElement element called justification on the page does not contain x-form-invalid-field.
Analysis:
What does this mean? Very simple, because every field on our page is validated by js form, if the element on this page is typed incorrectly, there will be a red box to prompt the user to enter illegally, and the style of this red box is x-form-invalid-field, so our role is to wait for the red box to disappear, and then proceed to the next step.
As shown below:
Because the WebElement of this justification is mandatory for the user, if the user does not enter it, there is a red box:
Then the style of the red box contains x-form-invalid-field, as shown below:
So it's pretty clear now that our goal is to make the red box disappear when we run automation testcase.