[PHP] $driver->executeScript(script)

90 views
Skip to first unread message

Sebastian Szyja

unread,
Mar 29, 2022, 2:35:56 AM3/29/22
to Selenium Users
Hi,
how to capture the received result from a sent ajax request?

I have a script in which I send an ajax request to the server. When I call the script in the form of text like: return test223. I will get this result, but when I want to capture the received result from ajax so it no longer works ... I tried to wait a few seconds but it also did not give any effects ...

Skrypt:
$stringRequest  = 'let http = new XMLHttpRequest();';
$stringRequest .= 'var url = "adres www";';
$stringRequest .= 'http.open("POST", url, true);';
$stringRequest .= 'http.setRequestHeader("Accept", "application/json, text/plain, */*");';
$stringRequest .= 'http.setRequestHeader("Content-Type", "application/json;charset=utf-8");';
$stringRequest .= 'http.setRequestHeader("X-XSRF-TOKEN", "'.$xsrf.'");';
$stringRequest .= 'http.send(JSON.stringify(parameters));';
$stringRequest .= 'http.onload = function() { if (http.status != 200) { return 'error';} else {return http.response;}};';
$stringRequest .= 'http.onprogress = function(event) {if (event.lengthComputable) {console.log(`Received ${event.loaded} of ${event.total} bytes`);} else {console.log(`Received ${event.loaded} bytes`);}};';
$stringRequest .= 'http.onerror = function() {return "Request failed";};';

$result = $driver->executeScript($parameters);

while (true) {
     echo 'Oczekiwanie na dane... '.PHP_EOL;
     if($result) {
          print_r($result); break;
     }
}

David

unread,
Mar 29, 2022, 4:51:43 PM3/29/22
to Selenium Users
The problem is the XmlHttpRequest by default is asynchronous, and interfacing to an asynchronous javascript call from Selenium can be tricky.

You have possibly 3 options, whichever you find easiest to work with.
  1. make the request synchronous, so the code blocks until the request completes
  2. alter the code to have it store the result somewhere e.g. local storage, session storage, then from PHP/Selenium you keep polling for the stored data until found or you time out after a given timeout duration (you don't want to poll forever)
  3. use executeScriptAsync() or something like that if available to you, that is calling the asynchronous version of execute script from Selenium. However, I don't know how you would do much with this option, it's still probably combined with option 2 together if executeScript (synchronous) version doesn't work right.
For option 1, you might have to alter the javascript code a bit, I'm not sure, since the execution becomes synchronous, you may not need an event handler to update/set the result, as the result comes back from the call or something possibly. I haven't used XmlHttpRequest myself in a while to know the details. Some reference: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#synchronous_request

For option 2, for your onload event handler / function, instead of returning something, you write the (successful and failure) result to local storage. In PHP Selenium, after executing the javascript, you then have another loop in PHP to continually check the local storage with another different execute script call for the existence of what the XmlHttpRequest writes on success (or failure). Your PHP loop then retries if not found after perhaps sleeping some time first before next retry. And the loop exits on finding the success/failure result in local storage or times out after a max duration/retry limit.

Reply all
Reply to author
Forward
0 new messages