Class Condition
- java.lang.Object
-
- com.codeborne.selenide.Condition
-
- Direct Known Subclasses:
And,Attribute,AttributeWithValue,CaseSensitiveText,Checked,CssClass,CssValue,CustomMatch,Disabled,Enabled,ExactText,ExactTextCaseSensitive,Exist,ExplainedCondition,Focused,Hidden,IsImageLoaded,MatchAttributeWithValue,MatchText,NamedCondition,Not,Or,PseudoElementPropertyWithValue,Selected,SelectedText,Text,Value,Visible
@ParametersAreNonnullByDefault public abstract class Condition extends java.lang.ObjectConditions to match web elements: checks for visibility, text etc.
-
-
Field Summary
Fields Modifier and Type Field Description static ConditionappearSynonym forvisible- may be used for better readabilitystatic ConditionappearsSynonym forvisible- may be used for better readabilitystatic ConditioncheckedChecks that checkbox is checkedstatic ConditiondisabledChecks that element is disabledstatic ConditiondisappearSynonym forhidden- may be used for better readability:static ConditiondisappearsSynonym forhidden- may be used for better readability:static Conditionempty1) For input element, check that value is missing or emptystatic ConditionenabledChecks that element is not disabledstatic ConditionexistCheck if element exist.static ConditionfocusedCheck if browser focus is currently in given element.static ConditionhiddenChecks that element is not visible or does not exists.static ConditionimageCheck if image is loaded.static ConditionreadonlyCheck if element has "readonly" attribute (with any value)static ConditionselectedChecks that element is selected (inputs like drop-downs etc.)static ConditionvisibleChecks if element is visible
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.StringactualValue(Driver driver, org.openqa.selenium.WebElement element)If element didn't match the condition, returns the actual value of element.static Conditionand(java.lang.String name, Condition... conditions)Check if element matches ALL given conditions.abstract booleanapply(Driver driver, org.openqa.selenium.WebElement element)Check if given element matches this condition.booleanapplyNull()static Conditionattribute(java.lang.String attributeName)Check if element has given attribute (with any value)static Conditionattribute(java.lang.String attributeName, java.lang.String expectedAttributeValue)Sample:$("#mydiv").shouldHave(attribute("fileId", "12345"));static ConditionattributeMatching(java.lang.String attributeName, java.lang.String attributeRegex)Assert that given element's attribute matches given regular expressionstatic Conditionbe(Condition delegate)Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))Conditionbecause(java.lang.String message)Should be used for explaining the reason of conditionstatic ConditioncssClass(java.lang.String cssClass)Asserts that element has the given class.static ConditioncssValue(java.lang.String propertyName, java.lang.String expectedValue)Checks if css property (style) applies for the element.static ConditionexactText(java.lang.String text)Assert that element has exactly (case insensitive) given textstatic ConditionexactTextCaseSensitive(java.lang.String text)Assert that element has exactly the given textstatic ConditionexactValue(java.lang.String value)Sample:$("#input").shouldHave(exactValue("John"));java.lang.StringgetName()static Conditionhave(Condition delegate)Used to form human-readable condition expression Example element.should(be(visible),have(text("abc"))static Conditionid(java.lang.String id)Sample:$("#input").shouldHave(id("myForm"))static Conditionmatch(java.lang.String description, java.util.function.Predicate<org.openqa.selenium.WebElement> predicate)Checks if element matches the given predicate.static ConditionmatchesText(java.lang.String text)The same as matchText()static ConditionmatchText(java.lang.String regex)Assert that given element's text matches given regular expressionbooleanmissingElementSatisfiesCondition()static Conditionname(java.lang.String name)Asserts the name attribute of the element to be exact stringConditionnegate()static Conditionnot(Condition condition)Negate given condition.static Conditionor(java.lang.String name, Condition... conditions)Check if element matches ANY of given conditions.static Conditionpseudo(java.lang.String pseudoElementName, java.lang.String expectedValue)Check that element has given the "content" property of the pseudo-elementstatic Conditionpseudo(java.lang.String pseudoElementName, java.lang.String propertyName, java.lang.String expectedValue)Check that element has given the property value of the pseudo-elementstatic ConditionselectedText(java.lang.String expectedText)Checks on a element that exactly given text is selected (=marked with mouse/keyboard)static Conditiontext(java.lang.String text)Assert that element contains given text as a substring.static ConditiontextCaseSensitive(java.lang.String text)Assert that element contains given text as a case sensitive substringjava.lang.StringtoString()static Conditiontype(java.lang.String type)Asserts the type attribute of the element to be exact stringstatic Conditionvalue(java.lang.String expectedValue)Assert that element contains given "value" attribute as substring NB! Ignores difference in non-visible characters like spaces, non-breakable spaces, tabs, newlines etc.
-
-
-
Field Detail
-
visible
public static final Condition visible
Checks if element is visibleSample:
$("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
appearSample:
$("input").shouldBe(hidden);
-
appear
public static final Condition appear
Synonym forvisible- may be used for better readabilitySample:
$("#logoutLink").should(appear);
-
appears
public static final Condition appears
Synonym forvisible- may be used for better readability$("#logoutLink").waitUntil(appears, 10000);
-
disappears
public static final Condition disappears
Synonym forhidden- may be used for better readability:Sample:
$("#loginLink").waitUntil(disappears, 9000);
-
disappear
public static final Condition disappear
Synonym forhidden- 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 emptySample:
$("#input").shouldBe(empty)2) For other elements, check that text is empty
Sample:
$("h2").shouldBe(empty)
-
image
public static final Condition image
Check if image is loaded.
-
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 (inputs like drop-downs etc.)- See Also:
WebElement.isSelected()
-
checked
public static final Condition checked
Checks that checkbox is checked- See Also:
WebElement.isSelected()
-
-
Method Detail
-
attribute
@CheckReturnValue @Nonnull 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
@CheckReturnValue @Nonnull public static Condition attribute(java.lang.String attributeName, java.lang.String expectedAttributeValue)
Sample:
$("#mydiv").shouldHave(attribute("fileId", "12345"));- Parameters:
attributeName- name of attributeexpectedAttributeValue- expected value of attribute
-
attributeMatching
@CheckReturnValue @Nonnull public static Condition attributeMatching(java.lang.String attributeName, java.lang.String attributeRegex)
Assert that given element's attribute matches given regular expressionSample:
$("h1").should(attributeMatching("fileId", ".*12345.*"))- Parameters:
attributeName- name of attributeattributeRegex- regex to match attribute value
-
value
@CheckReturnValue @Nonnull public static Condition value(java.lang.String expectedValue)
Assert that element contains 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
-
pseudo
@CheckReturnValue @Nonnull public static Condition pseudo(java.lang.String pseudoElementName, java.lang.String propertyName, java.lang.String expectedValue)
Check that element has given the property value of the pseudo-elementSample:
$("input").shouldHave(pseudo(":first-letter", "color", "#ff0000"));- Parameters:
pseudoElementName- pseudo-element name of the element, ":before", ":after", ":first-letter", ":first-line", ":selection"propertyName- property name of the pseudo-elementexpectedValue- expected value of the property
-
pseudo
@CheckReturnValue @Nonnull public static Condition pseudo(java.lang.String pseudoElementName, java.lang.String expectedValue)
Check that element has given the "content" property of the pseudo-elementSample:
$("input").shouldHave(pseudo(":before", "Hello"));- Parameters:
pseudoElementName- pseudo-element name of the element, ":before", ":after"expectedValue- expected content of the pseudo-element
-
exactValue
@CheckReturnValue @Nonnull public static Condition exactValue(java.lang.String value)
Sample:
$("#input").shouldHave(exactValue("John"));- Parameters:
value- expected value of input field
-
name
@CheckReturnValue @Nonnull public static Condition name(java.lang.String name)
Asserts the name attribute of the element to be exact stringSample:
$("#input").shouldHave(name("username"))- Parameters:
name- expected name of input field
-
type
@CheckReturnValue @Nonnull public static Condition type(java.lang.String type)
Asserts the type attribute of the element to be exact stringSample:
$("#input").shouldHave(type("checkbox"))- Parameters:
type- expected type of input field
-
id
@CheckReturnValue @Nonnull public static Condition id(java.lang.String id)
Sample:
$("#input").shouldHave(id("myForm"))- Parameters:
id- expected id of input field
-
matchesText
@CheckReturnValue @Nonnull public static Condition matchesText(java.lang.String text)
The same as matchText()Sample:
$(".error_message").waitWhile(matchesText("Exception"), 12000)- See Also:
matchText(String)
-
matchText
@CheckReturnValue @Nonnull public static Condition matchText(java.lang.String regex)
Assert that given element's text matches given regular expressionSample:
$("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
@CheckReturnValue @Nonnull public static Condition text(java.lang.String text)
Assert that element contains given text as a substring.
Sample:
$("h1").shouldHave(text("Hello\s*John"))NB! Case insensitive
NB! Ignores multiple whitespaces between words
- Parameters:
text- expected text of HTML element. NB! Empty string is not allowed (because any element does contain an empty text).- Throws:
java.lang.IllegalArgumentException- if given text is null or empty
-
selectedText
@CheckReturnValue @Nonnull public static Condition selectedText(java.lang.String expectedText)
Checks on a element that exactly given text is selected (=marked with mouse/keyboard)Sample:
$("input").shouldHave(selectedText("Text"))NB! Case sensitive
- Parameters:
expectedText- expected selected text of the element
-
textCaseSensitive
@CheckReturnValue @Nonnull public static Condition textCaseSensitive(java.lang.String text)
Assert that element contains given text as a case sensitive substringSample:
$("h1").shouldHave(textCaseSensitive("Hello\s*John"))NB! Ignores multiple whitespaces between words
- Parameters:
text- expected text of HTML element
-
exactText
@CheckReturnValue @Nonnull public static Condition exactText(java.lang.String text)
Assert that element has exactly (case insensitive) given textSample:
$("h1").shouldHave(exactText("Hello"))Case insensitive
NB! Ignores multiple whitespaces between words
- Parameters:
text- expected text of HTML element
-
exactTextCaseSensitive
@CheckReturnValue @Nonnull public static Condition exactTextCaseSensitive(java.lang.String text)
Assert that element has exactly the given textSample:
$("h1").shouldHave(exactTextCaseSensitive("Hello"))NB! Ignores multiple whitespaces between words
- Parameters:
text- expected text of HTML element
-
cssClass
@CheckReturnValue @Nonnull public static Condition cssClass(java.lang.String cssClass)
Asserts that element has the given class. Element may other classes too.Sample:
$("input").shouldHave(cssClass("active"));
-
cssValue
@CheckReturnValue @Nonnull public static Condition cssValue(java.lang.String propertyName, @Nullable 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 elementexpectedValue- expected value of css property- See Also:
WebElement.getCssValue(java.lang.String)
-
match
@CheckReturnValue @Nonnull public static Condition match(java.lang.String description, java.util.function.Predicate<org.openqa.selenium.WebElement> predicate)
Checks if element matches the given predicate.Sample:
$("input").should(match("empty value attribute", el -> el.getAttribute("value").isEmpty()));- Parameters:
description- the description of the predicatepredicate- thePredicateto match
-
not
@CheckReturnValue @Nonnull 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.
-
negate
@Nonnull public Condition negate()
-
and
@CheckReturnValue @Nonnull 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
@CheckReturnValue @Nonnull 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
@CheckReturnValue @Nonnull 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
@CheckReturnValue @Nonnull 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 boolean applyNull()
-
actualValue
@Nullable 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- given driverelement- given WebElement- Returns:
- any string that needs to be appended to error message.
-
because
@CheckReturnValue @Nonnull public Condition because(java.lang.String message)
Should be used for explaining the reason of condition
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getName
public java.lang.String getName()
-
missingElementSatisfiesCondition
public boolean missingElementSatisfiesCondition()
-
-