简体   繁体   English

Selenium / Select Dropdrown /断言是否存在某个值

[英]Selenium / Select Dropdrown / assert that a certain value is present or not

I have two Select webelements. 我有两个Select webelements。 The first one is depending on the second one. 第一个取决于第二个。 For example: If you chose interestPeriod with value 20 , loanTermSelect does not offer the value 10 . 例如:如果你选择了interestPeriod值为20loanTermSelect不提供值10 And that is exactly what I want to check with a selenium webtest. 而这正是我要使用Selenium Webtest检查的内容。

My code: 我的代码:

@Test
public void testLoanTerm20() {
    kqcPage = loginToFinancialPlanet().openKonditionenQuickcheckPage();
    kqcPage = kqcPage.fillForm(getLoanTerm20());
}

private Map<String, Object> getLoanTerm20() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("addTypeOfLoan", "KfW Energieeff. Bauen (153)");
    map.put("interestPeriod", "20");
    map.put("loanTermSelect", "25");
    return map;
}

Usually I'm using something like the following code to assert, that a given element is visible. 通常,我使用类似以下代码的内容来断言,给定的元素是可见的。 But this time I want to check, if a given element offers a specific value. 但是这次我要检查给定元素是否提供特定值。

assertThat(extranetRulesVersionDisplayBegriffMultiSumme.getButtonDelete().isDisplayed(), is(true));

What's the best practise? 最佳做法是什么? Help is very appreciated. 非常感谢您的帮助。

To get all the options inside a Select class you can use:- 要在Select类中获取所有选项,可以使用:-

Select select = new Select(driver);
List<WebElement> allOptions = select.getOptions();

It will return a list of all the options inside the dropdown then you can iterate on the dropdown for fetching the text. 它将返回下拉菜单中所有选项的列表,然后您可以在下拉菜单中进行迭代以获取文本。

for(WebElement el : allOptions) {
    // So you can get the text like:-
    String text = el.getText();
    if(text.equals("specific value")) {
        // Your operation that you want to do once string matches.
    }
}

This way you can match the specific value you are looking for. 这样,您可以匹配要查找的特定值。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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