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

org.seleniumhq.selenium.fluent.FluentSelect Maven / Gradle / Ivy

/*
Copyright 2011 Software Freedom Conservancy

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.seleniumhq.selenium.fluent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class FluentSelect extends FluentWebElement {

    private Select currentSelect;

    public FluentSelect(WebDriver delegate, WebElement currentElement, Context context) {
        super(delegate, currentElement, context);
    }

    private FluentSelect(WebDriver delegate, Select currentSelect, WebElement currentElement, Context context) {
        super(delegate, currentElement, context);
        this.currentSelect = currentSelect;
    }

    /**
     * @return Whether this select element support selecting multiple options at the same time? This
     *         is done by checking the value of the "multiple" attribute.
     */
    public boolean isMultiple() {
        return decorateExecution(new Execution() {
            public Boolean execute() {
                return getSelect().isMultiple();
            }
        }, Context.singular(context, "isMultiple"));
    }

    /**
     * @return All options belonging to this select tag
     */
    public List getOptions() {
        return decorateExecution(new Execution>() {
            public List execute() {
                return getSelect().getOptions();
            }
        }, Context.singular(context, "getOptions"));
    }

    /**
     * @return All selected options belonging to this select tag
     */
    public List getAllSelectedOptions() {
        return decorateExecution(new Execution>() {
            public List execute() {
                return getSelect().getAllSelectedOptions();
            }
        }, Context.singular(context, "getAllSelectedOptions"));
    }

    /**
     * @return The first selected option in this select tag (or the currently selected option in a
     *         normal select)
     */
    public WebElement getFirstSelectedOption() {
        return decorateExecution(new Execution() {
            public WebElement execute() {
                return getSelect().getFirstSelectedOption();
            }
        }, Context.singular(context, "getFirstSelectedOption"));
    }

    /**
     * Select all options that display text matching the argument. That is, when given "Bar" this
     * would select an option like:
     * 

* <option value="foo">Bar</option> * * @param text The visible text to match against */ public FluentSelect selectByVisibleText(final String text) { decorateExecution(new Execution() { public Boolean execute() { getSelect().selectByVisibleText(text); return true; } }, Context.singular(context, "selectByVisibleText", null, text)); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Select the option at the given index. This is done by examing the "index" attribute of an * element, and not merely by counting. * * @param index The option at this index will be selected */ public FluentSelect selectByIndex(final int index) { decorateExecution(new Execution() { public Boolean execute() { getSelect().selectByIndex(index); return true; } }, Context.singular(context, "selectByIndex", null, index)); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Select all options that have a value matching the argument. That is, when given "foo" this * would select an option like: *

* <option value="foo">Bar</option> * * @param value The value to match against */ public FluentSelect selectByValue(final String value) { decorateExecution(new Execution() { public Boolean execute() { getSelect().selectByValue(value); return true; } }, Context.singular(context, "selectByValue", null, value)); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Clear all selected entries. This is only valid when the SELECT supports multiple selections. * * @throws UnsupportedOperationException If the SELECT does not support multiple selections */ public FluentSelect deselectAll() { decorateExecution(new Execution() { public Boolean execute() { getSelect().deselectAll(); return true; } }, Context.singular(context, "deselectAll")); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Deselect all options that have a value matching the argument. That is, when given "foo" this * would deselect an option like: *

* <option value="foo">Bar</option> * * @param value The value to match against */ public FluentSelect deselectByValue(final String value) { decorateExecution(new Execution() { public Boolean execute() { getSelect().deselectByValue(value); return true; } }, Context.singular(context, "deselectByValue", null, value)); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Deselect the option at the given index. This is done by examining the "index" attribute of an * element, and not merely by counting. * * @param index The option at this index will be deselected */ public FluentSelect deselectByIndex(final int index) { decorateExecution(new Execution() { public Boolean execute() { getSelect().deselectByIndex(index); return true; } }, Context.singular(context, "deselectByIndex", null, index)); return new FluentSelect(super.delegate, currentElement, this.context); } /** * Deselect all options that display text matching the argument. That is, when given "Bar" this * would deselect an option like: *

* <option value="foo">Bar</option> * * @param text The visible text to match against */ public FluentSelect deselectByVisibleText(final String text) { decorateExecution(new Execution() { public Boolean execute() { getSelect().deselectByVisibleText(text); return true; } }, Context.singular(context, "deselectByVisibleText", null, text)); return new FluentSelect(super.delegate, currentElement, this.context); } protected synchronized Select getSelect() { if (currentSelect == null) { currentSelect = new Select(currentElement); } return currentSelect; } public FluentSelect within(Period period) { return new MorePatientFluentSelect(delegate, Context.singular(context, "within", null, period), currentSelect, currentElement, period); } // TODO rename private class MorePatientFluentSelect extends FluentSelect { private final Period period; public MorePatientFluentSelect(WebDriver webDriver, Context context, Select currentSelect, WebElement currentElement, Period period) { super(webDriver, currentSelect, currentElement, context); this.period = period; } @Override protected WebElement findIt(By by) { return retryingFindIt(by); } @Override protected List findThem(By by) { return retryingFindThem(by); } @Override protected Period getPeriod() { return period; } @Override protected void changeTimeout() { delegate.manage().timeouts().implicitlyWait(period.howLong(), period.timeUnit()); } @Override protected void resetTimeout() { delegate.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy