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

com.pojosontheweb.selenium.formz.Select Maven / Gradle / Ivy

The newest version!
package com.pojosontheweb.selenium.formz;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.pojosontheweb.selenium.Findr;
import org.openqa.selenium.WebElement;

public class Select {

    private final Findr findr;

    public Select(Findr findr) {
        this.findr = findr;
    }

    public Findr getFindr() {
        return findr;
    }

    public Select selectByVisibleText(String text) {
        findr.eval(makeSelectByVisibleText(text));
        return this;
    }

    public Select assertSelectedText(String expected) {
        findr.where(selectedText(expected)).eval();
        return this;
    }

    public static Function makeSelectByVisibleText(final String text) {
        return new Function() {
            @Override
            public Object apply(WebElement select) {
                org.openqa.selenium.support.ui.Select selSelect =
                    new org.openqa.selenium.support.ui.Select(select);
                selSelect.selectByVisibleText(text);
                return true;
            }

            @Override
            public String toString() {
                return "selectByVisibleText:" + text;
            }
        };
    }

    public static Predicate selectedText(final String expectedText) {
        return new Predicate() {
            @Override
            public boolean apply(WebElement select) {
                org.openqa.selenium.support.ui.Select selSelect =
                        new org.openqa.selenium.support.ui.Select(select);
                WebElement selOption = selSelect.getFirstSelectedOption();
                if (selOption == null) {
                    return false;
                }
                String text = selOption.getText();
                if (text == null) {
                    return false;
                }
                return text.equals(expectedText);
            }

            @Override
            public String toString() {
                return "selectedText:" + expectedText;
            }

        };
    }

    /**
     * @deprecated use instance method(s) instead
     */
    @Deprecated
    public static void selectByVisibleText(Findr selectFindr, final String text) {
        selectFindr.eval(makeSelectByVisibleText(text));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy