DrColza
DrColza

Reputation: 110

Behat fill form fields which are dynamically set to 'visible'

I have a form with hidden fields. If a user selects a checkbox then some of the hidden form fields are revealed using the jquery function slideToggle(). It's very straight forward code, you can view an example here.

My problem is that Behat still considers the form fields to be invisible and non-interactable after the checkbox has revealed them for the user.

This is the scenario I'm trying to test in my feature:

When I check "form_checkbox" 
And I fill in "form_field" with "some data"

How do I go about waiting for the on click listener for the checkbox to finish execution before I attempt to fill in the form or is there a better approach?

Upvotes: 0

Views: 760

Answers (1)

tanGee
tanGee

Reputation: 573

You could try putting an AfterStep step between the two to allow for jQuery to perform the animation. Something like this in your FeatureContext should help:

 /**
 * @AfterStep @javascript
 */
public function afterStep($event)
{
    $text = $event->getStep()->getText();
    if (preg_match('/(follow|press|click|submit|check)/i', $text)) {
        $this->jqueryWait();
    }
}

protected function jqueryWait($duration = 4000)
{
        $this->getSession()->wait($duration, '(0 === jQuery.active && 0 === jQuery(':animated').length)');
}

This basically means that after any step whose text contains "follow", "press", "click", "submit" or "check", the execution will wait either $duration milliseconds or until the javascript assertion returns true (i.e. jQuery is not animating anything).

Upvotes: 1

Related Questions