Created
January 11, 2012 22:50
-
-
Save gcoop/1597261 to your computer and use it in GitHub Desktop.
PHPUnit test case extension to include Selenium2.
This file contains hidden or 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 "php-webdriver/__init__.php"; | |
class SomeTests extends PHPUnit_Framework_TestCase | |
{ | |
protected $session; | |
public function setUp() | |
{ | |
parent::setUp(); | |
// Validate script arguments. | |
if ($_SERVER['argc'] < 2) | |
exit('Invalid number of arguments supplied. Usage: ./test.php [OPTIONS] [COMMAND] [ARGS...]'."\n"); | |
// Iterate the inputs joining options into a key value array and then determining the command. | |
$options = array(); | |
$command = false; | |
$args = array_slice($_SERVER['argv'], 2, count($_SERVER['argv'])); | |
for ($i = 0; $i < count($args); $i = $i + 2) { | |
if (strpos($args[$i], "--") === 0) { | |
$parts = explode(",", $args[$i + 1]); | |
$options[str_replace("--", "", $args[$i])] = count($parts) > 1 ? $parts : $parts[0]; | |
} | |
} | |
if (!isset($options['env'])) | |
{ | |
throw new Exception("Must define --env option either; DEVELOPMENT, PRODUCTION, LATEST or CI."); | |
} | |
define("UAT_ENV", $options["env"]); | |
$driver = new WebDriver("http://localhost:4444/wd/hub"); | |
if (!isset($options['browser'])) | |
{ | |
throw new Exception("Must define --browser option."); | |
} | |
$this->session = $driver->session(strtolower($options["browser"])); | |
$this->session->timeouts('implicit_wait', 5); | |
switch (UAT_ENV) | |
{ | |
case "DEVELOPMENT": | |
$url = "http://google.com/"; | |
break; | |
case "LATEST": | |
$url = "http://latest.google.com/"; | |
break; | |
default: | |
throw new Exception("Unknown --env option passed, don't have a root mapping for that one, i.e. have no base URL."); | |
} | |
$this->session->open($url); | |
} | |
public function tearDown() | |
{ | |
$this->session->close(); | |
unset($this->session); | |
parent::tearDown(); | |
} | |
public function testPOTD() | |
{ | |
$this->session->element('xpath', "//div//a[1]")->click(); | |
$this->assertTrue(strpos($this->session->url(), "hello") !== false, "Page didn't change to url including hello."); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment