Dongminator
Dongminator

Reputation: 825

Selenium ExpectedConditions parameter

While writing Selenium tests, I noticed in the ExpectedConditions class, some methods use only By as parameter, some use only WebElement as parameter, and some methods have overloaded methods that support both parameters.

So my questions are:

  1. Why dont all methods use By, WebElement or both?
  2. Is there any benefit of using By over WebElement or the other way around?
  3. If I use Page Object Model, does it mean I need to maintain both By instance and WebElement instance for the same element on the page?

Source: https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Upvotes: 4

Views: 978

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

Actually it's depends on requirements

  • presenceOfElementLocated(final By locator) :

    An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

Here you can only use By locator because if you have already found WebElement then there is nothing make sense to use this ExpectedConditions by passing WebElement.

  • visibilityOf(final WebElement element) :

    An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Means if you have WebElement and you already know this element present on the DOM and would not be stale means this wouldn't throw StaleElementException due to changing of the page at run time but you doesn't know element is visible or not then use this ExpectedConditions by passing WebElement.

  • elementToBeClickable(final By locator) and elementToBeClickable(final WebElement element) :

    An expectation for checking an element is visible and enabled such that you can click it.

Means if you have WebElement and you already know this element present on the DOM and would not be stale means this wouldn't throw StaleElementException due to changing of the page at run time but you doesn't know element is visible and enable or not then use elementToBeClickable(final WebElement element).

But some time due to ajax or other reason if page would be change during checking elementToBeClickable(final WebElement element) ExpectedConditions then it couldn't meet with condition due to StaleElementException because in elementToBeClickable(final WebElement element) condition selenium only check for visibility and enable condition for already found WebElement while using elementToBeClickable(final By locator) ExpectedConditions selenium goes to find element before checking that element is visible and enable to overcome the chance of failure due to StaleElementException.

So this is the benefits of using By over WebElement Here.

Upvotes: 0

Related Questions