Search code examples
javascriptphpajaxsymfonycodeception

Codeception testing form with select autocomplete ajax field


Behavior in production environment.

I have a form with an autocomplete select box, where its initial representation is empty:

<select name="customer"></select>

Latter, when I click, an ajax request load and populate the first page of options (I don't need type anything at this time).

Then, I can choose one option and to submit the form using ajax. The entity is saved successfully and I show a flash message "Item has been successfully created."

Testing behavior in Codeception environment:

I need test the behavior previously explained using functional test (Codeception)

$I->amOnPage('/create');
$I->selectOption('select[name="customer"]', 1); //choose option with value = 1
$I->submitForm('form');
$I->canSee('Item has been successfully created.');

Result:

[InvalidArgumentException] Input "customer" cannot take "1" as a value (possible values: ).

This not works! of course, the select element is empty (no options).

So my question is obvious at this moment, how can I test this scenario?


Solution

  • I've found the answer to question later:

    http://codeception.com/docs/modules/PhpBrowser#submitForm.

    For those case where the fields (<select>) are populated dynamically (AJAX), simply omits the $I->selectOption(...) statement and pass the values to $I->submitForm('form', [...]); directly, so:

    $I->amOnPage('/create');
    $I->submitForm('form', ['customer' => 1]);
    $I->canSee('Item has been successfully created.');
    

    It's enough, my tests they are green again :)