1. Select value from a listbox
public void selectValue(WebElement listbox, String value) {
if (listbox != null && !value.isEmpty()) {
List listValues = listbox.findElements(By.tagName("option"));
for (WebElement option : listValues) {
if (option.getText().equals(value)) {
option.setSelected();
break;
}
}
}
}
2. Get selected value from listbox
public String getSelectedValue(WebElement listbox) throws MyException {
if (listbox == null) {
throw new MyException("listboxValues = " + listbox);
}
List options = listbox.findElements(By.tagName("option"));
for (WebElement option : options) {
if (option.isSelected())
return option.getText();
}
return null;
}
3. Verfiy for a value in listbox
public boolean verifyListboxValue(WebElement listbox,String value) throws IllegalArgumentException {
if (listbox == null) {
throw new IllegalArgumentException("listboxValues = " + listbox);
}
List options = listbox.findElements(By.tagName("option"));
for (WebElement option : options) {
if (option.getText().equals(value))
return true;
}
return false;
}
class MyException extends Exception{
.....
}
public void selectValue(WebElement listbox, String value) {
if (listbox != null && !value.isEmpty()) {
List
for (WebElement option : listValues) {
if (option.getText().equals(value)) {
option.setSelected();
break;
}
}
}
}
2. Get selected value from listbox
public String getSelectedValue(WebElement listbox) throws MyException {
if (listbox == null) {
throw new MyException("listboxValues = " + listbox);
}
List
for (WebElement option : options) {
if (option.isSelected())
return option.getText();
}
return null;
}
3. Verfiy for a value in listbox
public boolean verifyListboxValue(WebElement listbox,String value) throws IllegalArgumentException {
if (listbox == null) {
throw new IllegalArgumentException("listboxValues = " + listbox);
}
List
for (WebElement option : options) {
if (option.getText().equals(value))
return true;
}
return false;
}
class MyException extends Exception{
.....
}