5

I am new to protractor and would like to test if a link is working. I understand trying to get the element id but what should i expect that the link equals?

Also has anyone got any good documentation on example protractor tests? I have been through this http://angular.github.io/protractor/#/tutorial which was helpful but i need more example of possible tests I could do.

i have this so far:

it('should redirect to the correct page', function(){
        element(by.id('signmein').click();
        expect(browser.driver.getCurrentUrl()).toEqual("http://localhost:8080/web/tfgm_customer/my-account");
    });
BlackMagma
  • 67
  • 2
  • 2
  • 6
  • The second part of the question is not a really good fit for SO which can lead to the question to be closed as off-topic. – alecxe Mar 23 '15 at 14:09

1 Answers1

15

would like to test if a link is working

This is a bit broad - it could mean the link to have an appropriate hrefattribute, or that after clicking a link there should be a new page opened.

To check the href attribute, use getAttribute():

expect(element(by.id('myLink')).getAttribute('href')).toEqual('http://myUrl.com');

To click the link use click(), to check the current URL, use getCurrentUrl():

element(by.id('myLink').click();
expect(browser.getCurrentUrl()).toEqual("http://myUrl.com");

Note that if there is a non-angular page opened after the click, you need to play around with ignoreSynchronization flag, see:

If the link is opened in a new tab, you need to switch to that window, check the URL and then switch back to the main window:

element(by.id('myLink')).click().then(function () {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[handles.length - 1]).then(function () {
            expect(browser.getCurrentUrl()).toEqual("http://myUrl.com");
        });

        // switch back to the main window
        browser.switchTo().window(handles[0]);
    });
});
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • mate, I feel like it's so close, but im not quite there! i have pasted the statement in the code question. any reason why it doesn't like line 2? – BlackMagma Mar 23 '15 at 14:37
  • @BlackMagma missing parenthesis, replace `element(by.id('signmein').click();` with `element(by.id('signmein')).click();` – alecxe Mar 23 '15 at 14:51
  • ah yes, that makes sense. just one more query, by html code (i didnt write it) looks like this: `
  • Sign me in
  • ` i.e. there is no id...what can you get it by then? p.s. thanks, will upvote shortly! – BlackMagma Mar 23 '15 at 14:55
  • @BlackMagma at least, two options: `by.css('a.signmein')`, or, `by.linkText('Sign me in')`. – alecxe Mar 23 '15 at 14:57
  • thanks a lot, mine is still playing up but im sure i'll figure it out. – BlackMagma Mar 23 '15 at 15:07