选择量角器中的第一个可见元素

2024-01-26

我正在编写量角器测试并且喜欢它,尽管有时似乎会陷入一些看起来应该很简单的事情。例如,我想循环浏览其中一个页面上包含“提名”文本的所有按钮。页面上有几十个,但只有 1 或 2 个可见。所以我想点击第一个。这是我当前使用的代码:

    var nominateButtons = element.all(by.buttonText('Nominate'));
    nominateButtons.then(function(els){
        for(var x = 0;x < els.length;x++){
            //Since isDisplayed returns a promise, I need to do it this way to get to the actual value
            els[x].isDisplayed().then(function(isVisible){
                //isVisible now has the right value                 
                if(isVisible){
                    //But now els is not defined because of the scope of the promise!!
                    els[x].click();
                }
            });
        }
    });

当我运行此代码时,我收到“无法调用未定义的方法单击”错误,因为 els[x] 不再在范围内,但我似乎无法在不使用承诺的情况下检查可见性。所以我的问题是,如何循环遍历元素集合,检查它们的可见性,然后单击第一个可见的元素? (我试图不使用期望来检查可见性,因为我知道大多数按钮都不可见)

提前致谢


els被定义为。没有定义的是x。 最简单的方法是:

var nominateButtons = element.all(by.buttonText('Nominate'));
var displayedButtons = nominateButtons.filter(function(elem) {
   return elem.isDisplayed(); 
});
displayedButtons.first().click();

or

element.all(by.buttonText('Nominate')).
  filter(function(elem) {
    return elem.isDisplayed(); 
  }).
  first().
  click();

编辑,顺便说一句,您不应该依赖此行为(单击文本“提名”的第一个按钮),因为当您更改应用程序时,它会导致问题。看看您是否可以按 ID 选择,或选择更具体的“提名”,例如element(by.css('the section the nominate is under')).element(by.buttonText('Nominate'));

再次编辑:参见使用带有循环的量角器 https://stackoverflow.com/questions/27910331/using-protractor-with-loops/27910350用于解释

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

选择量角器中的第一个可见元素 的相关文章

随机推荐