如何用量角器测试 html 链接? [英] How to test html links with protractor?

查看:28
本文介绍了如何用量角器测试 html 链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是量角器的新手,想测试链接是否有效.我了解尝试获取元素 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 hrefattribute, 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 链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆