3 namespace Drupal\FunctionalJavascriptTests;
5 use WebDriver\Service\CurlService;
6 use WebDriver\Exception\CurlExec;
7 use WebDriver\Exception as WebDriverException;
10 * Provides a curl service to interact with Selenium driver.
12 * Extends WebDriver\Service\CurlService to solve problem with race conditions,
13 * when multiple processes requests.
15 class WebDriverCurlService extends CurlService {
20 public function execute($requestMethod, $url, $parameters = NULL, $extraOptions = []) {
22 CURLOPT_FAILONERROR => TRUE,
25 while ($retries < 10) {
28 'Content-Type: application/json;charset=UTF-8',
29 'Accept: application/json;charset=UTF-8',
32 $curl = curl_init($url);
33 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
35 switch ($requestMethod) {
40 if ($parameters && is_array($parameters)) {
41 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
44 $customHeaders[] = 'Content-Length: 0';
47 // Suppress "Expect: 100-continue" header automatically added by
48 // cURL that causes a 1 second delay if the remote server does not
50 $customHeaders[] = 'Expect:';
52 curl_setopt($curl, CURLOPT_POST, TRUE);
56 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
60 if ($parameters && is_array($parameters)) {
61 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
64 $customHeaders[] = 'Content-Length: 0';
67 // Suppress "Expect: 100-continue" header automatically added by
68 // cURL that causes a 1 second delay if the remote server does not
70 $customHeaders[] = 'Expect:';
72 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
76 foreach ($extraOptions as $option => $value) {
77 curl_setopt($curl, $option, $value);
80 curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
82 $rawResult = trim(curl_exec($curl));
84 $info = curl_getinfo($curl);
85 $info['request_method'] = $requestMethod;
87 if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) && $extraOptions[CURLOPT_FAILONERROR] && CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) && $error = curl_error($curl)) {
90 throw WebDriverException::factory(WebDriverException::CURL_EXEC, sprintf("Curl error thrown for http %s to %s%s\n\n%s", $requestMethod, $url, $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '', $error));
95 $result = json_decode($rawResult, TRUE);
96 if (isset($result['status']) && $result['status'] === WebDriverException::STALE_ELEMENT_REFERENCE) {
101 return [$rawResult, $info];
103 catch (CurlExec $exception) {
107 throw WebDriverException::factory(WebDriverException::CURL_EXEC, sprintf("Curl error thrown for http %s to %s%s\n\n%s", $requestMethod, $url, $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '', $error));