WebDriverCurlService.php 4.0 KB

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