2017-06-06 25 views
1

有沒有辦法刪除或刪除由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'); 
     } 
    }) 
    }) 
}) 

我知道我可以&迭代只通過76個值的循環使用XPath值,但我通過CSS選擇器&我需要嚴格獲取值的整個列表的方式努力着,所以有沒有我可以修剪或廢除'HTTP消息','HTTP方法','PX到EM轉換'&'鍵盤快捷方式'動態通過我的代碼從數組返回,以便我的斷言通過?

回答

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'));