简体   繁体   中英

How to assert if selected option from drop down is choosen. Selenium. Page Object Model

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",??????);}

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM