sensorario
sensorario

Reputation: 21698

Test if input field is focused or not

I am using Behat with Selenium2 and I want to write a test to check if a field is focused or not. This is my experiment:

/**
 * @Then /^I could see username focused$/
 */
public function iCouldSeeUsernameFocused()
{
    $this->getSession()->wait(1000, "$('#username').is(':focus') == true");
}

This goes alwais in green. Also this:

/**
 * @Then /^I could see username focused$/
 */
public function iCouldSeeUsernameFocused()
{
    $this->getSession()->wait(1000, "false");
}

Can I test, with Behat, if a textField is focused or not?

Upvotes: 2

Views: 828

Answers (1)

ornous
ornous

Reputation: 449

Your step does not throw exceptions so it will, indeed, always be successful.

Also, wait is not the right option, here, as all it does is wait. It has no meaningful return value, as you can see here: http://mink.behat.org/api/behat/mink/session.html#wait()

Instead, I would use phpunit asserts and the evaluateScript method of the session:

assertTrue($this->getSession()->evaluateScript('// your jQuery here'));

Make sure you include phpunit's assertion function as described here: http://docs.behat.org/guides/2.definitions.html#failed-steps

Upvotes: 2

Related Questions