Skip to content

Instantly share code, notes, and snippets.

@andrewwheal
Created April 21, 2015 09:59
Show Gist options
  • Save andrewwheal/847adcf19fa0e9366d53 to your computer and use it in GitHub Desktop.
Save andrewwheal/847adcf19fa0e9366d53 to your computer and use it in GitHub Desktop.
Abstract Selenium Test Case - allows Browserstack and browser configuration from environment variables
<?php
class AbstractTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
/**
* Array of browsers and platforms to run tests in
*
* @var array
*/
public static $browsers = array(
array(
'browserName' => 'firefox',
'desiredCapabilities' => array(
'version' => '35',
'os' => 'windows',
'os_version' => '8'
)
),
array(
'browserName' => 'chrome',
'desiredCapabilities' => array(
'version' => '39',
'os' => 'windows',
'os_version' => '7'
)
),
);
/**
* Array of default capabilities for tests
*
* @var array
*/
protected static $default_capabilities = [
'host' => 'hub.browserstack.com',
'port' => 80,
'desiredCapabilities' => [
'browserstack.user' => null,
'browserstack.key' => null,
// Enable visual logs in Browser Stack
'browserstack.debug' => true,
// Enable the use of the local connection
'browserstack.local' => true,
// We need this for running locally as we use self signed certificates
'acceptSslCerts' => true,
// Set the resolution higher than BrowserStack's default
'resolution' => "1280x1024",
]
];
/**
* Implementation of Fuel's Arr::get()
*
* We do not have access to Fuel from unit tests so we have a local version
* of its Arr::get() command
*
* @param array $array
* @param string|int $key
* @param mixed $default
*
* @return mixed
*/
protected static function arr_get($array, $key, $default = null)
{
if ( ! is_array($array) and ! $array instanceof \ArrayAccess) {
throw new \InvalidArgumentException('First parameter must be an array or ArrayAccess object.');
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
return $default;
}
/**
* Extension of PHPUnit_Extensions_Selenium2TestCase::suite() method
*
* This method is run before the browsers are set up and looped, thus
* allowing us to change them based on environment variables.
*
* This is also where we extend the configured browsers with the default
* capabilities, and set up Browserstack's project, build and name values
*
* Uses the environment variables:
* - SELENIUM_BROWSERS
* - BROWSERSTACK_USER
* - BROWSERSTACK_KEY
*
* @param string $className
*
* @return \PHPUnit_Extensions_SeleniumTestSuite
*/
public static function suite($className)
{
// If SELENIUM_BROWSERS environment variable has been set (used by Jenkins)
// then override the browsers set in code
if ($browsers = static::arr_get($_SERVER, 'SELENIUM_BROWSERS')) {
$browsers = json_decode($browsers, true);
if ( ! $browsers) {
echo "\n\nCOULD NOT PARSE SELENIUM_BROWSERS ENVIRONMENT VARIABLE\n\n";
// run parent and break out
return parent::suite($className);
}
static::$browsers = [];
foreach ($browsers as $browser) {
static::$browsers[] = [
'browserName' => $browser['browserName'],
'desiredCapabilities' => $browser,
];
}
}
foreach (static::$browsers as &$browser) {
// Always merge in defaults
$browser = array_replace_recursive(
static::$default_capabilities,
$browser
);
$browser['desiredCapabilities']['browserstack.user'] = static::arr_get($_SERVER, 'BROWSERSTACK_USER');
$browser['desiredCapabilities']['browserstack.key'] = static::arr_get($_SERVER, 'BROWSERSTACK_KEY');
// Always add/override consistent project/build/name values to all browsers
$project = explode('/', __DIR__)[4];
$browser['desiredCapabilities']['project'] = $project;
$browser['desiredCapabilities']['build'] = $project;
$browser['desiredCapabilities']['name'] = get_called_class();
}
return parent::suite($className);
}
/**
* Setup an individual test
*
* We add the test name to the Browserstack name value to identify
* individual tests within a test class
*
* This is where you might need to do extra setup for tests such as
* navigating to the website and setting timeouts
*/
public function setUp()
{
// Add test name to BrowserStack name
$desiredCapabilities = $this->getDesiredCapabilities();
$desiredCapabilities['name'] .= '::' . $this->getName();
$this->setDesiredCapabilities($desiredCapabilities);
parent::setUp();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment