I currently have the following setup, but I'm not sure that my waits (Implicit and pageLoadTimeout) are working. Is this the proper implementation? By putting it in the @Before("@setup"), does it work for every Scenario or Step Definition run? Will the driver wait accordingly, everytime I call a @Given, @When..etc?
@Before("@setup")
public void setUp() {
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
Why is it necessary to assign a WebElement to the following wait , what does WebElement element receive? Is this the right implementation? -
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
boolean status = element.isDisplayed();
If you just started to use selenium, is a legit question. I always suggest to have a look to the official doc.
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Example:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
In this case, if the web element with id myDynamicElement is not present in the DOM when you try to locate it, you are saying to re-try until 10 seconds. The polling time depends on the webdriver that you are using. The thing that you have to know is that it will try to find the element for 10 seconds. Of course, if the element is located before the end of this time, the code will go on. Otherwise, an exception is thrown.
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
Example:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.
And, at the end, there is written: This example is also functionally equivalent to the first Implicit Waits example.
So, if you use presenceOfElementLocated as expected condition (for each elements that you try to locate), is exactly the same to use an implicit wait. But there isn't only this as condition. As you can see from ExpectedConditions you can specify other conditions (example: elementToBeClickable, stalenessOf and so on).
So, returning to your question:
With
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
you are saying to wait, whenever you try lo locate an element, the presence (remember, it's like presenceOfElementLocated) until 30 seconds.
With
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
you are setting to 30 seconds the time that a webpage needs to be loaded.
With:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
boolean status = element.isDisplayed();
You are saying that you want to wait the visibilityOfElementLocated, of the web element with id:"id", until 30 seconds.
Finally:
Why is it necessary to assign a WebElement to the following wait , what does WebElement element receive? Is this the right implementation?
What does the WebElement element receive? what else if not the webelement with id:"id"? Of course, if it's visible. Otherwise, an exception will be thrown.
implicitlyWait()
implicitlyWait()
is to tell the WebDriver instance i.e. driver to poll the HTML DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default wait configuration is set to 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Your code trial is just perfect as in:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Here you will find a detailed discussion in Using implicit wait in selenium
pageLoadTimeout()
pageLoadTimeout()
sets the timespan to wait for a page load to be completed before throwing an error.
Your code trial is just perfect as in:
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
Here you can find a detailed discussion in pageLoadTimeout in Selenium not working
Note : Try to avoid configuring
pageLoadTimeout()
until and unless the Test Specification explicitly mentions about the same.
Why WebDriverWait?
Modern browsers uses JavaScript, AJAX and React Native where elements within an webpage are loaded dynamically. So to wait for a specific condition to be met before proceeding for the next line of code Explicit Waits i.e. WebDriverWait is the way to proceed ahead.
Note : As per the official documentation of Explicit and Implicit Waits Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.
Your code trial is just perfect to wait for the visibility of an element as in:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
Here you can find a detailed discussion of Replace implicit wait with explicit wait (selenium webdriver & java)
Your specific questions
Why is it necessary to assign a WebElement to the following wait : WebDriverWait in conjunction with ExpectedConditions not only returns a WebElement but depending on the ExpectedConditions can return void, Boolean, List too.
What does WebElement element receive? : As per your code block where you have used ExpectedConditions as
visibilityOfElementLocated()
, the WebElement will be returned once the element is present on the DOM Tree of the webpage and is visible. Visibility means that the elements are not only displayed but also has a height and width that is greater than 0.Is this the right implementation? : Your implementation was near perfect but the last line of code i.e.
boolean status = element.isDisplayed();
is redundant asvisibilityOfElementLocated()
returns the element once the element is visible (i.e. the elements are not only displayed but also has a height and width that is greater than 0).
You only need to wait for page load in your case , and your Implementation is fine. @ Before is one of the Cucumber Hooks and it means your method gonna run before each Scenario. I suggest to use fluent wait instead of webdriverwait and here is why:
When using the FluentWait instance:
- You can check defined conditions (visibility of, invisibility of...)
- Ignore specific types of exception waiting such as NoSuchElementExceptions or StaleElementException while searching for an element on the page.
- You can specify polling and timeouts for each condition.
For instance:
// Waiting 30 seconds for an element to be present on the page, checking for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS).ignoring(NoSuchElementException.class);
Hope I helped.
来源:https://stackoverflow.com/questions/50518467/how-to-properly-configure-implicit-explicit-waits-and-pageloadtimeout-through