All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.screenplay.questions.Displayed Maven / Gradle / Ivy

There is a newer version: 4.2.8
Show newest version
package net.serenitybdd.screenplay.questions;

import net.serenitybdd.core.pages.WebElementFacade;
import net.serenitybdd.core.pages.WebElementState;
import net.serenitybdd.screenplay.Question;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
import static net.serenitybdd.screenplay.questions.LabelledQuestion.answer;
import static net.serenitybdd.screenplay.questions.LabelledQuestion.answerEach;

/**
 * Check whether an element is displayed.
 */
public class Displayed {

    public static Question of(Target target) {
        return answer(target.getName() + " is enabled for", actor -> matches(target.resolveAllFor(actor)));
    }

    public static Question of(By byLocator) {
        return answer(byLocator + " is enabled for", actor -> matches(BrowseTheWeb.as(actor).findAll(byLocator)));
    }

    public static Question of(String locator) {
        return answer(locator + " is enabled for", actor -> matches(BrowseTheWeb.as(actor).findAll(locator)));
    }


    public static Question> ofEach(Target target) {
        return answerEach(target.getName() + " is enabled for each", actor -> target.resolveAllFor(actor)
                .stream()
                .map(element -> matches(singletonList(element)))
                .collect(Collectors.toList()));
    }

    public static Question> ofEach(By byLocator) {
        return answerEach(byLocator + " is enabled for each", actor -> BrowseTheWeb.as(actor).findAll(byLocator)
                .stream()
                .map(element -> matches(singletonList(element)))
                .collect(Collectors.toList()));
    }

    public static Question> ofEach(String locator) {
        return answerEach(locator + " is enabled for each", actor -> BrowseTheWeb.as(actor).findAll(locator)
                .stream()
                .map(element -> matches(singletonList(element)))
                .collect(Collectors.toList()));
    }

    private static boolean matches(List elements) {
        return elements.stream()
                .findFirst()
                .map(WebElement::isDisplayed)
                .orElse(false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy