简体   繁体   English

如何在IE的下拉列表中验证所选选项

[英]How to verify the selected option in a dropdown in IE

I'm having some trouble verifying that an option is selected in a dropdown. 我在验证是否在下拉列表中选择了一个选项时遇到了一些麻烦。 In my application, I have a dropdown with 10 options. 在我的应用程序中,我有一个包含10个选项的下拉菜单。 If I select option 5, I want to verify that it was actually selected. 如果选择选项5,则要验证它是否已被实际选择。

Using Firefox and Chrome, its very simple. 使用Firefox和Chrome,它非常简单。 Below is the HTML for my dropdown and the options. 以下是我的下拉菜单和选项的HTML。

<select class="Test-field-ddlist" runat="server" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" name="ctl00$cphMainContent$ctl17">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected="selected">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>

To verify the selected option, I created an object using the CSS selector. 为了验证所选的选项,我使用CSS选择器创建了一个对象。 I then verified that the text is equal to 5. 然后,我验证了该文本等于5。

public static final String ManagerPage_DropdownSelection = "css=.Test-field-ddlist option[selected]";

String actualtext = driver.getSingleElement(ManagerPage_DropdownSelection).getText();
Assert.assertEquals(actualtext, "5");

The problem I'm having with IE is that the HTML is different. IE的问题在于HTML是不同的。 When I inspect the element, the HTML looks like this. 当我检查元素时,HTML看起来像这样。 There is nothing that says 5 is selected 没有什么说5被选中

<SELECT name=ctl00$cphMainContent$ctl17 class=Test-field-ddlist onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" runat="server" sizcache="0" sizset="0"> 
<OPTION value=1>1</OPTION> 
<OPTION value=2>2</OPTION> 
<OPTION value=3>3</OPTION> 
<OPTION value=4>4</OPTION> 
<OPTION value=5>5</OPTION> 
<OPTION value=6>6</OPTION> 
<OPTION value=7>7</OPTION> 
<OPTION value=8>8</OPTION> 
<OPTION value=9>9</OPTION> 
<OPTION value=10>10</OPTION>

It looks like the "selected" text is hidden. 看起来“选定”文本已隐藏。 If I copy and past the HTML into a text editor, I can then see the text "selected". 如果将HTML复制并粘贴到文本编辑器中,则可以看到文本“已选择”。 Unlike Firefox and Chrome, there is not an attribute of selected. 与Firefox和Chrome不同,它没有selected属性。 Instead, selected text is added to the value attribute. 而是将选定的文本添加到value属性。

<SELECT name=ctl00$cphMainContent$ctl17 class=Test-field-ddlist onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphMainContent$ctl17\',\'\')', 0)" runat="server" sizcache="0" sizset="0"> 
<OPTION value=1>1</OPTION> 
<OPTION value=2>2</OPTION> 
<OPTION value=3>3</OPTION> 
<OPTION value=4>4</OPTION> 
<OPTION value=5 selected>5</OPTION> 
<OPTION value=6>6</OPTION> 
<OPTION value=7>7</OPTION> 
<OPTION value=8>8</OPTION> 
<OPTION value=9>9</OPTION> 
<OPTION value=10>10</OPTION> 

Can I create a selector that will find the selected dropdown option in all browsers? 我可以创建一个选择器来在所有浏览器中找到选定的下拉选项吗? Or is there a better way to verify the selected value of a dropdown? 还是有更好的方法来验证下拉列表的选定值? I have tried the below method. 我尝试了以下方法。 It seems to work, but takes a very long time to complete (about 5 minutes). 它似乎可以工作,但是需要很长时间才能完成(大约5分钟)。

protected void verifyDropDownSelection(String selector, String expectedvalue) {
        List<String> listA = new ArrayList<String>();
        listA.add(expectedvalue);
        List<String> listB = new ArrayList<String>();
        List<Element> DDSelected = driver.getSingleElement(selector).useAsDropdown().getAllSelectedOptions();
            for(Element selectedoption : DDSelected) {
                String actualtext = selectedoption.getText();
                listB.add(actualtext);
            }
        log.info("INFO: Verifying the selected option in the dropdown");
        Assert.assertEquals(listB, listA);
        log.info("PASS: "+expectedvalue+" was the selected option in the dropdown");
    }

I would let the Select class handle the case: 我将让Select处理这种情况:

WebElement select = driver.findElement(By.name("ctl00$cphMainContent$ctl17"));
Select dropDown = new Select(select);  

String selected = dropDown.getFirstSelectedOption().getText();
Assert.assertEquals(selected, "5");

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

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