Last Updated: February 25, 2016
·
3.204K
· ex00

Symfony2 Behat - check checkbox if not checked yet (with onclick event)

I check "checkbox_id"' doesn't work for me
Behat includes method for checking checkboxes, but it have one disadvantage - when it is run on Selenium server it only tacks checkbox value but don't fire onclick event. I write some small snippet to handle this flaw.

Code snippet

/**
 * @When /^I check "([^"]*)" if not checked yet$/
 */
public function iCheckIfNotCheckedYet($id)
{
    $Page = $this->getSession()->getPage();
    $isChecked = $Page->find('css', 'input[type="checkbox"]:checked#' . $id);
    if (!$isChecked) {
        $Page->find('css', '#' . $id)->click();
    }
}

It checks if checkbox is checked, if not it fire click action on it.