使用Protractor在页面上测试所有链接的有效性 [英] Test all links for validity on a page using Protractor

查看:103
本文介绍了使用Protractor在页面上测试所有链接的有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Protractor中实现的是获取页面上的所有链接并逐个查看它们以检查是否有链接路由到404页面。

What I am trying to achieve in Protractor is getting all the links on a page and go to them one by one to check if there is a link that routes to a 404 page.

以下代码来自我的电子邮件页面对象,请注意我在href上调用替换,因为我将测试的页面上的链接是以下格式: https://app.perflectie.nl/something 我希望在 https://上进行端到端测试staging.perflectie.nl 和我的localhost。

The following piece of code comes from my email page object, please note that I am calling replace on the href because the links on the pages that I will test are the following format: https://app.perflectie.nl/something and I want to end-to-end test on https://staging.perflectie.nl and on my localhost.

// Used to check for 'broken' links - links that lead to a 404 page
Email.prototype.verifyLinkQuality = function() {
    browser.driver.findElements(by.css('a')).then(function (elements) {

        elements.forEach(function(el) {
            // Change the url to the base url this tests now runs on, e.g. localhost or staging
            el.getAttribute('href').then(function(href) {
                var url = href.replace(/https\:\/\/app\.perflectie\.nl\//g, localhost);

                browser.get(url);

                browser.driver.getCurrentUrl().then(function(url) {
                    expect(url).not.toContain('/Error/');
                    browser.navigate().back();
                });
            });
        });
    });
}

如果我运行此命令,则会收到以下错误消息:

If I run this, I am getting the following error message:

Failed: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'DESKTOP-QLFLPK5', ip: '169.254.82.243', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_73'
Driver info: driver.version: unknown

I我不知道为什么会失败,我非常感谢你的帮助。

I am at a loss why this is failing, and I would very much appreciate your help.

推荐答案

而不是来回走动,通常导致陈旧的元素引用错误导致DOM更改/重新加载,我会使用 map() 然后逐个处理它们。这也会对测试的性能产生积极的影响

Instead of going back and forth, which often leads to stale element reference errors cause of the DOM change/reload, I would collect all the links into an array using map() and then process them one by one. This would also have a positive impact on the performance of the test:

$$('a').map(function(link) {
    return link.getAttribute("href").then(function (href) {
        return href.replace(/https\:\/\/app\.perflectie\.nl\//g, localhost);
    });
}).then(function(links) {
    links.forEach(function(link) {
        browser.get(link);
        expect(browser.getCurrentUrl()).not.toContain('/Error/');
    });
});

请注意,也无需解析 .getCurrentUrl()显式,因为 expect()会在做出预期之前隐式地为我们做。

Note that there is also no need to resolve the .getCurrentUrl() explicitly since expect() would do that implicitly for us before making the expectation.

这篇关于使用Protractor在页面上测试所有链接的有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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