Lord Yggdrasill
Lord Yggdrasill

Reputation: 3397

Codeception Acceptance Test: How to test AJAX request?

Well I was using codeception to test on an ajax page, in which I click the button and some kind of text is shown after an AJAX request is performed.

$I->amOnPage('/clickbutton.html');
$I->click('Get my ID');
$I->see('Your user id is 1', '.divbox');

As you see, the test is supposed to work in a way that 'Your user id is {$id}' is returned(in this case the id is 1), and updates a div box with the text. However, it doesnt work at all, instead the test says the div box is blank. What did I do wrong? How can I use codeception to test an AJAX request?

Upvotes: 3

Views: 2647

Answers (2)

Oliver Maksimovic
Oliver Maksimovic

Reputation: 3262

You can also use this:

$I->click('#something');
$I->waitForText('Something that appears a bit later', 20, '#my_element');

20 = timeout (in seconds), give it some sane value.

It's a bit more flexible instead of hammering down things like $I->wait(X);, because they are usually a lot faster than waiting for them in seconds. So, for example, if you've got many elements that you need to "wait" for, let's say 15-20, then your test will spend 15-20 seconds "waiting" while actual operations finish in maybe 1-2s total. Across many tests this can increase build times significantly, which is... not good :)

Upvotes: 1

justaguess
justaguess

Reputation: 36

Are you sure your request has finished by the time you check the div? Try adding a little wait after sending the request:

$I->amOnPage('/clickbutton.html');
$I->click('Get my ID');

$I->wait(1); //add this

$I->see('Your user id is 1', '.divbox');

Upvotes: 0

Related Questions