2

I've been able to work this out in java but so far I'm only able to open the webpage using jasmine js. In java, all the anchor tag links can be saved in List and then each link can be navigated using the browser driver object. But in jasmine js, I'm unable to store those links in an array. Here's what I've tried to do:

describe('demo', function()
{
  it('mydemo', function()
  {
    browser.ignoreSynchronization = true;
    browser.driver.get('https://www.google.co.in');
    var array = [];
    array.push(browser.findElement(by.xpath("//a[@href]")));

    for(var i=0; i<array.length; i++)
    {
        expect(browser.navigate().to(array[i]));
    }
  }
}

A new browser window pops up, navigates to google and then closes. There seems to be a timeout issue. Using browser.ignoreSynchronization = true, the server ignores it as an angular application but still the timeout issues persists. Any suggestions?

wolfsbane
  • 1,729
  • 2
  • 11
  • 17

3 Answers3

1

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);
    })
  );
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

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. :)

Community
  • 1
  • 1
Manuli
  • 1,203
  • 4
  • 20
  • 43
0
 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)
        })
    })
});
radio_head
  • 1,306
  • 4
  • 13
  • 28