X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fjcalderonzumba%2Fmink-phantomjs-driver%2Fsrc%2FJavascriptTrait.php;fp=vendor%2Fjcalderonzumba%2Fmink-phantomjs-driver%2Fsrc%2FJavascriptTrait.php;h=11453246b02d5c1f4463f61c49bb758c6851e1a4;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/jcalderonzumba/mink-phantomjs-driver/src/JavascriptTrait.php b/vendor/jcalderonzumba/mink-phantomjs-driver/src/JavascriptTrait.php new file mode 100644 index 000000000..11453246b --- /dev/null +++ b/vendor/jcalderonzumba/mink-phantomjs-driver/src/JavascriptTrait.php @@ -0,0 +1,75 @@ +browser->execute($this->fixSelfExecutingFunction($script)); + } + + /** + * Evaluates a script and returns the result + * @param string $script + * @return mixed + */ + public function evaluateScript($script) { + $script = preg_replace('/^return\s+/', '', $script); + + $script = $this->fixSelfExecutingFunction($script); + + return $this->browser->evaluate($script); + } + + /** + * Waits some time or until JS condition turns true. + * + * @param integer $timeout timeout in milliseconds + * @param string $condition JS condition + * @return boolean + * @throws DriverException When the operation cannot be done + */ + public function wait($timeout, $condition) { + $start = microtime(true); + $end = $start + $timeout / 1000.0; + do { + $result = $this->browser->evaluate($condition); + if ($result) { + // No need to wait any longer when the condition is met already. + return TRUE; + } + usleep(100000); + } while (microtime(true) < $end && !$result); + + return (bool)$result; + } + + /** + * Fixes self-executing functions to allow evaluating them. + * + * The self-executing function must be wrapped in braces to work. + * + * @param string $script + * + * @return string + */ + private function fixSelfExecutingFunction($script) + { + if (preg_match('/^function[\s\(]/', $script)) { + $script = preg_replace('/;$/', '', $script); + $script = '(' . $script . ')'; + } + + return $script; + } +}