0

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

4

1 回答 1

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>
于 2013-04-18T21:31:45.650 回答