From 786881d3f24e0d1f02359b3dba23658bac2a669f Mon Sep 17 00:00:00 2001 From: David Gonzalez Date: Wed, 11 Oct 2017 15:45:38 +0200 Subject: [PATCH 1/2] Vars in Evalute function --- src/Casper.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Casper.php b/src/Casper.php index e5fec51..310a901 100644 --- a/src/Casper.php +++ b/src/Casper.php @@ -16,6 +16,7 @@ class Casper private $TAG_CURRENT_STATUS = '[CURRENT_STATUS]'; private $TAG_CURRENT_STATUS_TEXT = '[CURRENT_STATUS_TEXT]'; private $TAG_CURRENT_COOKIES = '[CURRENT_COOKIES]'; + private $TAG_CURRENT_VARS = '[CURRENT_VARS]'; private $debug = false; private $options = array(); @@ -35,6 +36,7 @@ class Casper private $status; private $statusText = ''; private $cookies = []; + private $vars = false; public function __construct($path2casper = null, $tempDir = null) { @@ -212,6 +214,9 @@ public function start($url) } }); }); + +var vars = {}; + FRAGMENT; @@ -542,6 +547,22 @@ public function evaluate($function) }); }); +FRAGMENT; + + $this->script .= $fragment; + + return $this; + } + + public function evaluateToVar($var, $function) + { + $fragment = <<script .= $fragment; @@ -585,6 +606,7 @@ public function run() this.echo('$this->TAG_CURRENT_STATUS' + this.currentResponse.status); this.echo('$this->TAG_CURRENT_STATUS_TEXT' + this.currentResponse.statusText); this.echo('$this->TAG_CURRENT_COOKIES' + JSON.stringify(phantom.cookies)); + this.echo('$this->TAG_CURRENT_VARS' + JSON.stringify(vars)); }); casper.run(); @@ -680,6 +702,10 @@ private function processOutput() if (0 === strpos($outputLine, $this->TAG_CURRENT_COOKIES)) { $this->cookies = json_decode(str_replace($this->TAG_CURRENT_COOKIES, '', $outputLine), true); } + + if (0 === strpos($outputLine, $this->TAG_CURRENT_VARS)) { + $this->vars = json_decode(str_replace($this->TAG_CURRENT_VARS, '', $outputLine), true); + } } } @@ -739,4 +765,12 @@ public function getCookies() { return $this->cookies; } + + /** + * @return array + */ + public function getVars() + { + return $this->vars; + } } From 7fa9a48926ac4230a2753bd4b4f7d2df97e1683c Mon Sep 17 00:00:00 2001 From: David Gonzalez Date: Wed, 11 Oct 2017 16:18:47 +0200 Subject: [PATCH 2/2] Create unit tests --- Tests/CasperTest.php | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Tests/CasperTest.php b/Tests/CasperTest.php index 12812b7..1ba534e 100644 --- a/Tests/CasperTest.php +++ b/Tests/CasperTest.php @@ -287,6 +287,53 @@ public function testEvaluate() @unlink($filename); } + public function testEvaluateToVar() + { + $evaluateHtml = << + + + + test evaluate + + + link to google +
    +
  • Item 1
  • +
  • Item 2
  • +
+ + +TEXT; + $filename = '/tmp/test-evaluate-var.html'; + + file_put_contents($filename, $evaluateHtml); + + $evaluate_href = <<start($filename) + ->evaluateToVar('href',$evaluate_href) + ->evaluateToVar('num_items',$evaluate_item) + ->run(); + + $vars = $casper->getVars(); + + $this->assertContains('google.com', $vars['href']); + $this->assertEquals(2, $vars['num_items']); + + @unlink($filename); + } + public function testDoubleClick() { $evaluateHtml = <<