如何在ProtractorJS E2E测试中选择下拉选项

127

我正在尝试使用Protractor为Angular的端到端测试从下拉框中选择选项。

这是选择选项的代码片段:

<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();

这给我带来了以下错误:

指定了一个无效或非法的字符串 构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'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毫秒
构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'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'
会话ID:bdeb8088-d8ad-0f49-aad9-82201c45c63f
驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
功能 [{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}]


请问有谁能帮我解决这个问题或指点一下我在这里做错了什么。
34个回答

0
static selectDropdownValue(dropDownLocator,dropDownListLocator,dropDownValue){
    let ListVal ='';
    WebLibraryUtils.getElement('xpath',dropDownLocator).click()
      WebLibraryUtils.getElements('xpath',dropDownListLocator).then(function(selectItem){
        if(selectItem.length>0)
        {
            for( let i =0;i<=selectItem.length;i++)
               {
                   if(selectItem[i]==dropDownValue)
                   {
                       console.log(selectItem[i])
                       selectItem[i].click();
                   }
               }            
        }

    })

}

0
我们可以为此创建一个自定义的DropDown类,并添加一个方法,如下所示:
async selectSingleValue(value: string) {
        await this.element.element(by.xpath('.//option[normalize-space(.)=\'' + value + '\']')).click();
    }

此外,为了验证当前选择的值,我们可以使用以下代码:

async getSelectedValues() {
        return await this.element.$('option:checked').getText();
    }

-1

您可以像这样选择下拉选项:

element(by.xpath(
'//*[@id="locregion"]//option[contains(text(),"Ranjans Mobile Testing")]'
)).click();

-1

按 CSS 属性选择选项

element(by.model("organization.parent_id")).element(by.css("[value='1']")).click();

或者

element(by.css("#locregion")).element(by.css("[value='1']")).click();

其中locregion(id)和organization.parent_id(模型名称)是选择元素的属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接