Skip to content

Instantly share code, notes, and snippets.

@generalredneck
Created March 2, 2012 22:18
Show Gist options
  • Save generalredneck/1961816 to your computer and use it in GitHub Desktop.
Save generalredneck/1961816 to your computer and use it in GitHub Desktop.
a file you include using php -a to control browsers interactively using selenium.
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase/Driver.php';
class SeleniumConsole {
public $driver;
public $testId;
function __construct($browser) {
$this->testId = md5(uniqid(rand(), TRUE));
$this->driver = $this->getDriver($browser);
}
protected function getDriver(array $browser) {
if (isset($browser['name'])) {
if (!is_string($browser['name'])) {
throw new InvalidArgumentException(
'Array element "name" is no string.'
);
}
} else {
$browser['name'] = '';
}
if (isset($browser['browser'])) {
if (!is_string($browser['browser'])) {
throw new InvalidArgumentException(
'Array element "browser" is no string.'
);
}
} else {
$browser['browser'] = '';
}
if (isset($browser['host'])) {
if (!is_string($browser['host'])) {
throw new InvalidArgumentException(
'Array element "host" is no string.'
);
}
} else {
$browser['host'] = 'localhost';
}
if (isset($browser['port'])) {
if (!is_int($browser['port'])) {
throw new InvalidArgumentException(
'Array element "port" is no integer.'
);
}
} else {
$browser['port'] = 4444;
}
if (isset($browser['timeout'])) {
if (!is_int($browser['timeout'])) {
throw new InvalidArgumentException(
'Array element "timeout" is no integer.'
);
}
} else {
$browser['timeout'] = 30;
}
if (isset($browser['httpTimeout'])) {
if (!is_int($browser['httpTimeout'])) {
throw new InvalidArgumentException(
'Array element "httpTimeout" is no integer.'
);
}
} else {
$browser['httpTimeout'] = 45;
}
$driver = new SeleniumConsoleDriver();
$driver->setName($browser['name']);
$driver->setBrowser($browser['browser']);
$driver->setHost($browser['host']);
$driver->setPort($browser['port']);
$driver->setTimeout($browser['timeout']);
$driver->setHttpTimeout($browser['httpTimeout']);
$driver->setTestId($this->testId);
$driver->setBrowserUrl($browser['url']);
$driver->setTimeout(30);
$driver->setTestCase(new SeleniumConsoleTestCase());
$this->drivers[] = $driver;
return $driver;
}
}
class SeleniumConsoleDriver extends PHPUnit_Extensions_SeleniumTestCase_Driver {
public function setTestCase($testCase)
{
$this->testCase = $testCase;
}
}
class SeleniumConsoleTestCase {
public function runDefaultAssertions() {
return;
}
}
if(empty($browser['browser'])) {
$browser['browser'] = '*firefox';
}
$console = new SeleniumConsole($browser);
$console->driver->start();
$driver = $console->driver;
$console->driver->open('/');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment