如何用量角器测试 html 链接?

How to test html links with protractor?

我是量角器的新手,想测试一下 link 是否正常工作。 我了解尝试获取元素 ID 但我应该期望 link 等于什么?

还有人有关于示例量角器测试的任何好的文档吗? 我已经完成了这个 http://angular.github.io/protractor/#/tutorial 这很有帮助,但我需要更多我可以做的可能测试的例子。

到目前为止我有这个:

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

would like to test if a link is working

这有点宽泛 - 它可能意味着 link 具有适当的 href 属性,或者在单击 link 后应该打开一个新页面。

要检查 href 属性,请使用 getAttribute():

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

点击 link 使用 click(), to check the current URL, use getCurrentUrl():

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

注意,如果点击后打开了一个非angular页面,你需要用ignoreSynchronization标志来玩,见:

如果 link 在新标签页中打开,您需要切换到那个 window,检查 URL 然后切换回主 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]);
    });
});