簡體   English   中英

使用 Laravel Dusk 測試 ReCaptcha

[英]Testing ReCaptcha with Laravel Dusk

我在使用 Laravel Dusk 在表單上測試不可見的 ReCaptcha 時遇到了一個小問題。 涉及這種情況的包是由 Albertcht 提供的 Invisible Recaptcha

我在/tests CreatesApplication.php文件中有這個方法

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

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

這對於功能測試來說非常有效,因為我只需要點擊 API 並且我可以忽略來自 post 請求的g-recaptcha-response輸入。

但是如果我想測試表單本身,使用像 Laravel Dusk 這樣的瀏覽器測試系統,當然這還不夠。

我目前的 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");
    });
 }

在這里很簡單,我調用注冊表,填寫數據,提交並檢查是否有警報。 目前這不起作用,我收到此錯誤:

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

所以問題很明顯:在測試期間,ReCaptcha 輸入沒有被渲染,因此不可能擊中它甚至偽造

有沒有辦法渲染假的 ReCaptcha 以使其與 Laravel Dusk 一起使用?

謝謝

reCaptcha 通常在 iframe 中。 Dusk 無法識別 iframe 內的元素。 但是你可以使用 method ->withinFrame('selector', function ($browser) {}); 並在 insideFrame 閉包內斷言值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM