testing all the links on a page using protractor testing all the links on a page using protractor selenium selenium

testing all the links on a page using protractor


To get all the links, call getAttribute on an ElementArrayFinder. It will return a Promise which once resolved will give you all the links.Then call filter to exclude the dynamic links (href="javascript:...) and forEach to iterate each link:

browser.ignoreSynchronization = true;$$("a[href]").getAttribute("href")  .then(links => links    .filter(link => !/^javascript/.test(link))    .forEach(link => {      console.log(link);      browser.driver.get(link);    })  );

Another and quicker way is to get all the links with execute script with a single call to the browser:

browser.ignoreSynchronization = true;browser.driver.executeScript("return [].map.call(document.links, function(e){return e.href})")  .then(links => links    .filter(link => !/^javascript/.test(link))    .forEach(link => {      console.log(link);      browser.driver.get(link);    })  );


See following code.

$$('a').map(function(link) {    return link.getAttribute("href").then(function (href) {        return href.replace(/https\:\/\/app\.perflectie\.nl\//g, localhost);    });}).then(function(links) {    links.forEach(function(link) {        browser.get(link);        expect(browser.getCurrentUrl()).not.toContain('/Error/');    });});

For more innformation go to following links.

Link 1

Link 2

Hope this helps. :)


 it('link elements', function () {    browser.ignoreSynchronization = true;    browser.get('https://www.google.co.in');    element.all(by.tagName('a')).each(function (elem) { // this is the important step, rest you can do whatever you want inside this        elem.getText().then(function (val) {            console.log('@@@@ : ' + val)        })    })});