comparison vendor/jcalderonzumba/mink-phantomjs-driver/src/JavascriptTrait.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
13 /** 13 /**
14 * Executes a script on the browser 14 * Executes a script on the browser
15 * @param string $script 15 * @param string $script
16 */ 16 */
17 public function executeScript($script) { 17 public function executeScript($script) {
18 $this->browser->execute($script); 18 $this->browser->execute($this->fixSelfExecutingFunction($script));
19 } 19 }
20 20
21 /** 21 /**
22 * Evaluates a script and returns the result 22 * Evaluates a script and returns the result
23 * @param string $script 23 * @param string $script
24 * @return mixed 24 * @return mixed
25 */ 25 */
26 public function evaluateScript($script) { 26 public function evaluateScript($script) {
27 return $this->browser->evaluate($script); 27 $script = preg_replace('/^return\s+/', '', $script);
28
29 $script = $this->fixSelfExecutingFunction($script);
30
31 return $this->browser->evaluate($script);
28 } 32 }
29 33
30 /** 34 /**
31 * Waits some time or until JS condition turns true. 35 * Waits some time or until JS condition turns true.
32 * 36 *
38 public function wait($timeout, $condition) { 42 public function wait($timeout, $condition) {
39 $start = microtime(true); 43 $start = microtime(true);
40 $end = $start + $timeout / 1000.0; 44 $end = $start + $timeout / 1000.0;
41 do { 45 do {
42 $result = $this->browser->evaluate($condition); 46 $result = $this->browser->evaluate($condition);
47 if ($result) {
48 // No need to wait any longer when the condition is met already.
49 return TRUE;
50 }
43 usleep(100000); 51 usleep(100000);
44 } while (microtime(true) < $end && !$result); 52 } while (microtime(true) < $end && !$result);
45 53
46 return (bool)$result; 54 return (bool)$result;
47 } 55 }
48 56
57 /**
58 * Fixes self-executing functions to allow evaluating them.
59 *
60 * The self-executing function must be wrapped in braces to work.
61 *
62 * @param string $script
63 *
64 * @return string
65 */
66 private function fixSelfExecutingFunction($script)
67 {
68 if (preg_match('/^function[\s\(]/', $script)) {
69 $script = preg_replace('/;$/', '', $script);
70 $script = '(' . $script . ')';
71 }
72
73 return $script;
74 }
49 } 75 }