FREE MEANS 100% FREE IT’S TOTALLY FREE

Lists of ExpectedConditions in Selenium with explanation

What Is ExpectedConditions In Selenium?

AJAX (Offbeat JavaScript and XML) is utilized for the lion’s share of web items, which suggests that components on the site are stacked at diverse times. When utilizing the Selenium framework for computerization testing, this may cause timing issues. What happens on the off chance that you run a test on a WebElement that isn’t within the DOM? The ElementNotVisibleException will be tossed by the findElement work.

If you have read the article on Wait in Selenium, then you might know the keyword ExpectedConditions. However, if you haven’t read the article, it would help to do so before learning about Expectedconditions in Selenium. This article will explain lists of Expectedconditions commonly used in Selenium with explanation.

Wait With ExpectedConditions in Selenium

When a WebElement is not immediately accessible, the Selenium WebDriver polls the DOM for a given period of time, which is called Implicit Wait. Implicit Wait is available for the entire life of the WebDriver object once it is set.

On the contrary, Explicit Waits are used to halt the execution until the time a particular condition is meet or the maximum time has elapsed. Unlike implicit wait, explicit waits are applied for a particular instance only. (Explicit Wait + ExpectedConditions)

driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
By elem_dynamic = By.id("dynamic-element");
wait.until(ExpectedConditions.presenceOfElementLocated(elem_dynamic));

The Selenium WebDriver waits for a maximum of 10 seconds until the WebElement with ID ‘dynamic part’ is found in the above snippet demonstrating expected conditions in Selenium Java. The Selenium WebDriver calls the ExpectedCondition every 500 milliseconds until it succeeds.

The ExpectedCondition returns true if the WebElement is available within 10 seconds (the maximum wait time), and execution moves on to the next stage. If the element is not present in the DOM after the maximum period of 10 seconds has expired, a TimeoutException is thrown.

Types of Expected Conditions In Selenium Java with their lists

In Selenium, there are three main categories of ExpectedConditions:

  1. ExpectedCondition <WebElement>

    1. presenceOfElementLocated 

      public static ExpectedCondition<WebElement> presenceOfElementLocated​(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.
      Parameters:
      locator – used to find the element
      Returns:
      the WebElement once it is located
    2. visibilityOfElementLocated

      public static ExpectedCondition<WebElement> visibilityOfElementLocated​(By locator)
      An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
      Parameters:
      locator – used to find the element
      Returns:
      the WebElement once it is located and visible
    3. visibilityOfAllElementsLocatedBy

      public static ExpectedCondition<java.util.List<WebElement>> visibilityOfAllElementsLocatedBy​(By locator)
      An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.
      Parameters:
      locator – used to find the element
      Returns:
      the list of WebElements once they are located
    4. visibilityOfAllElements

      public static ExpectedCondition<java.util.List<WebElement>> visibilityOfAllElements​(WebElement... elements)
      An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.
      Parameters:
      elements – list of WebElements
      Returns:
      the list of WebElements once they are located
    5. visibilityOf

      public static ExpectedCondition<WebElement> visibilityOf​(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.
      Parameters:
      element – the WebElement
      Returns:
      the (same) WebElement once it is visible
    6. presenceOfAllElementsLocatedBy

      public static ExpectedCondition<java.util.List<WebElement>> presenceOfAllElementsLocatedBy​(By locator)
      An expectation for checking that there is at least one elementpresent on a web page.
      Parameters:
      locator – used to find the element
      Returns:
      the list of WebElements once they are located
    7. elementToBeClickable

      public static ExpectedCondition<WebElement> elementToBeClickable​(By locator)
      An expectation for checking an element is visible and enabled such that you can click it.
      Parameters:
      locator – used to find the element
      Returns:
      the WebElement once it is located and clickable (visible and enabled)
    8. numberOfElementsToBeMoreThan

      public static ExpectedCondition<java.util.List<WebElement>> numberOfElementsToBeMoreThan​(By locator, java.lang.Integer number)
      An expectation for checking number of WebElements with given locator being more than defined number
      Parameters:
      locator – used to find the element
      number – used to define minimum number of elements
      Returns:
      Boolean true when size of elements list is more than defined
    9. numberOfElementsToBeLessThan

      public static ExpectedCondition<java.util.List<WebElement>> numberOfElementsToBeLessThan​(By locator, java.lang.Integer number)
      An expectation for checking number of WebElements with given locator being less than defined number
      Parameters:
      locator – used to find the element
      number – used to define maximum number of elements
      Returns:
      Boolean true when size of elements list is less than defined
    10. numberOfElementsToBe

      public static ExpectedCondition<java.util.List<WebElement>> numberOfElementsToBe​(By locator, java.lang.Integer number)
      An expectation for checking number of WebElements with given locator
      Parameters:
      locator – used to find the element
      number – used to define number of elements
      Returns:
      Boolean true when size of elements list is equal to defined
    11. visibilityOfNestedElementsLocatedBy

      public static ExpectedCondition<java.util.List<WebElement>> visibilityOfNestedElementsLocatedBy​(By parent, By childLocator)
      An expectation for checking child WebElement as a part of parent element to be visible
      Parameters:
      parent – used to check parent element. For example table with locator By.id(“fish”)
      childLocator – used to find the ultimate child element.
      Returns:
      visible nested elemen
    12. visibilityOfNestedElementsLocatedBy

      public static ExpectedCondition<java.util.List<WebElement>> visibilityOfNestedElementsLocatedBy​(WebElement element, By childLocator)
      An expectation for checking child WebElement as a part of parent element to be visible
      Parameters:
      element – used as parent element. For example table with locator By.xpath(“//table”)
      childLocator – used to find child element. For example td By.xpath(“./tr/td”)
      Returns:
      visible subelement
    13. presenceOfNestedElementLocatedBy

      public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy​(By locator, By childLocator)
      An expectation for checking child WebElement as a part of parent element to present
      Parameters:
      locator – used to check parent element. For example table with locator By.xpath(“//table”)
      childLocator – used to find child element. For example td By.xpath(“./tr/td”)
      Returns:
      subelement
    14. presenceOfNestedElementLocatedBy

      public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy​(WebElement element, By childLocator)
      An expectation for checking child WebElement as a part of parent element to be present
      Parameters:
      element – used as parent element
      childLocator – used to find child element. For example td By.xpath(“./tr/td”)
      Returns:
      subelement
    15. presenceOfNestedElementsLocatedBy

      public static ExpectedCondition<java.util.List<WebElement>> presenceOfNestedElementsLocatedBy​(By parent, By childLocator)
      An expectation for checking child WebElement as a part of parent element to present
      Parameters:
      parent – used to check parent element. For example table with locator By.xpath(“//table”)
      childLocator – used to find child element. For example td By.xpath(“./tr/td”)
      Returns:
      subelement
  2. ExpectedCondition <WebDriver> 
    1. frameToBeAvailableAndSwitchToIt
      public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt​(By locator)
      An expectation for checking whether the given frame is available to switch to.

      If the frame is available it switches the given driver to the specified frame.

      Parameters:
      frameLocator – used to find the frame (id or name)
      Returns:
      WebDriver instance after frame has been switched
  3. ExpectedCondition <Boolean>
    1. titleIs
      public static ExpectedCondition<java.lang.Boolean> titleIs​(java.lang.String title)
      An expectation for checking the title of a page.
      Parameters:
      title – the expected title, which must be an exact match
      Returns:
      true when the title matches, false otherwise
    2. titleContains
      public static ExpectedCondition<java.lang.Boolean> titleContains​(java.lang.String title)
      An expectation for checking that the title contains a case-sensitive substring
      Parameters:
      title – the fragment of title expected
      Returns:
      true when the title matches, false otherwise
    3. urlToBe

      public static ExpectedCondition<java.lang.Boolean> urlToBe​(java.lang.String url)
      An expectation for the URL of the current page to be a specific url.
      Parameters:
      url – the url that the page should be on
      Returns:
      true when the URL is what it should be
    4. urlContains

      public static ExpectedCondition<java.lang.Boolean> urlContains​(java.lang.String fraction)
      An expectation for the URL of the current page to contain specific text.
      Parameters:
      fraction – the fraction of the url that the page should be on
      Returns:
      true when the URL contains the text
    5. urlMatches

      public static ExpectedCondition<java.lang.Boolean> urlMatches​(java.lang.String regex)
      Expectation for the URL to match a specific regular expression
      Parameters:
      regex – the regular expression that the URL should match
      Returns:
      true if the URL matches the specified regular expression
    6. textToBePresentInElement

      public static ExpectedCondition<java.lang.Boolean> textToBePresentInElement​(WebElement element, java.lang.String text)
      An expectation for checking if the given text is present in the specified element.
      Parameters:
      element – the WebElement
      text – to be present in the element
      Returns:
      true once the element contains the given text
    7. textToBePresentInElementValue
      public static ExpectedCondition<java.lang.Boolean> textToBePresentInElementValue​(WebElement element, java.lang.String text)
      An expectation for checking if the given text is present in the specified elements value attribute.
      Parameters:
      element – the WebElement
      text – to be present in the element’s value attribute
      Returns:
      true once the element’s value attribute contains the given text
    8. invisibilityOfElementLocated
      public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated​(By locator)
      An expectation for checking that an element is either invisible or not present on the DOM.
      Parameters:
      locator – used to find the element
      Returns:
      true if the element is not displayed or the element doesn’t exist or stale element
    9. invisibilityOfElementWithText
      public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementWithText​(By locator, java.lang.String text)
      An expectation for checking that an element with text is either invisible or not present on the DOM.
      Parameters:
      locator – used to find the element
      text – of the element
      Returns:
      true if no such element, stale element or displayed text not equal that provided
    10. stalenessOf
      public static ExpectedCondition<java.lang.Boolean> stalenessOf​(WebElement element)
      Wait until an element is no longer attached to the DOM.
      Parameters:
      element – The element to wait for.
      Returns:
      false if the element is still attached to the DOM, true otherwise.
    11. elementToBeSelected
      public static ExpectedCondition<java.lang.Boolean> elementToBeSelected​(WebElement element)
      An expectation for checking if the given element is selected.
      Parameters:
      element – WebElement to be selected
      Returns:
      true once the element is selected
    12. elementSelectionStateToBe
      public static ExpectedCondition<java.lang.Boolean> elementSelectionStateToBe​(WebElement element, boolean selected)
      An expectation for checking if the given element is selected.
      Parameters:
      element – WebElement to be selected
      selected – boolean state of the selection state of the element
      Returns:
      true once the element’s selection stated is that of selected
    13. elementToBeSelected
    14. elementSelectionStateToBe
    15. numberOfWindowsToBe
    16. attributeToBe
      public static ExpectedCondition<java.lang.Boolean> attributeToBe​(By locator, java.lang.String attribute, java.lang.String value)
      An expectation for checking WebElement with given locator has attribute with a specific value
      Parameters:
      locator – used to find the element
      attribute – used to define css or html attribute
      value – used as expected attribute value
      Returns:
      Boolean true when element has css or html attribute with the value
    17. textToBe
      public static ExpectedCondition<java.lang.Boolean> textToBe​(By locator, java.lang.String value)
      An expectation for checking WebElement with given locator has specific text
      Parameters:
      locator – used to find the element
      value – used as expected text
      Returns:
      Boolean true when element has text value equal to @value
    18. textMatches
      public static ExpectedCondition<java.lang.Boolean> textMatches​(By locator, java.util.regex.Pattern pattern)
      An expectation for checking WebElement with given locator has text with a value as a part of it
      Parameters:
      locator – used to find the element
      pattern – used as expected text matcher pattern
      Returns:
      Boolean true when element has text value containing @value
    19. domPropertyToBe
      public static ExpectedCondition<java.lang.Boolean> domPropertyToBe​(WebElement element, java.lang.String property, java.lang.String value)
      An expectation for checking given WebElement has DOM property with a specific value
      Parameters:
      element – used to check its parameters
      property – property name
      value – used as expected property value
      Returns:
      Boolean true when element has DOM property with the value
    20. domAttributeToBe
      public static ExpectedCondition<java.lang.Boolean> domAttributeToBe​(WebElement element, java.lang.String attribute, java.lang.String value)
      An expectation for checking given WebElement has DOM attribute with a specific value
      Parameters:
      element – used to check its parameters
      attribute – attribute name
      value – used as expected attribute value
      Returns:
      Boolean true when element has DOM attribute with the value
    21. attributeToBe
      public static ExpectedCondition<java.lang.Boolean> attributeToBe​(WebElement element, java.lang.String attribute, java.lang.String value)
      An expectation for checking given WebElement has attribute with a specific value
      Parameters:
      element – used to check its parameters
      attribute – used to define css or html attribute
      value – used as expected attribute value
      Returns:
      Boolean true when element has css or html attribute with the value
    22. attributeContains

      public static ExpectedCondition<java.lang.Boolean> attributeContains​(WebElement element, java.lang.String attribute, java.lang.String value)
      An expectation for checking WebElement with given locator has attribute which contains specific value
      Parameters:
      element – used to check its parameters
      attribute – used to define css or html attribute
      value – used as expected attribute value
      Returns:
      Boolean true when element has css or html attribute which contains the value
    23. attributeContains
      public static ExpectedCondition<java.lang.Boolean> attributeContains​(By locator, java.lang.String attribute, java.lang.String value)
      An expectation for checking WebElement with given locator has attribute which contains specific value
      Parameters:
      locator – used to define WebElement to check its parameters
      attribute – used to define css or html attribute
      value – used as expected attribute value
      Returns:
      Boolean true when element has css or html attribute which contains the value
    24. attributeToBeNotEmpty
      public static ExpectedCondition<java.lang.Boolean> attributeToBeNotEmpty​(WebElement element, java.lang.String attribute)
      An expectation for checking WebElement any non empty value for given attribute
      Parameters:
      element – used to check its parameters
      attribute – used to define css or html attribute
      Returns:
      Boolean true when element has css or html attribute with non empty value
    25. invisibilityOfAllElements
      public static ExpectedCondition<java.lang.Boolean> invisibilityOfAllElements​(WebElement... elements)
      An expectation for checking all elements from given list to be invisible
      Parameters:
      elements – used to check their invisibility
      Returns:
      Boolean true when all elements are not visible anymore
    26. or
      public static ExpectedCondition<java.lang.Boolean> or​(ExpectedCondition<?>... conditions)
      An expectation with the logical or condition of the given list of conditions. Each condition is checked until at least one of them returns true or not null.
      Parameters:
      conditions – ExpectedCondition is a list of alternative conditions
      Returns:
      true once one of conditions is satisfied
    27. and
      public static ExpectedCondition<java.lang.Boolean> and​(ExpectedCondition<?>... conditions)
      An expectation with the logical and condition of the given list of conditions. Each condition is checked until all of them return true or not null
      Parameters:
      conditions – ExpectedCondition is a list of alternative conditions
      Returns:
      true once all conditions are satisfied
    28. javaScriptThrowsNoExceptions
      public static ExpectedCondition<java.lang.Boolean> javaScriptThrowsNoExceptions​(java.lang.String javaScript)
      An expectation to check if js executable. Useful when you know that there should be a Javascript value or something at the stage.
      Parameters:
      javaScript – used as executable script
      Returns:
      true once javaScript executed without errors
      example: Example
  4. ExpectedCondition <Alert>
    1. alertIsPresen – public static ExpectedCondition<Alert> alertIsPresent();
  5. ExpectedCondition<java.lang.Object>
    1. jsReturnsValue
      public static ExpectedCondition<java.lang.Object> jsReturnsValue​(java.lang.String javaScript)
      An expectation for String value from javascript
      Parameters:
      javaScript – as executable js line
      Returns:
      object once javaScript executes without errors
      example: Example

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping