[英]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. 我想选择“阿拉巴马州”,并使用assert来检查是否实际上选择了“阿拉巴马州”。 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. 在Select类中,有一个名为“ getFirstSelectedOption()”的方法,该方法将从下拉列表中返回所选的Web元素选项。 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)
; 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... 希望对您有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.