下面列出了org.openqa.selenium.support.ui.Select#getFirstSelectedOption ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void checkElementTypeBeforeProcessing() {
if (Element != null) {
if (Element.getTagName().equalsIgnoreCase("select")) {
Select select = new Select(Element);
Element = select.getFirstSelectedOption();
System.out.println("As it is Select Element assserting "
+ "the text of first selected Element");
}
}
}
/**
* checks if the image with the <b>orderLabel</b> is selected
*
* @param oderLabel
* @return true if the if the image with the <b>orderLabel</b> is selected
*/
public boolean isImageSelected(String oderLabel) {
By selector = By.cssSelector(SELECTBOX_SELECTOR);
WebElement element = this.getDriver().findElement(selector);
Select select = new Select(element);
return select != null && select.getFirstSelectedOption() != null
&& select.getFirstSelectedOption().getText().equalsIgnoreCase(oderLabel);
}
/**
* Selects the option by visible text and returns whether the dropdown value has changed.
*
* @throws AssertionError if the selected option is not the one we wanted to select
*
* @see Select#selectByVisibleText(String)
*/
boolean selectDropdownByVisibleValue(WebElement element, String text) {
Select select = new Select(element);
WebElement originalSelectedOption = select.getFirstSelectedOption();
select.selectByVisibleText(text);
WebElement newSelectedOption = select.getFirstSelectedOption();
assertEquals(text, newSelectedOption.getText().trim());
return !newSelectedOption.equals(originalSelectedOption);
}
/**
* Selects the option by value and returns whether the dropdown value has changed.
*
* @throws AssertionError if the selected option is not the one we wanted to select
*
* @see Select#selectByValue(String)
*/
boolean selectDropdownByActualValue(WebElement element, String value) {
Select select = new Select(element);
WebElement originalSelectedOption = select.getFirstSelectedOption();
select.selectByValue(value);
WebElement newSelectedOption = select.getFirstSelectedOption();
assertEquals(value, newSelectedOption.getAttribute("value"));
return !newSelectedOption.equals(originalSelectedOption);
}