Reputation: 113
I need to select option from drop list and check if this option is choosen / visible for user. Code for choosing option:
@FindBy(how = How.ID, using = "id_state")
public WebElement StateDropDown;
public void ChooseState(String index){
Select Choose = new Select(StateDropDown);
Choose.selectByVisibleText(index);
This is my drop down:
<select name="id_state" id="id_state" class="form-control">
<option value="">-</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option </select>
I want to choose 'Alabama'and use assert to check if 'Alabama' is in fact choosen. I understand that I have to write a function which sends a name of choosen state to string. Assert it's going to be included here:
@Then("^I see \"([^\"]*)\" in dropdown$")
public void iSeeInDropdown(String state)
Assert.assertEquals("Alabama",??????);}
Upvotes: 1
Views: 5346
Reputation: 1689
In Select class, there is a method called 'getFirstSelectedOption()' which will return the selected web element option from the drop down. By using this method, you can retrieve the option like below:
Select select = new Select(someElement);
String option = select.getFirstSelectedOption().getText();
The you can assert the condition like below:
Assert.assertEquals("Alabama", option)
;
Try to follow the below steps,
Add the below method in the page objects class :
public String getSelected() {
return new Select(StateDropDown).getFirstSelectedOption().getText().trim();
}
And modify this step definition method like below, which will call the method of the page objects class and then get the selected option and assert it.
@Then("^I see \"([^\"]*)\" in dropdown$") {
public void iSeeInDropdown(String state)
String selectedOption = new PageObjects().getSelected();
Assert.assertEquals("Alabama", selectedOption);
}
I hope it helps...
Upvotes: 3