Filling in hidden inputs with Behat

Sean picture Sean · Sep 26, 2011 · Viewed 7.1k times · Source

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?

Answer

WayFarer picture WayFarer · Jan 3, 2013

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);
}