简体   繁体   English

选择了如何从下拉菜单中断言所选选项。 硒。 页面对象模型

[英]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.

相关问题 如何打印通过硒中的“ selectByVisibleText”方法选择的所选选项的文本 - How to print the text of the selected option choosen through 'selectByVisibleText' method in selenium 如何提交从jsp页面的下拉列表中选择的选项以执行mysql查询 - how to submit the option selected from drop down list in jsp page to perform a mysql query 从下拉框中选择相应的对象ID时,如何在模型中设置对象引用 - How to set object reference in model when respective object id is selected from drop down box 如何使用 div 标签/类从下拉列表中选择 select 选项?.selenium - How to select a option from drop down with div tag/class?.selenium 从 Java Selenium 的下拉菜单中选择一个选项 - Select an option from drop down in Java Selenium 从硒的隐藏下拉菜单中选择一个选项 - Select an option from hidden drop down in selenium 硒-如何基于不同下拉菜单中选择的值从下拉菜单中读取值 - Selenium - How to read values from drop down based on the value selected in a different drop down 如何在Selenium 2中选择/获取下拉选项 - How to select/get drop down option in Selenium 2 从硒的下拉列表中获取所选选项作为文本/字符串f? - Getting the selected option as text/string ffrom a drop-down in Selenium? 页 Object Model - 下拉菜单 - Page Object Model - drop down menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM