简体   繁体   English

使用 Laravel Dusk 测试 ReCaptcha

[英]Testing ReCaptcha with Laravel Dusk

I've a small problem testing an invisible ReCaptcha on a form using Laravel Dusk.我在使用 Laravel Dusk 在表单上测试不可见的 ReCaptcha 时遇到了一个小问题。 The package involved in this situation is Invisible Recaptcha by Albertcht涉及这种情况的包是由 Albertcht 提供的 Invisible Recaptcha

I've this method inside CreatesApplication.php file in /tests我在/tests CreatesApplication.php文件中有这个方法

protected function withoutRecaptcha() : void
{
    InvisibleReCaptcha::shouldReceive("verifyRequest")
        ->andReturnTrue();

    InvisibleReCaptcha::shouldReceive("verifyResponse")
        ->once()
        ->andReturnTrue();
}

This works quiet well for Features test, because I only need to hit APIs and I can ignore the g-recaptcha-response input from the post request.这对于功能测试来说非常有效,因为我只需要点击 API 并且我可以忽略来自 post 请求的g-recaptcha-response输入。

But if I want to test the form itself, using a browser test system like Laravel Dusk, of course this is not enough.但是如果我想测试表单本身,使用像 Laravel Dusk 这样的浏览器测试系统,当然这还不够。

My current Dusk method is the following.我目前的 Dusk 方法如下。

 public function a_guest_can_fill_the_registration_form()
 {
     $this->withoutRecaptcha();

     $this->browse(function (Browser $browser) {
         $browser->maximize()
            ->visit(new Register())
            ->assertPresent("input[name=g-recaptcha-response]")
            ->fillFormWithRandomData()
            ->submitForm()
            ->waitForReload()
            ->assertVisible(".alert.alert-success");
    });
 }

Straightforward stuff here, I call the register form, fill the data, submit it and check if an alert is in sight.在这里很简单,我调用注册表,填写数据,提交并检查是否有警报。 Currently this doesn't work and I get this error:目前这不起作用,我收到此错误:

There was 1 failure:

1) Tests\Browser\RegisterTest::a_guest_can_fill_the_registration_form
Element [body input[name=g-recaptcha-response]] is not present.
Failed asserting that false is true.

/app/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:806
/app/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:67

So the problem is clear: During the test the ReCaptcha input is not rendered thus it's impossible to hit it or even fake it所以问题很明显:在测试期间,ReCaptcha 输入没有被渲染,因此不可能击中它甚至伪造

Is there a way to render of fake the ReCaptcha in order to make it works with Laravel Dusk?有没有办法渲染假的 ReCaptcha 以使其与 Laravel Dusk 一起使用?

Thank you谢谢

reCaptcha usually within iframe. reCaptcha 通常在 iframe 中。 Dusk cannot recognize element inside iframe. Dusk 无法识别 iframe 内的元素。 However you can use method ->withinFrame('selector', function ($browser) {});但是你可以使用 method ->withinFrame('selector', function ($browser) {}); and assert the value inside withinFrame closure.并在 insideFrame 闭包内断言值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM