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();
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:
For instance:
Hope I helped.
If you just started to use selenium, is a legit question. I always suggest to have a look to the official doc.
Example:
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.
Example:
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
you are saying to wait, whenever you try lo locate an element, the presence (remember, it's like presenceOfElementLocated) until 30 seconds.
With
you are setting to 30 seconds the time that a webpage needs to be loaded.
With:
You are saying that you want to wait the visibilityOfElementLocated, of the web element with id:"id", until 30 seconds.
Finally:
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:
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:
Here you can find a detailed discussion in pageLoadTimeout in Selenium not working
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.
Your code trial is just perfect to wait for the visibility of an element as in:
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).