Recaptcha automated testing #215

vdroznik opened this Issue Dec 28, 2012 · 6 comments


None yet
2 participants

vdroznik commented Dec 28, 2012

Is there a way to automate testing of form with recaptcha?
I tried to turn it off in config:

        enabled: false

but this config assumes that recaptcha won't be used at all anywhere in the code.

I offer to introduce parameter, like:

        test_mode: true

which will not render a captcha (or maybe render some placeholder) and validator will always return true.

I am ready to implement it, if the author is agree, and if there is no other way to test


bamarni commented Dec 29, 2012

It sounds good, what about :

# app/config_test.yml
        code: 1234

So that one can actually test success and failure?

But I think you can still have a way to do it, maybe if you overwrite genemu.form.type_recaptcha.validator with your own.


vdroznik commented Jan 2, 2013

@bamarni, this is possible to implement your solution with predefined code by replacing recaptcha widget with simple input field (it means replacing RecaptchaType with something like ReCaptchaTestType dynamically) and replacing validator to match config value and value from form field. But do we really need it? We will be testing this fake classes in this case which has small sense.

If we use test_mode: true, then we can just pass it to ReCaptchaValidator and it will return true for validation. This way we don't need to do anything with ReCaptchaType and code changes are minimal. Thoughts?


vdroznik commented Jan 2, 2013

btw, which branch should I do PR against?


bamarni commented Jan 2, 2013

In both cases the recaptcha type wouldn't have to be touched, only the validator, the point is that with your suggestion one couldn't test failure (eg. check if his custom error message for the field is displayed in case of wrong code) and a static code would also be compliant with captcha type, it is not documented yet but I had submitted a PR for this which allows a static code for functional testings : @6e17157cf1cb768c0c621942ecc2a54739850c69, @35be100e62732668295bc666fbd0710791595627

I don't think it is more complicated to implement this than always returning true, anyway. For the PR 2.1 branch sounds good.


vdroznik commented Jan 2, 2013

Recaptcha widget is rendered as follows:

<label class="required">Captcha</label>
<div id="contact_captcha_div" style="display: none;"></div>
    <iframe src="`recaptcha-key`" width="500" height="300" frameborder="0"></iframe>
    <br />
    <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
    <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />

I am trying to use Behat to test the form and next scenario doesn't work:

Scenario: Successfull contact
    Given I am on "/demo/contact"
    When I fill in "Email" with ""
    And I fill in "Message" with "contact text"
    And I fill in "recaptcha_response_field" with "1234"
    And I press "Send"
    Then I should see "Message sent!"

Unfortunately it doesn't see the field inside <noscript> tag neigher with BrowserKit nor with Goutte drivers.
This means that we should change ReCaptchaType in some way to test it (or test in real JavaScript-enabled browser which is very slow).


vdroznik commented Jan 2, 2013

It seems it works with phpUnit and DomCrawler component (it can find and fill recaptcha_challenge_field),
ok, I am moving with code solution

@vdroznik vdroznik added a commit to vdroznik/GenemuFormBundle that referenced this issue Jan 3, 2013

@vdroznik vdroznik [ReCaptcha] added a way for automated testing (issue #215) b48e37e

bamarni closed this Jan 17, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment