Selenium IDE Drop Downlist框有一定值Loop?

时间:2013-04-17 13:05:19

标签: javascript loops selenium ide

如何使用selenium IDE和JavaScript测试某些项目(不是全部)的下拉框并进行循环直到列表结束?指针或建议?

1 个答案:

答案 0 :(得分:0)

鉴于您有以下下拉框:

<select id="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

并且您想测试它是否包含“volvo”和“opel”值,那么您可以在storeEval selenium命令中使用以下JavaScript代码:

(function(document, searchArray) {
    var dropDownBox = document.getElementById('cars');
    var dropDownBoxOptions = dropDownBox.getElementsByTagName('option');

    var valuesFound = 0;
    for(var i in searchArray) {
        for(var y=0; y<dropDownBoxOptions.length; y++) {
            if (dropDownBoxOptions[y].getAttribute('value') == searchArray[i]) {
                valuesFound++;
                break;
            }
        }
    }

    return valuesFound == searchArray.length;
})(selenium.browserbot.getCurrentWindow().document, ['volvo', 'opel']);

以上是Selenium测试用例的来源:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.google.com.tr/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
    <td>storeEval</td>
    <td>(function(document, searchArray) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var dropDownBox = document.getElementById('cars');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var dropDownBoxOptions = dropDownBox.getElementsByTagName('option');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var valuesFound = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(var i in searchArray) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(var y=0; y&lt;dropDownBoxOptions.length; y++) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (dropDownBoxOptions[y].getAttribute('value') == searchArray[i]) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;valuesFound++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return valuesFound == searchArray.length; })(selenium.browserbot.getCurrentWindow().document, ['volvo', 'opel']);</td>
    <td>result</td>
</tr>
<tr>
    <td>echo</td>
    <td>${result}</td>
    <td></td>
</tr>
</tbody></table>
</body>
</html>