Filling in hidden inputs with Behat
Asked Answered
T

3

6

I am writing Behat tests and I need to change the value of a hidden input field

<input type="hidden" id="input_id" ..... />

I need to change the value of this input field, but I keep getting

Form field with id|name|label|value "input_id" not found

I have been using the step

$steps->And('I fill in "1" for "input_id"', $world);

Is there something special which needs to be done to modify hidden input fields?

Tenantry answered 26/9, 2011 at 20:47 Comment(0)
J
12

Despite the fact that user can't fill hidden fields, there are some situations when this is desirable to be able to fill hidden field for testing (as usually rules have exceptions). You can use next step in your feature context class to fill hidden field by name:

/**
 * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
 */
public function iFillHiddenFieldWith($field, $value)
{
    $this->getSession()->getPage()->find('css',
        'input[name="'.$field.'"]')->setValue($value);
}
Jecho answered 2/1, 2013 at 23:32 Comment(2)
For example, I used it to test ReCaptcha widget with custom validator (which just validates against predefined value set if test environment) without running it in real browser.Jecho
This is a great strategy for testing server side validation hacks as well.Phidias
P
9

Rev is right. If real user can can change input fields via javascript by clicking a button or link. try doing that. Fields that are not visible to user are also not visible to Mink also.

Or else what you can do is Call $session->executeScript($javascript) from your context with $javascript like

$javascript = "document.getElementById('input_id').value='abc'";
$this->getSession()->executeScript($javascript);

and check if that works

Paynter answered 1/10, 2012 at 11:20 Comment(1)
where does $session come from?Alfieri
D
2

It's intended by design. Mink is user+browser emulator. It emulates everything, that real user can do in real browser. And user surely can't fill hidden fields on the page - he just don't see them.

Mink is not crawler, it's a browser emulator. The whole idea of Mink is to describe real user interactions through simple and clean API. If there's something, that user can't do through real browser - you can't do it with Mink.

(source: http://groups.google.com/group/behat/browse_thread/thread/f06d423c27754c4d)

Deify answered 19/3, 2012 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.