废弃一些数组值以进行断言(Protractor)

时间:2017-06-06 13:11:28

标签: javascript arrays automation protractor gulp-protractor

有没有办法废弃或删除element.all数组返回的随机值或少量值?

我尝试自动化的方案是检查从数组列表返回的每一行中是否应该出现“HTML”一词。

我使用w3Schools作为虚拟测试网站来试验我的场景,下面是由于断言而失败的代码。

describe('Searching for word HTML', function() {
    var array=[];
    var counter=0;
    it('Beginning of the test', function() {

    browser.ignoreSynchronization = true;
    browser.get('https://www.w3schools.com/html/default.asp');

    browser.sleep(5000).then(function(){});

    var lines = element.all(by.css('#leftmenuinnerinner>a'));
    lines.count().then(function(counting){
        console.log("There are total "+counting+" links");
        counter=counter+counting;
        return counting;
    })
    lines.getText().then(function(text){

        console.log("Values:::: "+text);
        for(var k=0;k<text.length;k++){
        expect(text[k]).toContain('HTML');
        }
    })
    })
})

我知道我可以使用xpath值&amp;只迭代76个值的循环,但我正在尝试通过css选择器&amp;的方法我需要严格获取整个值列表,所以有没有办法可以修剪或删除值'HTTP Messages','HTTP Methods','PX to EM Converter'&amp; '键盘快捷键'动态地通过我返回的数组中的代码返回,以便我的断言通过?

2 个答案:

答案 0 :(得分:2)

您可以使用filter方法仅保留包含所需文本的元素。 - http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter

lines.filter(function(elem, index) {
  return elem.getText().then(function(text) {
    return text.includes('HTML');
  });
})

或者您可以使用cssContainingText定位器来获取所需的元素。 - http://www.protractortest.org/#/api?view=ProtractorBy.prototype.cssContainingText

var lines = element.all(by.cssContainingText('#leftmenuinnerinner>a','HTML'));

答案 1 :(得分:0)

避免for循环的最佳方法是each。请参阅文档:

http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.each