检查SelectBox是否有items, Robot-Framework, Selenium2Library



如何查看<select>框中<option>是否大于1个

使用ajax加载<option> s并将其放置在两个<optgroup> s中。

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

您可以使用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

您至少有两个选择:

  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

最新更新