Custom conditions
List of custom conditions, created by Selenide community
NB! These conditions can be found in Selenide tests.
Moving
Author: Andrejs Kalnačs @andrejska
Description: Checks that element is moving
Note: Element should implement Locatable
Sample: $("#loginLink").should(moveAround(300))
Code:
public static Condition moveAround(int movePeriodMs) {
return new Condition("moveAround") {
@Nonnull
@Override
public CheckResult check(Driver driver, WebElement element) {
if (!(element instanceof Locatable)) {
throw new RuntimeException("Provided WebElement is not Locatable, cannot understand if it moving or not");
}
Point initialLocation = ((Locatable) element).getCoordinates().inViewPort();
sleepAtLeast(movePeriodMs);
Point finalLocation = ((Locatable) element).getCoordinates().inViewPort();
return new CheckResult(!initialLocation.equals(finalLocation), "Location: " + finalLocation);
}
};
}Child
Author: Olivier Grech @olivier-grech
Description: Check condition on child
Sample: $$("#multirowTable tr").filterBy(child("td:nth-child(2)", text("Norris")));
Code:
public static Condition child(final String childCssSelector, final Condition condition) {
return new Condition("child " + childCssSelector + " with " + condition.getName()) {
@Nonnull
@Override
public CheckResult check(Driver driver, WebElement element) {
WebElement child = element.findElement(By.cssSelector(childCssSelector));
return condition.check(driver, child);
}
};
}ValueInAttribute
Author: Ostap Oleksyn @ostap-oleksyn
Description: Check if element attribute contains given value. Case sensitive
Note: You can also give a name such as attributeContains for better readability.
Sample: $("#task").shouldHave(valueInAttribute("class", "done"));
Code:
public static Condition valueInAttribute(String attributeName, String value) {
return new Condition(String.format("value '%s' in attribute '%s'", value, attributeName)) {
@Override
@Nonnull
public CheckResult check(Driver driver, WebElement element) {
String attr = element.getAttribute(attributeName);
String attributeValue = attr == null ? "" : attr;
return new CheckResult(attributeValue.contains(value), attributeValue);
}
};
}TextOfLength
Author: Andrei Solntsev @asolntsev
Description: Check input text length
Sample: $("#random-username").shouldHave(textOfLength(42));
Code:
public static Condition textOfLength(int expectedLength) {
return new Condition("text of length " + expectedLength) {
@Nonnull
@Override
public CheckResult check(Driver driver, WebElement webElement) {
String text = webElement.getText();
return new CheckResult(text.length() == expectedLength, "length(\"" + text + "\") = " + text.length());
}
};
}