com.codeborne.selenide.WebElementsCondition Maven / Gradle / Ivy
package com.codeborne.selenide;
import com.codeborne.selenide.ex.UIAssertionError;
import com.codeborne.selenide.impl.CollectionSource;
import org.openqa.selenium.WebElement;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import static java.lang.System.lineSeparator;
public abstract class WebElementsCondition {
protected String explanation;
@Nonnull
@CheckReturnValue
public CheckResult check(Driver driver, List elements) {
throw new UnsupportedOperationException("Implement one of 'check' methods in your condition");
}
/**
* The most powerful way to implement condition.
* Can check the collection using JavaScript or any other effective means.
* Also, can return "actual values" in the returned {@link CheckResult} object.
*/
@Nonnull
@CheckReturnValue
public CheckResult check(CollectionSource collection) {
List elements = collection.getElements();
return check(collection.driver(), elements);
}
/**
* Override this method if you want to customize error class or description
*/
public void fail(CollectionSource collection, CheckResult lastCheckResult, @Nullable Exception cause, long timeoutMs) {
throw new UIAssertionError(collection.driver(),
errorMessage() +
lineSeparator() + "Actual: " + lastCheckResult.getActualValue() +
lineSeparator() + "Expected: " + expectedValue() +
(explanation == null ? "" : lineSeparator() + "Because: " + explanation) +
lineSeparator() + "Collection: " + collection.description(),
toString(), lastCheckResult.getActualValue()
);
}
public String errorMessage() {
return "Collection check failed";
}
public String expectedValue() {
return toString();
}
@Override
public abstract String toString();
/**
* Use for explaining the reason of condition:
* WHY you think this collection should match that condition?
*/
public WebElementsCondition because(String explanation) {
this.explanation = explanation;
return this;
}
public boolean missingElementsSatisfyCondition() {
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy