1

I'm trying to execute an AJAX request in the browser in Codeception. There is the function

$I->executeAsyncJS(); 

Manual https://codeception.com/docs/modules/WebDriver#executeAsyncJS

I'm trying to understand the manual. It says:

Executes asynchronous JavaScript. A callback should be executed by JavaScript to exit from a script. Callback is passed as a last element in arguments array. Additional arguments can be passed as array in second parameter.

I just don't understand what this is saying. Here's what I have so far:

JS on Page:

const Done = {}; Done.result = function(){ return "Happy"; } 

Codeception Selenium WebDriver Chrome Test:

<?php class SEQuestionCest { public function itShouldReturnJoy(\AcceptanceTester $I){ $I->amOnPage("/page.php"); $happy = $I->executeJS("return Done.result();"); $I->assertEquals($happy, "Happy"); //This works $happy = $I->executeAsyncJS("setTimeout(function() { return Done.result(); }, 2000);"); //I'm doing something wrong $I->assertEquals($happy, "Happy"); } } 

Result: enter image description here

I expected it to return after 2 seconds, but instead it just times out after about 20 seconds. I thought the timeout function would simulate an AJAX call. I'm doing something very wrong obviously.

3
  • I didn't think that executeAsyncJS actually returned anything. Commented Sep 9, 2020 at 3:29
  • Well executeJS does. What does executeAsyncJS do then? How can I get it to return PHP flow after 2 seconds - or rather after the js executes, asynchronously? Commented Sep 9, 2020 at 21:28
  • I'm not too sure - you'd have to poke around the source but my understanding was that executeJS executed, waited for and returned the result while executeAsyncJS initiated execution then returned immediately while the Javascript executed in the background. I'm not sure if Codeception gives you the level of control that you want (maybe it does) but try making the PHP wait for the Javascript to set some property on window as a signal or something like that. Commented Sep 9, 2020 at 22:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.