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{
.....
}
2 comments:
hi,
I am trying to implement a listbox and I would like to read a specific item from the list like ex- item(2). How do we do that?
Hope you can try as:
public void getItemfromListBox(WebElement listbox,int position){
List options = listbox.findElements(By.tagName("option"));
return options.get(position).getText();
}
Post a Comment