下面列出了org.openqa.selenium.support.ui.Select#selectByValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public WebElement selectValue( String locator, String value, boolean scrollToView )
{
int count = 5;
boolean check = true;
WebDriverWait wait = new WebDriverWait( getWebDriver( ), 10 );
WebElement element = null;
while(check && count-->0)
{
try
{
element = findElement( locator );
List<WebElement> elementList = new ArrayList<>( );
elementList.add( element );
wait.until( ExpectedConditions.visibilityOfAllElements( elementList ) );
check=false;
} catch (Throwable e) {
logger.info("Waiting for select element {} to be visible", locator);
}
}
Select select = new Select(element);
select.selectByValue( value );
return element;
}
@Test
public void test03definePublishOption() throws Exception {
tester.visitPortal(PortalPathSegments.DEFINE_PUBLISHOPTION);
tester.waitForElement(By.id(
PortalHtmlElements.DEFINE_PUBLISH_OPTION_DROPDOWN_SERVICENAME),
10);
Select dropdownServiceName = new Select(tester.getDriver().findElement(
By.id(PortalHtmlElements.DEFINE_PUBLISH_OPTION_DROPDOWN_SERVICENAME)));
dropdownServiceName
.selectByVisibleText(PlaygroundSuiteTest.marketServiceName);
Select dropdownMarketplace = new Select(tester.getDriver().findElement(
By.id(PortalHtmlElements.DEFINE_PUBLISH_OPTION_DROPDOWN_MARKETPLACE)));
dropdownMarketplace.selectByValue(PlaygroundSuiteTest.supplierOrgId);
tester.waitForElementVisible(
By.id(PortalHtmlElements.DEFINE_PUBLISH_OPTION_BUTTON_SAVE),
10);
tester.clickElement(
PortalHtmlElements.DEFINE_PUBLISH_OPTION_BUTTON_SAVE);
assertTrue(tester.getExecutionResult());
}
public static String selectOperation(WebElement we, String operation, String operationValue) {
String result = "";
// �����������
Select select = new Select(we);
// �����������¼�
switch (operation) {
case "selectbyvisibletext":
select.selectByVisibleText(operationValue);
result = "���������ͨ��VisibleText����ѡ��...��VisibleText����ֵ:" + operationValue + "��";
LogUtil.APP.info("���������ͨ��VisibleText����ѡ��...��VisibleText����ֵ:{}��",operationValue);
break;
case "selectbyvalue":
select.selectByValue(operationValue);
result = "���������ͨ��Value����ѡ��...��Value����ֵ:" + operationValue + "��";
LogUtil.APP.info("���������ͨ��Value����ѡ��...��Value����ֵ:{}��",operationValue);
break;
case "selectbyindex":
select.selectByIndex(Integer.parseInt(operationValue));
result = "���������ͨ��Index����ѡ��...��Index����ֵ:" + operationValue + "��";
LogUtil.APP.info("���������ͨ��Index����ѡ��...��Index����ֵ:{}��",operationValue);
break;
case "isselect":
result = "��ȡ����ֵ�ǡ�" + we.isSelected() + "��";
LogUtil.APP.info("�ж϶����Ƿ��Ѿ���ѡ��...�����ֵ:{}��",we.isSelected());
break;
default:
break;
}
return result;
}
/**
* When selecting different options in the dropdown menu that controls the numbers of displayed rows.
*/
@Test
public void shouldShowTheCorrectNumberOfRowsSelectedByLength() {
FreeStyleJob job = createFreeStyleJob("findbugs-severities.xml");
job.addPublisher(IssuesRecorder.class, recorder -> recorder.setToolWithPattern("FindBugs", "**/*.xml"));
job.save();
Build build = job.startBuild().waitUntilFinished();
build.open();
AnalysisSummary resultPage = new AnalysisSummary(build, "findbugs");
assertThat(resultPage).isDisplayed();
AnalysisResult findBugsAnalysisResult = resultPage.openOverallResult();
assertThat(findBugsAnalysisResult).hasAvailableTabs(Tab.ISSUES);
findBugsAnalysisResult.openPropertiesTable(Tab.ISSUES);
Select issuesLengthSelect = findBugsAnalysisResult.getLengthSelectElementByActiveTab();
issuesLengthSelect.selectByValue("10");
WebElement issuesInfo = findBugsAnalysisResult.getInfoElementByActiveTab();
waitUntilCondition(issuesInfo, "Showing 1 to 10 of 12 entries");
WebElement issuesPaginate = findBugsAnalysisResult.getPaginateElementByActiveTab();
List<WebElement> issuesPaginateButtons = issuesPaginate.findElements(By.cssSelector("ul li"));
assertThat(issuesPaginateButtons.size()).isEqualTo(2);
assertThat(ExpectedConditions.elementToBeClickable(issuesPaginateButtons.get(1)));
issuesLengthSelect.selectByValue("25");
waitUntilCondition(issuesInfo, "Showing 1 to 12 of 12 entries");
issuesPaginateButtons.clear();
issuesPaginateButtons = issuesPaginate.findElements(By.cssSelector("ul li"));
assertThat(issuesPaginateButtons.size()).isEqualTo(1);
}
public void setSelectOptionOnView(String viewName, String elementName, String optionText) throws IllegalArgumentException, IllegalAccessException {
String checkValue = getReferenceService().namespaceString(optionText);
WebElement elementFromReferencedView = seleniumElementService.getElementFromReferencedView(viewName, elementName);
Select select = new Select(elementFromReferencedView);
try{
select.selectByVisibleText(checkValue);
}
catch(NoSuchElementException nse) {
//could mean the value to use is an actual value
select.selectByValue(checkValue);
}
}
@Override
public boolean selectByValue(Element element, String value)
{
Select select = createSelect(element);
if(select != null)
{
select.selectByValue(value);
return true;
}
return false;
}
@Override
public void run(RecordingSimulatorModule recordingSimulatorModule) throws InterruptedException {
String baseUrl = (String) opts.get(URL);
WebDriver driver = recordingSimulatorModule.getWebDriver();
driver.get(baseUrl + TestUtils.Pages.SELECT_VALUE_DEMO_PAGE);
Select select = new Select(driver.findElement(By.id("select-element")));
select.selectByValue("first");
Thread.sleep(1000);
}
@Override
public void run() {
super.run();
By locator = this.readLocatorArgument("locator");
String optionValue = this.readStringArgument("optionValue", null);
String optionText = this.readStringArgument("optionText", null);
Integer optionNumber = this.readIntArgument("optionNumber", null);
this.waitForAsyncCallsToFinish();
Select dropdownElement = new Select(this.getElement(locator));
if (optionValue != null) {
dropdownElement.selectByValue(optionValue);
} else if (optionText != null) {
dropdownElement.selectByVisibleText(optionText);
} else if (optionNumber != null) {
dropdownElement.selectByIndex(optionNumber - 1);
} else {
throw new RuntimeException(
"You must identify the option you want to select from the "
+ "list by providing one of the following arguments: "
+ "optionValue, optionText or optionNumber.");
}
}
/**
* Selects student nationality from the dropdown list if the nationality is
* valid, otherwise it fails with a message.
*/
public void selectNationality(String studentNationality) {
if (NationalityHelper.getNationalities().contains(studentNationality) || "".equals(studentNationality)) {
Select dropdown = new Select(studentNationalityDropdown);
dropdown.selectByValue(studentNationality);
} else {
fail("Given nationality " + studentNationality + " is not valid!");
}
}
private void setTimeout(Select timeoutElement, WebElement unitElement,
int timeout, TimeUnit unit) {
timeoutElement.selectByValue(capitalize(unit.name().toLowerCase()));
UIUtils.setTextInputValue(unitElement, valueOf(timeout));
}
@When("I choose the two column layout")
public void selectTwoColumnLayout() {
final Select layouts = new Select(portal.findElement(By.id("pageLayout")));
layouts.selectByValue("columns_2");
}
public void SelectUsingValue(By locator,String value) {
Select select = new Select(getElement(locator));
select.selectByValue(value);
oLog.info("Locator : " + locator + " Value : " + value);
}
public void showEntries(final EntryCount entryCount) {
final Select select = new Select(findElement(By.cssSelector(".dataTables_length select")));
select.selectByValue(entryCount.value);
}
public void editType() {
Select typeEdited = new Select(webDriver.findElement(By.className("editTypeField")));
typeEdited.deselectAll();
typeEdited.selectByValue("ADMIN");
}
public void viewType() {
Select typeViewed = new Select(webDriver.findElement(By.className("viewTypeField")));
typeViewed.deselectAll();
typeViewed.selectByValue("ADMIN");
}
public void usageType() {
Select typeUsed = new Select(webDriver.findElement(By.className("usageTypeField")));
typeUsed.deselectAll();
typeUsed.selectByValue("OPENID");
}
@When("I choose the status as \"$status\"")
public void changeStatus(String status) {
final Select relationshipStatus = new Select(portal.findElement(By.id("statusField")));
relationshipStatus.selectByValue(status);
}
private void selectNewTimeZone(String timeZone) {
Select dropdown = new Select(timeZoneDropDown);
dropdown.selectByValue(timeZone);
}
private void selectNewTimeZone(String timeZone) {
Select dropdown = new Select(timeZoneDropdown);
dropdown.selectByValue(timeZone);
}
/**
* Selects in the dropdown (select) element with the given id the option
* with the given value.
*
* @param id
* the element id
* @param value
* the option value
* @throws NoSuchElementException
* if element is not present
*/
public void selectDropdown(String id, String value) {
Select select = new Select(driver.findElement(By.id(id)));
select.selectByValue(value);
}