Skip to content

Commit 701c090

Browse files
authored
Merge pull request #5 from browserstack/spec_v1
Spec v1
2 parents 2dafc31 + 2e823f6 commit 701c090

15 files changed

Lines changed: 211 additions & 210 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: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
1-
# PHPUnit-Browserstack
2-
3-
Run [PHPUnit](https://github.com/sebastianbergmann/phpunit) scripts on BrowserStack.
4-
5-
## Usage
6-
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>`
29-
30-
### Run the tests
31-
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
44-
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.
46-
47-
Happy Testing!
1+
# phpunit-browserstack
2+
3+
[PHPUnit](https://github.com/sebastianbergmann/phpunit) Integration with BrowserStack
4+
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
6+
7+
## Setup
8+
* Clone the repo
9+
* Install dependencies `composer install`
10+
* Update `*.conf.json` files inside the `config/` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
11+
12+
## Running your tests
13+
* To run a single test, run `composer single`
14+
* To run parallel tests, run `composer parallel`
15+
* To run local tests, run `composer local`
16+
17+
## Notes
18+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
19+
* To test on a different set of browsers, check out our [platform configurator](https://www.browserstack.com/automate/php#setting-os-and-browser)
20+
* You can export the environment variables for the Username and Access Key of your BrowserStack account
21+
22+
```
23+
export BROWSERSTACK_USERNAME=<browserstack-username> &&
24+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
25+
```
26+
27+
## Additional Resources
28+
* [Documentation for writing Automate test scripts in PHP](https://www.browserstack.com/automate/php)
29+
* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities)
30+
* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate)
31+
* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api)

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

config/parallel.conf.json

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

config/single.conf.json

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

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)