且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS,带有角度验证码的e2e测试

更新时间:2022-04-14 03:28:33

验证码已加载到iframe中,您需要在尝试检查之前将其切换为

The captcha is loaded in an iframe, you need to switch to it before trying to check:

browser.switchTo().frame(0);

其中,0是帧索引.您可以使用框架名称,id或先前找到的框架元素.

where 0 is a frame index. You can use a frame name, id or a previously found frame element.

使用Recaptcha演示页面的示例测试:

Sample test that uses the recaptcha demo page:

"use strict";

describe("Recaptcha", function () {
    beforeEach(function () {
        browser.ignoreSynchronization = true;
        browser.get("http://vividcortex.github.io/angular-recaptcha/");
    });

    it("should click the captcha", function () {
        browser.switchTo().frame(0).then(function () {
            var checkbox = $(".recaptcha-checkbox-checkmark");

            // first hover the checkbox
            browser.actions().mouseMove(checkbox).perform();

            // hardcoded delay
            browser.sleep(500);

            // okat, now click - TODO: may be we should click with browser.actions().click() and provide the x, y coordinates for where to click
            checkbox.click();
        });

        // expectations
    });
});

请注意,在我的情况下,单击后,它会要求选择某些图像,这些图像完全可以完成不让我的硒自动化机器人通过测试的工作.如果您想实际通过验证码,请根据新的Google reCAPTCHA如何工作?页以及有关此验证码工作原理的已知信息,我将尝试以下操作:

Note that in my case, after the click, it asks to select certain images that would exactly do the job of not letting my selenium automation bot pass the test. If you want to actually pass the captcha, according to the How does new Google reCAPTCHA work? page and the known information about how this captcha works, I would try the following things:

  • 使用您之前使用过的预载配置文件打开浏览器(至少具有浏览历史记录)
  • 在启动测试的浏览器中登录到Google帐户
  • 不要通过click()单击复选框,而要像普通人一样单击带有偏移量的复选框
  • 在浏览器动作之间延迟玩耍
  • open the browser with a pre-loaded profile that you've used before (that has the browsing history, at least)
  • be logged into google account in the browser fired up for testing
  • don't click the checkbox via click() and click it with an offset as a regular human would do
  • play around with a delays between browser actions

另一点是,您无需e2e测试验证码的工作原理-它超出了应用程序的端到端测试范围.找到一种方法来禁用/关闭测试用户或要对其运行测试的特定内部版本的验证码.

Another point is that, you don't need to e2e test how the captcha works - it is out of scope of the end-to-end testing of your application. Find a way to disable/turn the captcha off for the testing users, or for a specific build that you will be running tests against.