Skip to content

Instantly share code, notes, and snippets.

@jeznag
Last active November 23, 2020 00:08
Show Gist options
  • Save jeznag/e0c8c062785eb05a97ff to your computer and use it in GitHub Desktop.
Save jeznag/e0c8c062785eb05a97ff to your computer and use it in GitHub Desktop.
Combine PhantomJS with PHPUnit
<?php
class TestScaffold extends \PHPUnit_Extensions_Selenium2TestCase {
protected function setUp() {
$this->setBrowser( 'phantomjs' );
$this->setBrowserUrl( 'http://www.example.com/' );
$this->run_selenium_server();
$this->run_phantom_js();
}
protected function run_selenium_server()
{
if($this->selenium_server_already_running())
{
fwrite(STDOUT, "Selenium server already running\n");
}
else
{
shell_exec("java -jar " . __DIR__ . "\bin\selenium-server-standalone-2.45.0.jar");
}
}
protected function run_phantom_js()
{
if ($this->phantom_js_already_running()) {
fwrite(STDOUT, "PhantomJS already running\n");
} else {
fwrite(STDOUT, "Starting PhantomJS\n");
shell_exec(__DIR__ . "\bin\phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444");
}
}
protected function selenium_server_already_running()
{
return fsockopen("localhost", 4444);
}
protected function phantom_js_already_running()
{
try {
return fsockopen("localhost", 8080);
} catch (Exception $e) {
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment