如何测试与量角器HTML链接?量角器、链接、测试、HTML

2023-09-13 05:00:10 作者:轻嗅风中血

我是新来量角器,想测试一个链接工作。我明白试图让元素的id,但我应该期待什么该链接等于?

另外有没有人上了车比如量角器测试任何好的文档?我已经通过这个 http://angular.github.io/protractor/#/tutorial 这是有益的,但​​我需要的可能的测试,我可以做更多的例子。

我有这个至今:

 它('应该重定向到正确的页面',函数(){        。元素(by.id('signmein')点击();        expect(browser.driver.getCurrentUrl()).toEqual(\"http://localhost:8080/web/tfgm_customer/my-account\");    }); 

解决方案   

想测试一个链接工作

这是一个有点宽泛 - 它可能意味着的链接有一个适当的的href 属性,或者点击一个链接后,应该有一个新的页面打开如何使用HTML 5 标签

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

 预期(元素(by.id('myLink的')).getAttribute(HREF')).toEqual('http://myUrl.com'); 

要点击链接使用 点击() ,检查当前的URL,使用的 getCurrentUrl()

 元素(by.id('myLink的')点击();期待(browser.getCurrentUrl())toEqual(http://myUrl.com); 

请注意,如果有点击后,开了一家无棱角页面,需要玩弄 ignoreSynchronization 标志,请参见:

点击打开后无棱角页

如果该链接在新标签页打开,你需要切换到该窗口,查询的网址,然后切换回主窗口:

 元素(by.id('myLink的'))。点击()。然后(函数(){    browser.getAllWindowHandles()。然后(函数(句柄){        。browser.switchTo()窗口(句柄[handles.length  -  1]),然后(函数(){            期待(browser.getCurrentUrl())toEqual(http://myUrl.com);        });        //切换回主窗口        browser.switchTo()窗口(句柄[0])。    });}); 

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

解决方案

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:

Non-angular page opened after a click

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