Created
March 27, 2014 21:44
-
-
Save aminin/9819703 to your computer and use it in GitHub Desktop.
PhpUnit + Selenium WebDriver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SeleniumServer | |
{ | |
private $downloadUrl = 'http://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-2.40.0.jar'; | |
private $jarLocation; | |
private $process; | |
public function __constuct($location = null, $downloadUrl = null) | |
{ | |
if ($downloadUrl) { | |
$this->downloadUrl = $downloadUrl; | |
} | |
if ($location) { | |
$this->jarLocation = $location; | |
} else { | |
$this->jarLocation = __DIR__ . DIRECTORY_SEPARATOR . basename($this->downloadUrl); | |
} | |
} | |
public function start() | |
{ | |
if (!file_exists($this->jarLocation)) | |
$this->download(); | |
$this->process = proc_open( | |
'java -jar selenium-server-standalone-2.40.0.jar', | |
array( | |
0 => array('pipe', 'r'), | |
1 => array('pipe', 'w'), | |
2 => array('pipe', 'a'), | |
), | |
$pipes | |
); | |
while ($str = fgets($pipes[1])) { | |
if (strpos($str, 'Started SocketListener') !== false) | |
break; | |
} | |
} | |
public function stop() | |
{ | |
if ($this->process) | |
proc_terminate($this->process); | |
} | |
public function download() | |
{ | |
$jarContents = file_get_contents($this->downloadUrl); | |
file_put_contents($this->jarLocation, $jarContents); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'SeleniumServer.php'; | |
class WebTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @var \WebDriver\Session | |
*/ | |
protected static $session; | |
public static function setUpBeforeClass() | |
{ | |
static::startSession(); | |
} | |
public static function tearDownAfterClass() | |
{ | |
static::stopSession(); | |
} | |
protected function setUp() | |
{ | |
} | |
public function testTitle() | |
{ | |
$session = $this->getSession(); | |
$session->open('http://ya.ru/'); | |
$this->assertEquals('Яндекс', $session->title()); | |
$this->assertNotEmpty($session->element('css selector', '[type=submit]')); | |
} | |
/** | |
* @expectedException WebDriver\Exception\Timeout | |
*/ | |
public function testLocalhost() | |
{ | |
$session = $this->getSession(); | |
$session->open('http://localhost/'); | |
$session->element('css selector', 'body'); | |
$session->timeouts()->wait(function () { return false; }, 2, 5); | |
} | |
/** | |
* @return \WebDriver\Session | |
*/ | |
protected function getSession() | |
{ | |
return static::$session; | |
} | |
protected static function startSession() | |
{ | |
static::ensureSeleniumServerRun(); | |
$webDriver = new WebDriver\WebDriver(); | |
if (!static::$session) | |
static::$session = $webDriver->session('firefox'); | |
} | |
protected static function stopSession() | |
{ | |
if (static::$session) { | |
static::$session->close(); | |
static::$session = null; | |
} | |
} | |
protected static function restartSession() | |
{ | |
static::stopSession(); | |
static::startSession(); | |
} | |
private static function ensureSeleniumServerRun() | |
{ | |
try { | |
$webDriver = new WebDriver\WebDriver(); | |
$webDriver->status(); | |
} catch (WebDriver\Exception\CurlExec $e) { | |
$seleniumServer = new SeleniumServer(); | |
$seleniumServer->start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment