Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\FunctionalJavascriptTests;
|
Chris@17
|
4
|
Chris@17
|
5 use WebDriver\Service\CurlService;
|
Chris@17
|
6 use WebDriver\Exception\CurlExec;
|
Chris@17
|
7 use WebDriver\Exception as WebDriverException;
|
Chris@17
|
8
|
Chris@17
|
9 /**
|
Chris@17
|
10 * Provides a curl service to interact with Selenium driver.
|
Chris@17
|
11 *
|
Chris@17
|
12 * Extends WebDriver\Service\CurlService to solve problem with race conditions,
|
Chris@17
|
13 * when multiple processes requests.
|
Chris@17
|
14 */
|
Chris@17
|
15 class WebDriverCurlService extends CurlService {
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * {@inheritdoc}
|
Chris@17
|
19 */
|
Chris@17
|
20 public function execute($requestMethod, $url, $parameters = NULL, $extraOptions = []) {
|
Chris@17
|
21 $extraOptions += [
|
Chris@17
|
22 CURLOPT_FAILONERROR => TRUE,
|
Chris@17
|
23 ];
|
Chris@17
|
24 $retries = 0;
|
Chris@17
|
25 while ($retries < 10) {
|
Chris@17
|
26 try {
|
Chris@17
|
27 $customHeaders = [
|
Chris@17
|
28 'Content-Type: application/json;charset=UTF-8',
|
Chris@17
|
29 'Accept: application/json;charset=UTF-8',
|
Chris@17
|
30 ];
|
Chris@17
|
31
|
Chris@17
|
32 $curl = curl_init($url);
|
Chris@17
|
33 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
Chris@17
|
34
|
Chris@17
|
35 switch ($requestMethod) {
|
Chris@17
|
36 case 'GET':
|
Chris@17
|
37 break;
|
Chris@17
|
38
|
Chris@17
|
39 case 'POST':
|
Chris@17
|
40 if ($parameters && is_array($parameters)) {
|
Chris@17
|
41 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
|
Chris@17
|
42 }
|
Chris@17
|
43 else {
|
Chris@17
|
44 $customHeaders[] = 'Content-Length: 0';
|
Chris@17
|
45 }
|
Chris@17
|
46
|
Chris@17
|
47 // Suppress "Expect: 100-continue" header automatically added by
|
Chris@17
|
48 // cURL that causes a 1 second delay if the remote server does not
|
Chris@17
|
49 // support Expect.
|
Chris@17
|
50 $customHeaders[] = 'Expect:';
|
Chris@17
|
51
|
Chris@17
|
52 curl_setopt($curl, CURLOPT_POST, TRUE);
|
Chris@17
|
53 break;
|
Chris@17
|
54
|
Chris@17
|
55 case 'DELETE':
|
Chris@17
|
56 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
Chris@17
|
57 break;
|
Chris@17
|
58
|
Chris@17
|
59 case 'PUT':
|
Chris@17
|
60 if ($parameters && is_array($parameters)) {
|
Chris@17
|
61 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
|
Chris@17
|
62 }
|
Chris@17
|
63 else {
|
Chris@17
|
64 $customHeaders[] = 'Content-Length: 0';
|
Chris@17
|
65 }
|
Chris@17
|
66
|
Chris@17
|
67 // Suppress "Expect: 100-continue" header automatically added by
|
Chris@17
|
68 // cURL that causes a 1 second delay if the remote server does not
|
Chris@17
|
69 // support Expect.
|
Chris@17
|
70 $customHeaders[] = 'Expect:';
|
Chris@17
|
71
|
Chris@17
|
72 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
|
Chris@17
|
73 break;
|
Chris@17
|
74 }
|
Chris@17
|
75
|
Chris@17
|
76 foreach ($extraOptions as $option => $value) {
|
Chris@17
|
77 curl_setopt($curl, $option, $value);
|
Chris@17
|
78 }
|
Chris@17
|
79
|
Chris@17
|
80 curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
|
Chris@17
|
81
|
Chris@17
|
82 $rawResult = trim(curl_exec($curl));
|
Chris@17
|
83
|
Chris@17
|
84 $info = curl_getinfo($curl);
|
Chris@17
|
85 $info['request_method'] = $requestMethod;
|
Chris@17
|
86
|
Chris@17
|
87 if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) && $extraOptions[CURLOPT_FAILONERROR] && CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) && $error = curl_error($curl)) {
|
Chris@17
|
88 curl_close($curl);
|
Chris@17
|
89
|
Chris@17
|
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));
|
Chris@17
|
91 }
|
Chris@17
|
92
|
Chris@17
|
93 curl_close($curl);
|
Chris@17
|
94
|
Chris@17
|
95 $result = json_decode($rawResult, TRUE);
|
Chris@17
|
96 if (isset($result['status']) && $result['status'] === WebDriverException::STALE_ELEMENT_REFERENCE) {
|
Chris@17
|
97 usleep(100000);
|
Chris@17
|
98 $retries++;
|
Chris@17
|
99 continue;
|
Chris@17
|
100 }
|
Chris@17
|
101 return [$rawResult, $info];
|
Chris@17
|
102 }
|
Chris@17
|
103 catch (CurlExec $exception) {
|
Chris@17
|
104 $retries++;
|
Chris@17
|
105 }
|
Chris@17
|
106 }
|
Chris@17
|
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));
|
Chris@17
|
108 }
|
Chris@17
|
109
|
Chris@17
|
110 }
|