Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Tests/CasperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,53 @@ public function testEvaluate()
@unlink($filename);
}

public function testEvaluateToVar()
{
$evaluateHtml = <<<TEXT
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test evaluate</title>
</head>
<body>
<a id="theLink" href='http://www.google.com' onclick='return confirm("are you sure")'>link to google</a>
<ul id="theList">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
</ul>
</body>
</html>
TEXT;
$filename = '/tmp/test-evaluate-var.html';

file_put_contents($filename, $evaluateHtml);

$evaluate_href = <<<TEXT

var theLink = document.getElementById("theLink");

return theLink.href;
TEXT;

$evaluate_item = <<<TEXT
return document.getElementsByClassName("item").length;
TEXT;

$casper = new Casper(self::$casperBinPath);
$casper->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 = <<<TEXT
Expand Down
34 changes: 34 additions & 0 deletions src/Casper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -35,6 +36,7 @@ class Casper
private $status;
private $statusText = '';
private $cookies = [];
private $vars = false;

public function __construct($path2casper = null, $tempDir = null)
{
Expand Down Expand Up @@ -212,6 +214,9 @@ public function start($url)
}
});
});

var vars = {};


FRAGMENT;

Expand Down Expand Up @@ -542,6 +547,22 @@ public function evaluate($function)
});
});

FRAGMENT;

$this->script .= $fragment;

return $this;
}

public function evaluateToVar($var, $function)
{
$fragment = <<<FRAGMENT
casper.then(function() {
vars.$var = casper.evaluate(function() {
$function
});
});

FRAGMENT;

$this->script .= $fragment;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -739,4 +765,12 @@ public function getCookies()
{
return $this->cookies;
}

/**
* @return array
*/
public function getVars()
{
return $this->vars;
}
}