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

io.github.kgress.scaffold.webelements.DropDownWebElement Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
package io.github.kgress.scaffold.webelements;

import io.github.kgress.scaffold.BaseWebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

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

/**
 * Scaffold's strongly typed interpretation of a dropdown element.
 */
public class DropDownWebElement extends BaseClickableWebElement {

    /**
     * Creates a new {@link DropDownWebElement}. It is highly recommended using {@link By#cssSelector(String)} over
     * another method, such as {@link By#xpath(String)}, in almost all cases as it can be less flaky and less reliant
     * on DOM hierarchy.
     *
     * @see BaseWebElement#BaseWebElement(String)
     * @param cssSelector   the value of the {@link By#cssSelector(String)}
     */
    public DropDownWebElement(String cssSelector) {
        super(cssSelector);
    }

    /**
     * Creates a new {@link DropDownWebElement} and mark whether the element is hidden. It is highly recommended using
     * {@link By#cssSelector(String)} over another method, such as {@link By#xpath(String)}, in almost all cases as it
     * can be less flaky and less reliant on DOM hierarchy.
     *
     * @see BaseWebElement#BaseWebElement(String)
     * @param cssSelector   the value of the {@link By#cssSelector(String)}
     * @param isHidden      a {@link boolean} to specify if this element could be hidden
     */
    public DropDownWebElement(String cssSelector, boolean isHidden) {
        super(cssSelector, isHidden);
    }

    /**
     * Use this constructor when you'd like to locate an element with a {@link By} method different from
     * {@link By#cssSelector(String)}. We strongly recommend using {@link #DropDownWebElement(String cssSelector)}
     * in almost all cases.
     *
     * @see BaseWebElement#BaseWebElement(By)
     * @param by    the {@link By} locator
     */
    public DropDownWebElement(By by) {
        super(by);
    }

    /**
     * Use this constructor when you'd like to locate an element with a {@link By} method different from
     * {@link By#cssSelector(String)} and mark whether the element is hidden. We strongly recommend using
     * {@link #DropDownWebElement(String cssSelector)} in almost all cases.
     *
     * @see BaseWebElement#BaseWebElement(By)
     * @param by        the {@link By} locator
     * @param isHidden  a {@link boolean} to specify if this element could be hidden
     */
    public DropDownWebElement(By by, boolean isHidden) {
        super(by, isHidden);
    }

    /**
     * Use this constructor when you'd like to locate an element with a child and parent {@link By} together. Useful
     * when you want a more verbose element definition in context of your websites' DOM.
     *
     * @see BaseWebElement#BaseWebElement(By, By)
     * @param by        the {@link By} locator for the child element
     * @param parentBy  the {@link By} locator for the parent element
     */
    public DropDownWebElement(By by, By parentBy) {
        super(by, parentBy);
    }

    /**
     * Use this constructor when you'd like to locate an element with a child and parent {@link By} together and mark
     * whether the element is hidden. Useful when you want a more verbose element definition in context of your
     * websites' DOM.
     *
     * @param by       the {@link By} locator to be used by this element
     * @param parentBy the {@link By} locator for the parent element
     * @param isHidden a {@link boolean} to specify if this element could be hidden
     */
    public DropDownWebElement(By by, By parentBy, boolean isHidden) {
        super(by, parentBy, isHidden);
    }

    /**
     * This constructor is {@link Deprecated}. Please use a constructor that uses a {@link By}
     * locator. Using a constructor with {@link WebElement} will bypass scaffold's core
     * functionality. An example of using a {@link By} locator constructor: 
{@code
     * private final DivWebElement header = new DivWebElement(By.cssSelector(".header"));
     * }
* * Creates a new Scaffold element with a raw {@link WebElement}. This is primarily used during construction of * elements in the {@link #findElements(Class, By)} method. * * When instantiating new elements with this constructor, There is a risk of a * {@link StaleElementReferenceException} occurring when interacting with elements since * {@link #getRawWebElement()} will return the raw web element on being present. This means we are not re * finding the element prior to interacting with it. Use this constructor at your own risk. * * @param by the {@link By} locator to be used by this element * @param parentBy the {@link By} locator to be used by the parent element * @param webElement the {@link WebElement} being wrapped */ @Deprecated public DropDownWebElement(By by, By parentBy, WebElement webElement) { super(by, parentBy, webElement); } /** * This constructor is {@link Deprecated}. Please use a constructor that uses a {@link By} * locator. Using a constructor with {@link WebElement} will bypass scaffold's core * functionality. An example of using a {@link By} locator constructor:
{@code
     * private final DivWebElement header = new DivWebElement(By.cssSelector(".header"));
     * }
* * Creates a new Scaffold element with a raw {@link WebElement}. This is primarily used during construction of * elements in the {@link #findElements(Class, By)} method. * * When instantiating new elements with this constructor, There is a risk of a * {@link StaleElementReferenceException} occurring when interacting with elements since * {@link #getRawWebElement()} will return the raw web element on being present. This means we are not re * finding the element prior to interacting with it. Use this constructor at your own risk. * * @param by the {@link By} locator to be used by this element * @param webElement the {@link WebElement} being wrapped */ @Deprecated public DropDownWebElement(By by, WebElement webElement) { super(by, webElement); } /** * This constructor is {@link Deprecated}. Please use a constructor that uses a {@link By} * locator. Using a constructor with {@link WebElement} will bypass scaffold's core * functionality. An example of using a {@link By} locator constructor:
{@code
     * private final DivWebElement header = new DivWebElement(By.cssSelector(".header"));
     * }
* * Creates a new Scaffold element with a raw {@link WebElement}. This is primarily used during construction of * elements in the {@link #findElements(Class, By)} method. * * When instantiating new elements with this constructor, There is a risk of a * {@link StaleElementReferenceException} occurring when interacting with elements since * {@link #getRawWebElement()} will return the raw web element on being present. This means we are not re * finding the element prior to interacting with it. Use this constructor at your own risk. * * @param webElement the {@link WebElement} being wrapped */ @Deprecated public DropDownWebElement(WebElement webElement) { super(webElement); } /** * Returns a list of options in the DropDown * * @return the list of options. */ public List getOptionsText() { return getSelectElement().getOptions().stream() .map(WebElement::getText) .collect(Collectors.toList()); } /** * Selects an option in the dropdown based on the index provided. * * @see Select#selectByIndex(int) * @param index the index to select */ public void selectByIndex(int index) { getSelectElement().selectByIndex(index); } /** * Selects an option in the dropdown based on the value provided. * * @see Select#selectByValue(String) (int) * @param value the value to select */ public void selectByValue(String value) { getSelectElement().selectByValue(value); } /** * Sets the dropdown to the option contained in the visible to the option tag. If the value is blank, leave the * field alone. If someone is doing data-driven tests that contain many variables, we don't want to require * them to surround every instance of this method with the same if block. * * @param value the value to select */ public void selectByVisibleText(String value) { if (value.isBlank()) { getSelectElement().selectByVisibleText(value); } } /** * Return a Selenium {@link Select} object (a combo box) based on the underlying {@link WebElement} * * @return the {@link Select} object */ protected Select getSelectElement() { return new Select(this.getRawWebElement()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy