【问题标题】:Using Protractor, how do I get a random select option other that the currently selected one?使用量角器,我如何获得除当前选择之外的随机选择选项?
【发布时间】:2016-01-22 05:21:51
【问题描述】:

我想处理一个选择菜单的测试场景,其中规范会选择一个随机选项,而不是当前选择的选项。我似乎找不到可以让我这样做的有效定位器或方法。

  1. 策略1:获取option[selected]的当前索引并从其他索引中随机选择。
  2. 策略 2:获取未选择的选项by.css('option:not([selected])') - 获取数组的长度并随机选择一个。此选择器似乎忽略了 :not 部分并返回选项总数。

由于我是量角器的新手,所以看着API,我真的没有办法做到这一点。请问有什么提示吗?

【问题讨论】:

    标签: angularjs protractor


    【解决方案1】:

    首先,让我们使用.filter()过滤未选择的选项:

    var nonSelectedOptions = $$("option").filter(function (option) {
        return option.isSelected().then(function (isSelected) {
            return !isSelected;
        });;
    });
    

    现在,我们需要一个索引列表来从中随机选择一个,让我们使用.map()

    var indexes = nonSelectedOptions.map(function (option, index) {
        return index;
    });
    

    现在,我们需要解决 indexes 承诺以获取索引数组并使用来自 this answer 的“从数组中选择随机项”解决方案。我们将使用.get() 为随机选择的索引获取特定选项:

    indexes.then(function (indexes) {
        var randomIndex = indexes[Math.floor(Math.random()*indexes.length)];
    
       var randomOption = nonSelectedOptions.get(randomIndex);
    
       // now select the randomOption
    });
    

    如果您使用建议的包装器here,选项选择代码将是:

    randomOption.getAttribute("value").then(function (optionValue) {
        mySelect.selectByValue(optionValue);
    });
    

    未测试。

    【讨论】:

    • 像魅力一样工作,谢谢!当我有更多的基础时,我会尽快探索包装器的领域。我当然会在您提到的包装器中添加 SelectWrapper.prototype.selectAnother 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多