net.serenitybdd.screenplay.questions.WebElementQuestion Maven / Gradle / Ivy
package net.serenitybdd.screenplay.questions;
import net.serenitybdd.core.pages.WebElementFacade;
import net.serenitybdd.core.pages.WebElementState;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Question;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.serenitybdd.screenplay.annotations.Subject;
import net.serenitybdd.screenplay.matchers.statematchers.CheckForAbsenceHint;
import net.serenitybdd.screenplay.matchers.statematchers.MissingWebElement;
import net.serenitybdd.screenplay.targets.HasByLocator;
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.By;
import java.util.List;
import java.util.Set;
@Subject("#target")
public class WebElementQuestion implements Question, AcceptsHints {
private final Target target;
private boolean checkForAbsence = false;
public WebElementQuestion(Target target) {
this.target = target;
}
public static Question stateOf(Target target) {
return new WebElementQuestion(target);
}
public static Question stateOf(String xpathOrCssSelector) {
return new WebElementQuestion(Target.the(xpathOrCssSelector).locatedBy(xpathOrCssSelector));
}
public static Question stateOf(By byLocator) {
return new WebElementQuestion(Target.the(byLocator.toString()).located(byLocator));
}
public static Question valueOf(Target target) {
return stateOf(target);
}
public static Question valueOf(String xpathOrCssSelector) {
return stateOf(xpathOrCssSelector);
}
public static Question valueOf(By byLocator) {
return stateOf(byLocator);
}
public static Question the(Target target) {
return stateOf(target);
}
public static Question the(String xpathOrCssSelector) {
return stateOf(xpathOrCssSelector);
}
public static Question the(By byLocator) {
return stateOf(byLocator);
}
@Override
public WebElementState answeredBy(Actor actor) {
return (checkForAbsence) ? checkForAbsenceBy(actor) : checkForPresenceBy(actor);
}
private WebElementState checkForAbsenceBy(Actor actor) {
List matchingElements = target.resolveAllFor(actor);
if (matchingElements.isEmpty()) {
return new MissingWebElement(target.getName());
}
return matchingElements.get(0);
}
private WebElementState checkForPresenceBy(Actor actor) {
List matchingElements = target.resolveAllFor(actor);
//if nothing is found, create a UnresolvedTargetWebElementState but try to give him the selector of the target
if (matchingElements.isEmpty()) {
UnresolvedTargetWebElementState unresolvedTarget = new UnresolvedTargetWebElementState(target.getName());
try {
unresolvedTarget.setSelector(target.getCssOrXPathSelector());
} catch (Exception e) {
try {
if (target instanceof HasByLocator)
unresolvedTarget.setSelector(((HasByLocator) target).getLocator().toString());
else {
unresolvedTarget.setSelector(target.selectors(BrowseTheWeb.as(actor).getDriver()).toString());
}
} catch (Exception ignored) {
}
}
return unresolvedTarget;
} else {
return matchingElements.get(0);
}
//
// try {
// return target.resolveFor(actor);
// } catch (NoSuchElementException unresolvedTarget) {
// return new UnresolvedTargetWebElementState(target.getName());
// } catch (SerenityManagedException targetProbablyNotFound) {
// if (targetProbablyNotFound.getMessage().contains("No element was found")) {
// return new UnresolvedTargetWebElementState(target.getName());
// } else {
// throw targetProbablyNotFound;
// }
// }
}
@Override
public void apply(Set> hints) {
checkForAbsence = hints.contains(CheckForAbsenceHint.class);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy