問題描述
我是量角器的新手,想測試鏈接是否有效.我了解嘗試獲取元素 ID,但我應該期望鏈接等于什么?
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?
還有人有關于示例量角器測試的任何好的文檔嗎?我已經通過這個 http://angular.github.io/protractor/#/tutorial
這很有幫助,但我需要更多我可以做的可能測試的例子.
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.
到目前為止我有這個:
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
屬性,或者單擊鏈接后應該打開一個新頁面.
This is a bit broad - it could mean the link to have an appropriate href
attribute, or that after clicking a link there should be a new page opened.
要檢查 href
屬性,請使用 getAttribute()
:
To check the href
attribute, use 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");
注意,如果點擊后打開的是非角頁面,則需要玩轉ignoreSynchronization
標志,見:
Note that if there is a non-angular page opened after the click, you need to play around with ignoreSynchronization
flag, see:
- 點擊后打開的非角度頁面
如果鏈接在新標簽頁中打開,您需要切換到該窗口,檢查 URL,然后切換回主窗口:
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]);
});
});
這篇關于如何用量角器測試 html 鏈接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!