javascriptarraysautomationprotractorgulp-protractor

Scrape off some array values for asserting (Protractor)


Is there a way to scrape off or remove random or few values that are returned by the element.all array ?

The scenario i am trying to automate is to check that the word 'HTML' should be present in every row returned from the list of array.

I have used the w3Schools as a dummy test website to trial my scenario, below is the code which is failing because of assertion.

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

I know i could use the xpath values & iterate through a loop of 76 values only but i am trying via a approach of css selector & i need to strictly get the entire list of values, so is there a way i could trim or scrape off the values 'HTTP Messages','HTTP Methods','PX to EM Converter' & 'Keyboard Shortcuts' dynamically through my code from the array returned so that my assertion passes ?


Solution

  • You can use the filter method to only keep the elements containing the text you want. - http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter

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

    Or you can use the cssContainingText locator to get the desired elements. - http://www.protractortest.org/#/api?view=ProtractorBy.prototype.cssContainingText

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