AngularJS +量角器基于它的文本不是价值如何选择下拉选项 [英] AngularJS + Protractor How to select Dropdown option based on its text not value

查看:130
本文介绍了AngularJS +量角器基于它的文本不是价值如何选择下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上项目通过点击它的文本,而不是由它的下拉框中的值。

我发现这个伟大的职位:的https://$c$crwall.com/p/tjx5zg ,但它不按预期工作,搜索永远继续比赛后发现,它不单击项目,

如果有人有更好的例子(一个正在运行的),或者可以解决此问题code和使其工作,

我会apperciate。

这是code丹哈勒从使用后(预留给他的一切权利)

 函数selectOption(选择,项目){
    VAR选择列表,desiredOption;    选择列表= this.findElement(选择);
    selectList.click();    selectList.findElements(protractor.By.tagName('选项'))
        。然后(功能findMatchingOption(选项){
            options.some(功能(选件){
                option.getText()。然后(功能doesOptionMatch(文本){
                    如果(项目===文本){
                        desiredOption =选项;
                        返回true;
                    }
                });
            });
        })
        。然后(功能clickOption(){
            如果(desiredOption){
                desiredOption.click();
            }
        });
    }

这是我可以用这样的选择项功能:

  VAR浏览器= protractor.getInstance();
browser.selectOption = selectOption.bind(浏览器);
browser.selectOption(protractor.By.id(我的-下拉'),'我的价值');


解决方案

此功能选择您的选择框的第3个选项。

 函数selectDropdownByNumber(元素,指数,单位为毫秒){
    element.findElements(by.tagName('选项'))
      。然后(功能(选件){
        选项​​[指数]。点击();
      });
    如果(typeof运算毫秒!==未定义){
      browser.sleep(毫秒);
   }
}
VAR mySelect = $('#我-下拉');
selectDropdownByNumber(mySelect,2);

更多信息可以在这里找到 - 的http://qainsight.blogspot.fr/2014/04/select-dropdown-value-in-protractor-js.html

I want to click on item by it's text and not by it's value from dropdown box.

i found this great post : https://coderwall.com/p/tjx5zg but it doesn't work as expected, the search continue forever after match was found and it is not clicking the item,

if someone have better example (a working one) or can fix this code and make it work,

i will apperciate.

This is the code Dan Haller from the post used (all rights reserved to him)

function selectOption(selector, item){
    var selectList, desiredOption;

    selectList = this.findElement(selector);
    selectList.click();

    selectList.findElements(protractor.By.tagName('option'))
        .then(function findMatchingOption(options){
            options.some(function(option){
                option.getText().then(function doesOptionMatch(text){
                    if (item === text){
                        desiredOption = option;
                        return true;
                    }
                });
            });
        })
        .then(function clickOption(){
            if (desiredOption){
                desiredOption.click();
            }
        });
    }

This is a select item function that I can use like this:

var browser = protractor.getInstance();
browser.selectOption = selectOption.bind(browser);
browser.selectOption(protractor.By.id('my-dropdown'), 'My Value');

解决方案

This function selects the 3rd option of your select box.

function selectDropdownByNumber(element, index, milliseconds) {
    element.findElements(by.tagName('option'))
      .then(function(options) {
        options[index].click();
      });
    if (typeof milliseconds !== 'undefined') {
      browser.sleep(milliseconds);
   }
}
var mySelect = $('#my-dropdown');
selectDropdownByNumber(mySelect, 2);

More info can be found here - http://qainsight.blogspot.fr/2014/04/select-dropdown-value-in-protractor-js.html

这篇关于AngularJS +量角器基于它的文本不是价值如何选择下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆