Angularjs,使用 angular-recaptcha 进行 e2e 测试

2023-06-15前端开发问题
1

本文介绍了Angularjs,使用 angular-recaptcha 进行 e2e 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我实际上是在尝试对我的简单应用程序进行 e2e 测试,但在处理 angular-recaptcha 时遇到了一些麻烦(https://github.com/VividCortex/angular-recaptcha).

I'm actually trying to e2e test my simple application and I m having some troubles dealing with angular-recaptcha (https://github.com/VividCortex/angular-recaptcha).

这是我的测试:

  it('should redirect on another page', function() {

    browser.get('http://127.0.0.1:3000/#/');
    var userName = element(by.model('auth.loginInfos.username'));
    userName.sendKeys('consumer1@eco.com');

    var password = element(by.model('auth.loginInfos.password'));
    password.sendKeys('consumer1');



    var recapt = element(by.id('recaptcha'));
    recapt.sendKeys();/* How can I put the recaptcha value to true ? */

    var btn = element(by.className('btn'));
    btn.click();
    /**
     * Assertions etc...
     */

  });

所以,您可以看到我正在尝试填写 recaptcha 值,但我不知道如何继续.

So, you can see that I m trying to fill the recaptcha value and I don't know how to proceed.

你能帮帮我吗?

注意:我正在使用量角器

感谢您的帮助

推荐答案

验证码加载在一个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
    });
});

请注意,在我的情况下,单击后,它会要求选择某些图像,这些图像完全可以完成不让我的 selenium 自动化机器人通过测试的工作.如果你想真正通过验证码,根据 新的 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:

  • 使用您以前使用过的预加载配置文件打开浏览器(至少有浏览历史记录)
  • 在启动测试的浏览器中登录谷歌帐户
  • 不要像普通人那样通过 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.

这篇关于Angularjs,使用 angular-recaptcha 进行 e2e 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13