下面列出了org.openqa.selenium.support.ui.Select#getOptions ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public WebElement randomSelect(Element ele)
{
Select select = createSelect(ele);
if(select != null)
{
List<WebElement> options = select.getOptions();
if(CollectionUtils.isNotEmpty(options))
{
int count = options.size();
int index = RandomUtils.nextInt(count);
index = (index == 0 ? 1 : index); //通常第一个选项都是无效的选项
select.selectByIndex(index);
return options.get(index);
}
}
return null;
}
/**
* unselect a value
*
* @param value the value to unselect
*/
@Override
@PublicAtsApi
public void unsetValue(
String value ) {
new RealHtmlElementState(this).waitToBecomeExisting();
WebElement element = RealHtmlElementLocator.findElement(this);
Select select = new Select(element);
// select.deselectByVisibleText( value ); // this method doesn't throw an exception if the option doesn't exist
for (WebElement option : select.getOptions()) {
if (option.getText().equals(value)) {
if (option.isSelected()) {
option.click();
UiEngineUtilities.sleep();
}
return;
}
}
throw new SeleniumOperationException("Option with label '" + value + "' not found. ("
+ this.toString() + ")");
}
@Action(object = ObjectType.SELENIUM,
desc = "Assert if the select list [<Object>] contains [<Data>]",
input = InputType.YES)
public void assertSelectContains() {
if (elementPresent()) {
Boolean isPresent = false;
Select select = new Select(Element);
for (WebElement option : select.getOptions()) {
if (option.getText().trim().equals(Data)) {
isPresent = true;
break;
}
}
if (isPresent) {
Report.updateTestLog(Action, ObjectName + " Contains the Option " + Data, Status.DONE);
} else {
Report.updateTestLog(Action, ObjectName + " doesn't Contains the Option " + Data, Status.DEBUG);
}
} else {
throw new ElementException(ElementException.ExceptionType.Element_Not_Found, ObjectName);
}
}
public List<String> getAllDropDownValues(By locator) {
Select select = new Select(getElement(locator));
List<WebElement> elementList = select.getOptions();
List<String> valueList = new LinkedList<String>();
for (WebElement element : elementList) {
oLog.info(element.getText());
valueList.add(element.getText());
}
return valueList;
}
@Override
public void selectItemInDropDownList(Select select, String text, boolean addition)
{
if (select != null)
{
boolean multiple = select.isMultiple();
if (!multiple && addition)
{
softAssert
.recordFailedAssertion("Multiple selecting is not available to single select drop down");
return;
}
boolean selected = false;
List<WebElement> options = select.getOptions();
for (int i = 0; i < options.size(); i++)
{
WebElement currentOption = options.get(i);
String optionValue = webElementActions.getElementText(currentOption).trim();
if (text.equals(optionValue))
{
select.selectByIndex(i);
selected = true;
if (!multiple)
{
break;
}
}
else if (currentOption.isSelected() && !addition && multiple)
{
select.deselectByIndex(i);
}
}
String assertionMessage = multiple ? String.format(
"Items with the text '%s' are selected from a drop down", text) : String.format(
"Item with the text '%s' is selected from a drop down", text);
softAssert.assertTrue(assertionMessage, selected);
waitActions.waitForPageLoad();
}
}
/**
* {@inheritDoc}
*/
@Override
public int findOptionByIgnoreCaseText(String text, Select dropDown) {
int index = 0;
for (WebElement option : dropDown.getOptions()) {
if (comparingNames(text, option.getText())) {
return index;
}
index++;
}
return -1;
}
@Override
public List<String> getAllListOptions(String locator) {
List<String> optionValues = new ArrayList<String>();
Select menuList = getSelectObject(locator);
List<WebElement> options = menuList.getOptions();
for (WebElement option : options) {
optionValues.add(option.getText());
}
return optionValues;
}
public static boolean selectContainsOption(Select select, String optionText) {
for (WebElement option : select.getOptions()) {
if (option.getText().trim().equals(optionText)) {
return true;
}
}
return false;
}
static Set<String> getSelectValues(Select select) {
Set<String> roles = new HashSet<>();
for (WebElement option : select.getOptions()) {
roles.add(getTextFromElement(option).trim());
}
return roles;
}
public static Set<String> getSelectValues(Select select) {
Set<String> roles = new HashSet<>();
for (WebElement option : select.getOptions()) {
roles.add(getTextFromElement(option));
}
return roles;
}
/**
* Checks whether dropDown list with <b>dropDownName</b> equals to expected list <b>dropDownItems</b>
* <p>
* <b>dropDownName</b> is a <i><... name="any name"></i> attribute value identifying the dropDown list.
* </p>
* <b>Actions performed at this step:</b>
* <ul>
* <li>Checks that the dropDown list with specified <b>dropDownName</b> is present and returns it
* <li>Checks that expected list <b>dropDownItems</b> is equal to actual by size, list items sequence
* </ul>
* @param dropDownName Value of <b>name</b> attribute of actual dropDown list
* @param dropDownItems Expected list with isSelected state for list entries and entries text
* <table>
* <caption>A table of attributes</caption>
* <thead>
* <tr>
* <th><b>state </b></th><th><b>item</b></th>
* </tr>
* </thead> <tbody>
* <tr>
* <td>$state </td><td>$item1</td>
* </tr>
* <tr>
* <td>$state </td><td>$item2</td>
* </tr>
* <tr>
* <td>$state </td><td>$item3</td>
* </tr>
* </tbody>
* </table>
*/
@Then("a drop down with the name '$dropDownName' contains the items: $dropDownItems")
public void doesDropDownListContainItems(String dropDownName, ExamplesTable dropDownItems)
{
Select dropDown = isDropDownWithNameFound(dropDownName);
if (dropDown != null)
{
List<WebElement> actualItems = dropDown.getOptions();
List<Parameters> expectedItems = dropDownItems.getRowsAsParameters(true);
if (highlightingSoftAssert.assertEquals("Expected dropdown is of the same size as actual dropdown: ",
expectedItems.size(), actualItems.size()))
{
for (int i = 0; i < expectedItems.size(); i++)
{
WebElement option = actualItems.get(i);
Map<String, String> expectedRow = expectedItems.get(i).values();
highlightingSoftAssert.assertEquals(
String.format("Text of actual item at position [%s]", i + 1), expectedRow.get("item"),
webElementActions.getElementText(option));
highlightingSoftAssert.assertEquals(
String.format("State of actual item at position [%s]", i + 1),
Boolean.parseBoolean(expectedRow.get("state")), option.isSelected());
}
}
}
}
@Before
public void createAlarm() {
TestScenario.navigation.clickOnAddAlarm();
//Enter alarm name
WebElement name = driver.findElement(By.name("name"));
name.sendKeys(NAME);
LOGGER.info("Enter alarm name");
//Enter alarm description
WebElement description = driver.findElement(By.name("description"));
description.sendKeys(DESCRIPTION);
LOGGER.info("Enter alarm description");
//enter alarm graphite key
WebElement key = driver.findElement(By.name("graphite-key"));
key.sendKeys(GRAPHITE_KEY);
LOGGER.info("Enter alarm graphite key");
//go to step 2
Utils.clickWhenReady(driver, By.name("go-to-step-2"));
LOGGER.info("go to step 2");
//change windowMode
Select windowModes = new Select(driver.findElement(By.id("windowMode")));
List<WebElement> options = windowModes.getOptions();
for(WebElement option: options){
option.click();
String value = option.getAttribute("value");
if(value.equalsIgnoreCase("summarize")) {
assertTrue(driver.findElement(By.id("windowAggregation")).isDisplayed());
assertTrue(driver.findElement(By.id("timeUnitsNumber")).isDisplayed());
assertTrue(driver.findElement(By.id("windowUnits")).isDisplayed());
}
}
LOGGER.info("try all windowModes");
//go to step 3
Utils.clickWhenReady(driver, By.name("go-to-step-3"));
LOGGER.info("go to step 3");
//Enter warn Threshold
WebElement warnThreshold = driver.findElement(By.name("warn-threshold"));
warnThreshold.sendKeys(WARN_THRESHOLD);
LOGGER.info("Enter warning threshold");
//Enter error Threshold
WebElement errorThreshold = driver.findElement(By.name("error-threshold"));
errorThreshold.sendKeys(ERROR_THRESHOLD);
LOGGER.info("Enter error threshold");
//go to step 4
Utils.clickWhenReady(driver, By.id("go-to-step-4"));
LOGGER.info("Go to step 4");
//go to step 5
Utils.clickWhenReady(driver, By.id("go-to-step-5"));
LOGGER.info("Go to step 5");
//create alarm
new WebDriverWait(driver,Utils.DEFAULT_WAITING_TIME).until(ExpectedConditions.visibilityOf(driver.findElement(By.id("confirm-alarm-creation"))));
Utils.clickWhenReady(driver, By.id("confirm-alarm-creation"));
//wait redirect
new WebDriverWait(driver,Utils.DEFAULT_WAITING_TIME).until(ExpectedConditions.visibilityOfElementLocated(By.id("alarm-name-title")));
LOGGER.info("alarm is created");
}