【问题标题】:Scrape off some array values for asserting (Protractor)刮掉一些用于断言的数组值(量角器)
【发布时间】:2017-11-07 12:38:19
【问题描述】:

有没有办法刮掉或删除 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 值并仅遍历 76 个值的循环,但我正在尝试通过 css 选择器的方法,我需要严格获取整个值列表,所以有什么方法可以修剪或者通过我返回的数组中的代码动态地刮掉值“HTTP Messages”、“HTTP Methods”、“PX to EM Converter”和“Keyboard Shortcuts”,以便我的断言通过?

【问题讨论】:

    标签: javascript arrays automation protractor gulp-protractor


    【解决方案1】:

    您可以使用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'));
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        • 2017-10-09
        • 2015-10-12
        相关资源
        最近更新 更多