【问题标题】:Select a particular option in Protractor在量角器中选择特定选项
【发布时间】:2016-03-08 04:32:50
【问题描述】:

我是一名新出生的 QA,我正在尝试编写 Protractor 脚本以从下拉列表中选择一个选项。我的下拉列表中有 2 个选项,我正在尝试从数字中选择它。

这是我正在使用的代码。

var selectDropdownbyNum = function ( element, optionNum ) {
    if (optionNum){
    var options = element.findElements(by.tagName('entity.company_id as entity.company_name for entity in entities'))   
      .then(function(options){
        options[1].click();
      });
    }
  };

页面加载时默认选择选项 2。我需要的是从下拉列表中选择选项 1。但是,我的代码没有这样做。

这里是选择选项的代码sn-p。

<select required="required" class="form-control empty ng-pristine ng-valid ng-not-empty ng-valid-required ng-touched" name="entity_id" ng-model="invoice.entity_id" ng-options="entity.company_id as entity.company_name for entity in entities" ng-required="true" ng-change="entitySelect(invoice.entity_id)">

  <option value="string:568f97841a4885e5de39900e" label="Option Global">Option Global</option>

  <option selected="selected" value="string:568f976a1a4885e5de39900d" label="Option Computer Studies">OptionComputer Studies</option>

</select>

提前致谢:)

【问题讨论】:

标签: javascript angularjs protractor


【解决方案1】:

var selectDropdownbyNum = function (dropDown, optionNumber) {

dropDown.click().then(function(){ element.all(by.tagName('entity.company_id as entity.company_name for entity in entity')).first().click(); }); };

【讨论】:

    【解决方案2】:

    找到了完美的答案。

    element.all(by.css('cssSelector of the dropdown')).each(function (eachElement, index) 
        {
           eachElement.click();// select the <select>
           browser.driver.sleep(500);// wait for the renderings to take effect
           element(by.css('cssSelector of option want to select')).click();// select the first md-option
           browser.driver.sleep(500);// wait for the renderings to take effect
       });
    

    这很平静:)

    【讨论】:

    • 你可以用browser.driver.sleep(500)代替browser.waitForAngular()
    • 使用 browser.sleep() 会延长测试运行时间是一种不好的做法。推荐使用 browser.wait()
    【解决方案3】:

    得到完美答案

    var selectOption = element(by.model("invoice.entity_id")).all(by.tagName('option')); selectOption.get(1).click();

    我们也可以通过每个函数获取所有的option值

    selectOption.each(function(item,index){

       //Resolve the promise
      item.getText().then(function(data){
           console.log(data+" "+index);
      });
    

    });

    【讨论】:

      猜你喜欢
      • 2016-06-30
      • 1970-01-01
      • 2016-01-29
      • 2012-10-19
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多