Class Condition

    • Constructor Summary

      Constructors 
      Constructor Description
      Condition​(java.lang.String name)  
      Condition​(java.lang.String name, boolean absentElementMatchesCondition)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String actualValue​(Driver driver, org.openqa.selenium.WebElement element)
      If element didn't match the condition, returns the actual value of element.
      static Condition and​(java.lang.String name, Condition... conditions)
      Check if element matches ALL given conditions.
      abstract boolean apply​(Driver driver, org.openqa.selenium.WebElement element)
      Check if given element matches this condition.
      boolean applyNull()  
      static Condition attribute​(java.lang.String attributeName)
      Check if element has given attribute (with any value)
      static Condition attribute​(java.lang.String attributeName, java.lang.String expectedAttributeValue)
      Sample: $("#mydiv").shouldHave(attribute("fileId", "12345"));
      static Condition be​(Condition delegate)
      Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))
      Condition because​(java.lang.String message)
      Should be used for explaining the reason of condition
      static Condition cssClass​(java.lang.String cssClass)
      Sample: $("input").shouldHave(cssClass("active"));
      static Condition cssValue​(java.lang.String propertyName, java.lang.String expectedValue)
      Checks if css property (style) applies for the element.
      static Condition exactText​(java.lang.String text)
      Sample: $("h1").shouldHave(exactText("Hello"))
      static Condition exactTextCaseSensitive​(java.lang.String text)
      Sample: $("h1").shouldHave(exactTextCaseSensitive("Hello"))
      static Condition exactValue​(java.lang.String value)
      Sample: $("#input").shouldHave(exactValue("John"));
      java.lang.String getName()  
      static Condition have​(Condition delegate)
      Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))
      static Condition id​(java.lang.String id)
      Sample: $("#input").shouldHave(id("myForm"))
      static Condition matchesText​(java.lang.String text)
      Sample: $(".error_message").waitWhile(matchesText("Exception"), 12000)
      static Condition matchText​(java.lang.String regex)
      Assert that given element's text matches given regular expression
      boolean missingElementSatisfiesCondition()  
      static Condition name​(java.lang.String name)
      Sample: $("#input").shouldHave(name("username"))
      static Condition not​(Condition condition)
      Negate given condition.
      static Condition or​(java.lang.String name, Condition... conditions)
      Check if element matches ANY of given conditions.
      static Condition selectedText​(java.lang.String expectedText)
      Checks for selected text on a given input web element
      static Condition text​(java.lang.String text)
      Sample: $("h1").shouldHave(text("Hello\s*John"))
      static Condition textCaseSensitive​(java.lang.String text)
      Sample: $("h1").shouldHave(textCaseSensitive("Hello\s*John"))
      java.lang.String toString()  
      static Condition type​(java.lang.String type)
      Sample: $("#input").shouldHave(type("checkbox"))
      static Condition value​(java.lang.String expectedValue)
      Assert that element has given "value" attribute as substring NB! Ignores difference in non-visible characters like spaces, non-breakable spaces, tabs, newlines etc.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • visible

        public static final Condition visible
        Checks if element is visible

        Sample: $("input").shouldBe(visible);

      • exist

        public static final Condition exist
        Check if element exist. It can be visible or hidden.

        Sample: $("input").should(exist);

      • hidden

        public static final Condition hidden
        Checks that element is not visible or does not exists.

        Opposite to appear

        Sample: $("input").shouldBe(hidden);

      • appear

        public static final Condition appear
        Synonym for visible - may be used for better readability

        Sample: $("#logoutLink").should(appear);

      • appears

        public static final Condition appears
        Synonym for visible - may be used for better readability

        $("#logoutLink").waitUntil(appears, 10000);

        Though the same can be done in a shorter way:

        waitFor(By.id("logoutLink");

      • disappears

        public static final Condition disappears
        Synonym for hidden - may be used for better readability:

        Sample: $("#loginLink").waitUntil(disappears, 9000);

      • disappear

        public static final Condition disappear
        Synonym for hidden - may be used for better readability:

        $("#loginLink").should(disappear);

      • readonly

        public static final Condition readonly
        Check if element has "readonly" attribute (with any value)

        Sample: $("input").shouldBe(readonly);

      • empty

        public static final Condition empty
        1) For input element, check that value is missing or empty

        Sample: $("#input").shouldBe(empty)

        2) For other elements, check that text is empty

        Sample: $("h2").shouldBe(empty)

      • focused

        public static final Condition focused
        Check if browser focus is currently in given element.
      • enabled

        public static final Condition enabled
        Checks that element is not disabled
        See Also:
        WebElement.isEnabled()
      • disabled

        public static final Condition disabled
        Checks that element is disabled
        See Also:
        WebElement.isEnabled()
      • selected

        public static final Condition selected
        Checks that element is selected
        See Also:
        WebElement.isSelected()
      • checked

        public static final Condition checked
        Checks that checkbox is checked
        See Also:
        WebElement.isSelected()
    • Constructor Detail

      • Condition

        public Condition​(java.lang.String name)
      • Condition

        public Condition​(java.lang.String name,
                         boolean absentElementMatchesCondition)
    • Method Detail

      • attribute

        public static Condition attribute​(java.lang.String attributeName)
        Check if element has given attribute (with any value)

        Sample: $("#mydiv").shouldHave(attribute("fileId"));

        Parameters:
        attributeName - name of attribute, not null
        Returns:
        true iff attribute exists
      • attribute

        public static Condition attribute​(java.lang.String attributeName,
                                          java.lang.String expectedAttributeValue)

        Sample: $("#mydiv").shouldHave(attribute("fileId", "12345"));

        Parameters:
        attributeName - name of attribute
        expectedAttributeValue - expected value of attribute
      • value

        public static Condition value​(java.lang.String expectedValue)
        Assert that element has given "value" attribute as substring NB! Ignores difference in non-visible characters like spaces, non-breakable spaces, tabs, newlines etc.

        Sample: $("input").shouldHave(value("12345 666 77"));

        Parameters:
        expectedValue - expected value of "value" attribute
      • exactValue

        public static Condition exactValue​(java.lang.String value)

        Sample: $("#input").shouldHave(exactValue("John"));

        Parameters:
        value - expected value of input field
      • name

        public static Condition name​(java.lang.String name)

        Sample: $("#input").shouldHave(name("username"))

        Parameters:
        name - expected name of input field
      • type

        public static Condition type​(java.lang.String type)

        Sample: $("#input").shouldHave(type("checkbox"))

        Parameters:
        type - expected type of input field
      • id

        public static Condition id​(java.lang.String id)

        Sample: $("#input").shouldHave(id("myForm"))

        Parameters:
        id - expected id of input field
      • matchesText

        public static Condition matchesText​(java.lang.String text)

        Sample: $(".error_message").waitWhile(matchesText("Exception"), 12000)

        See Also:
        matchText(String)
      • matchText

        public static Condition matchText​(java.lang.String regex)
        Assert that given element's text matches given regular expression

        Sample: $("h1").should(matchText("Hello\s*John"))

        Parameters:
        regex - e.g. Kicked.*Chuck Norris - in this case ".*" can contain any characters including spaces, tabs, CR etc.
      • text

        public static Condition text​(java.lang.String text)

        Sample: $("h1").shouldHave(text("Hello\s*John"))

        NB! Case insensitive

        NB! Ignores multiple whitespaces between words

        Parameters:
        text - expected text of HTML element
      • selectedText

        public static Condition selectedText​(java.lang.String expectedText)
        Checks for selected text on a given input web element

        Sample: $("input").shouldHave(selectedText("Text"))

        NB! Case sensitive

        Parameters:
        expectedText - expected selected text of the element
      • textCaseSensitive

        public static Condition textCaseSensitive​(java.lang.String text)

        Sample: $("h1").shouldHave(textCaseSensitive("Hello\s*John"))

        NB! Ignores multiple whitespaces between words

        Parameters:
        text - expected text of HTML element
      • exactText

        public static Condition exactText​(java.lang.String text)

        Sample: $("h1").shouldHave(exactText("Hello"))

        Case insensitive

        NB! Ignores multiple whitespaces between words

        Parameters:
        text - expected text of HTML element
      • exactTextCaseSensitive

        public static Condition exactTextCaseSensitive​(java.lang.String text)

        Sample: $("h1").shouldHave(exactTextCaseSensitive("Hello"))

        NB! Ignores multiple whitespaces between words

        Parameters:
        text - expected text of HTML element
      • cssClass

        public static Condition cssClass​(java.lang.String cssClass)

        Sample: $("input").shouldHave(cssClass("active"));

      • cssValue

        public static Condition cssValue​(java.lang.String propertyName,
                                         java.lang.String expectedValue)
        Checks if css property (style) applies for the element. Both explicit and computed properties are supported.

        Note that if css property is missing WebElement.getCssValue(java.lang.String) return empty string. In this case you should assert against empty string.

        Sample:

        <input style="font-size: 12">

        $("input").shouldHave(cssValue("font-size", "12"));

        $("input").shouldHave(cssValue("display", "block"));

        Parameters:
        propertyName - the css property (style) name of the element
        expectedValue - expected value of css property
        See Also:
        WebElement.getCssValue(java.lang.String)
      • not

        public static Condition not​(Condition condition)
        Negate given condition.

        Used for methods like $.shouldNot(exist), $.shouldNotBe(visible)

        Typically you don't need to use it.

      • and

        public static Condition and​(java.lang.String name,
                                    Condition... conditions)
        Check if element matches ALL given conditions.
        Parameters:
        name - Name of this condition, like "empty" (meaning e.g. empty text AND empty value).
        conditions - Conditions to match.
        Returns:
        logical AND for given conditions.
      • or

        public static Condition or​(java.lang.String name,
                                   Condition... conditions)
        Check if element matches ANY of given conditions.
        Parameters:
        name - Name of this condition, like "error" (meaning e.g. "error" OR "failed").
        conditions - Conditions to match.
        Returns:
        logical OR for given conditions.
      • be

        public static Condition be​(Condition delegate)
        Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))
        Parameters:
        delegate - next condition to wrap
        Returns:
        Condition
      • have

        public static Condition have​(Condition delegate)
        Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))
        Parameters:
        delegate - next condition to wrap
        Returns:
        Condition
      • apply

        public abstract boolean apply​(Driver driver,
                                      org.openqa.selenium.WebElement element)
        Check if given element matches this condition.
        Parameters:
        element - given WebElement
        Returns:
        true if element matches condition
      • applyNull

        public final boolean applyNull()
      • actualValue

        public java.lang.String actualValue​(Driver driver,
                                            org.openqa.selenium.WebElement element)
        If element didn't match the condition, returns the actual value of element. Used in error reporting. Optional. Makes sense only if you need to add some additional important info to error message.
        Parameters:
        driver -
        element - given WebElement
        Returns:
        any string that needs to be appended to error message.
      • because

        public Condition because​(java.lang.String message)
        Should be used for explaining the reason of condition
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getName

        public java.lang.String getName()
      • missingElementSatisfiesCondition

        public boolean missingElementSatisfiesCondition()