【问题标题】:Check whether SelectBox has items, Robot-Framework, Selenium2Library检查 SelectBox 是否有项目、Robot-Framework、Selenium2Library
【发布时间】:2014-06-26 22:21:21
【问题描述】:

如何检查<select> 框是否有超过1 个<option>

<option>s 使用 ajax 加载并被放入两个 <optgroup>s。

<option value="">default</option>
<optgroup ...>
    <option value=..>...
    <option ..>...
    ...
</optgroup>
<optgroup ...>
    <option ..>...
    <option ..>...
    ...

【问题讨论】:

    标签: selenium-webdriver robotframework


    【解决方案1】:

    您可以使用Get List Items 返回所有项目的列表,然后使用Get Length 获取列表中的元素数量:

    Select Options Test
        Open Browser      your_url            chrome
        @{items}          Get List Items      id=select_list_id
        ${list_length}    Get Length          ${items}
        Should Be True    ${list_length} > 1
    

    【讨论】:

    • 我喜欢这个简单的答案!
    【解决方案2】:

    你至少有两个选择:

    1. 使用Get Matching Xpath Count 关键字返回与代表选项的xpath 匹配的数字元素,或者
    2. 使用Get List Items 获取所有项目,然后使用Get Length 返回长度。

    由于我无法针对您的确切代码编写示例,因此以下示例针对http://the-internet.herokuapp.com/dropdown 的页面运行。它有包含这个的标记(在我写这个答案的时候):

    <select id='dropdown'>
        <option value="" disabled="disabled" selected="selected">Please select an option</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>
    

    这是一个完整的工作示例:

    *** Test Cases ***
    | Example 
    | | 
    | | Go to | ${ROOT}/dropdown
    | | 
    | | ${count}= | Get matching xpath count | //select[@id='dropdown']/option
    | | Should be equal as numbers | ${count} | 3
    | | 
    | | @{items}= | Get list items | //select[@id='dropdown']
    | | ${item count}= | Get Length | ${items}
    | | Should be equal as numbers | ${item count} | 3
    
    *** Settings ***
    | Library        | Selenium2Library
    | Suite Setup    | Open browser | ${ROOT} | ${BROWSER}
    | Suite Teardown | Close all browsers
    
    *** Variables ***
    | ${BROWSER} | chrome
    | ${ROOT}    | http://the-internet.herokuapp.com
    

    【讨论】:

    • 感谢您提供替代答案(在您编辑后:)),这也非常有用。为你的时间点赞! :).
    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2017-02-21
    • 2017-04-07
    • 2016-09-08
    • 2015-08-12
    相关资源
    最近更新 更多