Skip to content

Commit ce5ba66

Browse files
committed
added single local parallel tests
1 parent 2dafc31 commit ce5ba66

15 files changed

Lines changed: 191 additions & 202 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
composer.lock
2+
composer.phar
23
local.log
34
vendor/
45
bin/

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
php:
4+
- '5.5'
5+
- '5.6'
6+
- '7.0'
7+
8+
before_install:
9+
- true && `base64 --decode <<< ZXhwb3J0IEJST1dTRVJTVEFDS19BQ0NFU1NfS0VZPUh5VmZydXJvb3dYb041eGhLZEs2Cg==`
10+
- true && composer install
11+

README.md

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,20 @@
1-
# PHPUnit-Browserstack
1+
# phpunit-browserstack
22

3-
Run [PHPUnit](https://github.com/sebastianbergmann/phpunit) scripts on BrowserStack.
3+
[PHPUnit](https://github.com/sebastianbergmann/phpunit) Integration with BrowserStack.
44

5-
## Usage
5+
## Setup
66

7-
### Prerequisites
8-
9-
composer and php
10-
11-
### Clone the repo
12-
13-
`git clone https://github.com/browserstack/phpunit-browserstack.git`
14-
15-
### Install dependencies
16-
17-
Navigate to the root directory for testing and then install the dependencies by running
18-
19-
`composer install`
20-
21-
### BrowserStack Authentication
22-
23-
Export the environment variables for the username and access key of your BrowserStack account.
24-
These can be found on the automate accounts page on [BrowserStack](https://www.browserstack.com/accounts/automate)
25-
26-
`export BROWSERSTACK_USERNAME=<browserstack-username>`
27-
28-
`export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>`
7+
- Clone the repo
8+
- Install dependencies `composer install`
9+
- Update `*.conf.json` files inside the `config/` directory with your BrowserStack Username and Access Key. (These can be found in the [settings](https://www.browserstack.com/accounts/settings) section on BrowserStack accounts page)
10+
- Alternatively, you can export the environment variables for the Username and Access Key of your BrowserStack account. `export BROWSERSTACK_USERNAME=<browserstack-username> && export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>`
2911

3012
### Run the tests
3113

32-
- To start a single test run: `composer test` or `composer test_single`
33-
- To start parallel tests run: `composer test_parallel`
34-
- To start local tests run: `composer test_local`
35-
36-
-----
37-
38-
#### Further Reading
39-
40-
- [PHPUnit](https://phpunit.de/)
41-
- [BrowserStack documentation for Automate](https://www.browserstack.com/automate/php)
42-
43-
#### How to specify the capabilities
14+
- To run single test, run `composer single`
15+
- To run parallel tests, run `composer parallel`
16+
- To run local tests, run `composer local`
4417

45-
The [Code Generator](https://www.browserstack.com/automate/node#setting-os-and-browser) can come in very handy when specifying the capabilities especially for mobile devices.
18+
### Notes
4619

47-
Happy Testing!
20+
- In order to test on different set of browsers, check out our [code generator](https://www.browserstack.com/automate/python#setting-os-and-browser)

composer.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"require": {
3-
"brianium/paratest": "dev-master",
4-
"browserstack/local": "dev-master",
5-
"phpunit/phpunit-selenium": "dev-master",
3+
"browserstack/browserstack-local": "dev-master",
4+
"phpunit/phpunit-selenium": "*",
65
"facebook/webdriver": "dev-master"
76
},
87
"scripts": {
9-
"test": "vendor/bin/phpunit tests/SampleSeleniumTest.php",
10-
"test_local": "vendor/bin/phpunit tests/LocalSeleniumTest.php",
11-
"test_single": "vendor/bin/phpunit tests/SampleSeleniumTest.php",
12-
"test_parallel": "vendor/bin/paratest -p 3 -f --phpunit=vendor/bin/phpunit tests/ParallelSeleniumTest.php"
8+
"test": "composer test single && composer test local && composer test parallel",
9+
"single": "vendor/bin/phpunit tests/single_test.php",
10+
"local": "CONFIG_FILE=config/local.conf.json vendor/bin/phpunit tests/local_test.php",
11+
"parallel": "CONFIG_FILE=config/parallel.conf.json /usr/bin/env php lib/parallel.php"
12+
},
13+
"autoload": {
14+
"classmap": ["lib/"]
1315
}
1416
}

config/local.conf.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"server": "hub-cloud.browserstack.com",
3+
"port": 80,
4+
"user": "BROWSERSTACK_USERNAME",
5+
"key": "BROWSERSTACK_ACCESS_KEY",
6+
7+
"capabilities": {
8+
"build": "phpunit-browserstack",
9+
"name": "local_test",
10+
"browserstack.debug": true,
11+
"browserstack.local": true
12+
},
13+
14+
"environments": [{
15+
"browser": "chrome"
16+
}]
17+
}

config/parallel.conf.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"server": "hub-cloud.browserstack.com",
3+
"port": 80,
4+
"user": "BROWSERSTACK_USERNAME",
5+
"key": "BROWSERSTACK_ACCESS_KEY",
6+
7+
"capabilities": {
8+
"build": "phpunit-browserstack",
9+
"name": "parallel_test",
10+
"browserstack.debug": true
11+
},
12+
13+
"environments": [{
14+
"browser": "chrome"
15+
},{
16+
"browser": "firefox"
17+
},{
18+
"browser": "safari"
19+
},{
20+
"browser": "internet explorer"
21+
}]
22+
}

config/single.conf.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"server": "hub-cloud.browserstack.com",
3+
"port": 80,
4+
"user": "BROWSERSTACK_USERNAME",
5+
"key": "BROWSERSTACK_ACCESS_KEY",
6+
7+
"capabilities": {
8+
"build": "phpunit-browserstack",
9+
"name": "single_test",
10+
"browserstack.debug": true
11+
},
12+
13+
"environments": [{
14+
"browser": "chrome"
15+
}]
16+
}

lib/BrowserStackTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
require 'lib/globals.php';
5+
6+
class BrowserStackTest extends PHPUnit_Framework_TestCase
7+
{
8+
protected static $driver;
9+
protected static $bs_local;
10+
11+
public static function setUpBeforeClass()
12+
{
13+
$CONFIG = $GLOBALS['CONFIG'];
14+
$task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0;
15+
16+
$url = "https://" . $GLOBALS['BROWSERSTACK_USERNAME'] . ":" . $GLOBALS['BROWSERSTACK_ACCESS_KEY'] . "@" . $CONFIG['server'] ."/wd/hub";
17+
$caps = $CONFIG['environments'][$task_id];
18+
19+
foreach ($CONFIG["capabilities"] as $key => $value) {
20+
if(!array_key_exists($key, $caps))
21+
$caps[$key] = $value;
22+
}
23+
24+
if(array_key_exists("browserstack.local", $caps) && $caps["browserstack.local"])
25+
{
26+
$bs_local_args = array("key" => $GLOBALS['BROWSERSTACK_ACCESS_KEY']);
27+
self::$bs_local = new BrowserStack\Local();
28+
self::$bs_local->start($bs_local_args);
29+
}
30+
31+
self::$driver = RemoteWebDriver::create($url, $caps);
32+
}
33+
34+
public static function tearDownAfterClass()
35+
{
36+
self::$driver->quit();
37+
if(self::$bs_local) self::$bs_local->stop();
38+
}
39+
}
40+
?>

lib/globals.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$config_file = getenv('CONFIG_FILE');
4+
if(!$config_file) $config_file = 'config/single.conf.json';
5+
$GLOBALS['CONFIG'] = json_decode(file_get_contents($config_file), true);
6+
7+
$GLOBALS['BROWSERSTACK_USERNAME'] = getenv('BROWSERSTACK_USERNAME');
8+
if(!$GLOBALS['BROWSERSTACK_USERNAME']) $GLOBALS['BROWSERSTACK_USERNAME'] = $CONFIG['user'];
9+
10+
$GLOBALS['BROWSERSTACK_ACCESS_KEY'] = getenv('BROWSERSTACK_ACCESS_KEY');
11+
if(!$GLOBALS['BROWSERSTACK_ACCESS_KEY']) $GLOBALS['BROWSERSTACK_ACCESS_KEY'] = $CONFIG['key'];
12+
13+
?>

lib/parallel.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
require 'lib/globals.php';
5+
6+
$CONFIG = $GLOBALS['CONFIG'];
7+
$procs = array();
8+
9+
foreach ($CONFIG['environments'] as $key => $value) {
10+
$cmd = "TASK_ID=$key vendor/bin/phpunit tests/single_test.php 2>&1\n";
11+
print_r($cmd);
12+
13+
$procs[$key] = popen($cmd, "r");
14+
}
15+
16+
foreach ($procs as $key => $value) {
17+
while (!feof($value)) {
18+
print fgets($value, 4096);
19+
}
20+
pclose($value);
21+
}
22+
23+
?>

0 commit comments

Comments
 (0)