Sean
Sean

Reputation: 1108

Filling in hidden inputs with Behat

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?

Upvotes: 6

Views: 7744

Answers (3)

simply-put
simply-put

Reputation: 1088

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

Upvotes: 9

WayFarer
WayFarer

Reputation: 1038

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

Upvotes: 12

ocornu
ocornu

Reputation: 579

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)

Upvotes: 2

Related Questions