我试图用protractor从angular e2e测试的下拉菜单中选择一个选项。
以下是选择选项的代码片段:
<select id="locregion" class="create_select ng-pristine ng-invalid ng-invalid-required" required="" ng-disabled="organization.id !== undefined" ng-options="o.id as o.name for o in organizations" ng-model="organization.parent_id">
<option value="?" selected="selected"></option>
<option value="0">Ranjans Mobile Testing</option>
<option value="1">BeaverBox Testing</option>
<option value="2">BadgerBox</option>
<option value="3">CritterCase</option>
<option value="4">BoxLox</option>
<option value="5">BooBoBum</option>
</select>
我已经试过了:
ptor.findElement(protractor.By.css('select option:1')).click();
这给了我以下错误:
指定了一个无效的或非法的字符串 Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'; 系统信息:os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9', java.version: ' 1.6.0_65'; 驱动程序信息: driver.version: unknown
我也试过了:
ptor.findElement(protractor.By.xpath('/html/body/div[2]/div/div[4]/div/div/div/div[3]/ng-include/div/div[2]/div/div/organization-form/form/div[2]/select/option[3]')).click();
这给了我以下错误:
ElementNotVisibleError:元素当前不可见,所以不能与之交互。 命令持续时间或超时:9毫秒 Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'; 系统信息:os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9', java.version: ' 1.6.0_65'; Session ID: bdeb8088-d8ad-0f49-aad9-82201c45c63f 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=24.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true} ]
谁能帮助我解决这个问题,或者告诉我,我可能做错了什么。
我也有类似的问题,最后写了一个辅助函数,选择下拉值。
我最终决定,我可以通过选项编号进行选择,因此写了一个方法,接收一个元素和选项编号,并选择该选项编号。 如果optionNumber是空的,它就不选择任何东西(让下拉菜单不被选择)。
var selectDropdownbyNum = function ( element, optionNum ) {
if (optionNum){
var options = element.all(by.tagName('option'))
.then(function(options){
options[optionNum].click();
});
}
};
如果你想了解更多细节,我写了一篇博文,其中还包括验证下拉菜单中所选选项的文本:http://technpol.wordpress.com/2013/12/01/protractor-and-dropdowns-validation/
你可以试试这个,希望它能发挥作用
element.all(by.id('locregion')).then(function(selectItem) {
expect(selectItem[0].getText()).toEqual('Ranjans Mobile Testing')
selectItem[0].click(); //will click on first item
selectItem[3].click(); //will click on fourth item
});