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

net.serenitybdd.screenplay.questions.CSSValue 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.screenplay.Question;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.By;

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

import static java.util.Collections.singletonList;

/**
 * Retrieve the value of a specific CSS attribute of an element.
 */
public class CSSValue {

    public static Question of(Target target, String attributeName) {
        return Question.about(attributeName +" CSS value of " + target.getName())
                .answeredBy(actor -> matches(target.resolveAllFor(actor), attributeName));
    }

    public static Question of(By byLocator, String attributeName) {
        return Question.about(attributeName +" CSS value of element located by " + byLocator)
                .answeredBy(actor -> matches(BrowseTheWeb.as(actor).findAll(byLocator), attributeName));
    }

    public static Question of(String locator, String attributeName) {
        return Question.about(attributeName +" CSS value of " + locator)
                .answeredBy(actor -> matches(BrowseTheWeb.as(actor).findAll(locator), attributeName));
    }

    public static Question> ofEach(Target target, String attributeName) {
        return Question.about(attributeName +" CSS value of each" + target.getName())
                .answeredBy(actor -> target.resolveAllFor(actor)
                .stream()
                .map(element -> matches(singletonList(element), attributeName))
                .collect(Collectors.toList()));
    }

    public static Question> ofEach(By byLocator, String attributeName) {
        return Question.about(attributeName +" CSS value of each" + byLocator)
                .answeredBy(actor -> BrowseTheWeb.as(actor).findAll(byLocator)
                .stream()
                .map(element -> matches(singletonList(element), attributeName))
                .collect(Collectors.toList()));
    }

    public static Question> ofEach(String locator, String attributeName) {
        return Question.about(attributeName +" CSS value of " + locator)
                .answeredBy(actor -> BrowseTheWeb.as(actor).findAll(locator)
                .stream()
                .map(element -> matches(singletonList(element), attributeName))
                .collect(Collectors.toList()));
    }

    public static QuestionForName of(Target target) {
        return name -> CSSValue.of(target, name);
    }

    public static QuestionForName of(By byLocator) {
        return name -> CSSValue.of(byLocator, name);
    }

    public static QuestionForName of(String locator) {
        return name -> CSSValue.of(locator, name);
    }

    public static QuestionForNames ofEach(Target target) {
        return name -> CSSValue.ofEach(target, name);
    }

    public static QuestionForNames ofEach(By byLocator) {
        return name -> CSSValue.ofEach(byLocator, name);
    }

    public static QuestionForNames ofEach(String locator) {
        return name -> CSSValue.ofEach(locator, name);
    }

    private static String matches(List elements, String attributeName) {
        return elements.stream()
                .findFirst()
                .map(element -> element.getCssValue(attributeName))
                .orElse("");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy