我是 Protractor 的新手,想测试链接是否有效。 我了解尝试获取元素 ID,但我应该期望链接等于什么?
还有人有关于示例 Protractor 测试的任何好的文档吗?
我已经完成了这个 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
这有点宽泛 - 它可能意味着链接具有适当的 href
属性,或者单击链接后应该打开一个新页面。
要检查 href
属性,请使用 getAttribute()
:
expect(element(by.id('myLink')).getAttribute('href')).toEqual('http://myUrl.com');
要单击链接,请使用 click()
, 要检查当前 URL,请使用 getCurrentUrl()
:
element(by.id('myLink').click();
expect(browser.getCurrentUrl()).toEqual("http://myUrl.com");
请注意,如果点击后打开了一个非 Angular 页面,则需要使用 ignoreSynchronization
标志,参见:
如果链接在新标签页中打开,您需要切换到该窗口,检查 URL,然后切换回主窗口:
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]);
});
});
关于html - 如何用 Protractor 测试 html 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212375/