How to test html links with protractor?

最后都变了- 提交于 2019-12-01 18:13:36
alecxe

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]);
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!