diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4dd26d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +composer.lock +composer.phar +local.log +vendor/ +bin/ +tests/_output/* +tests/_support/_generated/* diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c2ff578 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: php + +php: + - '5.5' + - '5.6' + - '7.0' + +before_install: + - true && composer install + diff --git a/Parallel Testing/acceptance.suite.yml b/Parallel Testing/acceptance.suite.yml deleted file mode 100644 index b0e2f34..0000000 --- a/Parallel Testing/acceptance.suite.yml +++ /dev/null @@ -1,82 +0,0 @@ -# Codeception Test Suite Configuration - -# suite for acceptance tests. -# perform tests in browser using the WebDriver or PhpBrowser. -# If you need both WebDriver and PHPBrowser tests - create a separate suite. - -class_name: WebGuy - -env: - p1: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: chrome - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os' : 'Windows' - 'os_version' : '10' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - p2: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: firefox - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os' : 'OS X' - 'os_version' : 'El Capitan' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - - p3: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: ie - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os': 'Windows' - 'os_version': '8.1' - 'browser_version': '11.0' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - p4: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: safari - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os': 'OS X' - 'os_version': 'Yosemite' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' \ No newline at end of file diff --git a/Parallel Testing/composer.json b/Parallel Testing/composer.json deleted file mode 100644 index 5092de6..0000000 --- a/Parallel Testing/composer.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "require": { - "codegyre/robo": "*", - "codeception/robo-paracept": "@dev" - } -} diff --git a/README.md b/README.md index 9079776..8a1e846 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,35 @@ -# Codeception-BrowserStack -Run Codeception tests on BrowserStack. Sequentially and in parallel. - -### Setup a sample Codeception test to run on BrowserStack - - -1. Add _codecept.phar_ to a sample project folder by excuting the following command: `wget http://codeception.com/codecept.phar` or `curl -O http://codeception.com/codecept.phar` - -2. Execute `php codecept.phar bootstrap` - -3. Execute `php codecept.phar generate:cept acceptance Welcome` - -4. To configure the test, open and edit _./tests/acceptance.suite.yml_ and copy the file from _Sample Tests/acceptance.suite.yml_. - -5. Execute `php codecept.phar build`. - -6. Lets write a test to search BrowserStack on Google. Open and edit file _./tests/acceptance/WelcomeCept.php_ and copy the file from _Sample Tests/WelcomeCept.php._ - -7. To run a single test (on say Firefox), execute the command `php codecept.phar run --env firefox`. - -### Sequential Runs - -1. You can make use of [Environments](http://codeception.com/docs/07-AdvancedUsage#Environments), to execute tests on different browser and OS combinations. - -2. Check the _acceptance.suite.yml_ to see how the capabilities and environments are mentioned. - -3. To run sequentially of 4 environments, execute the command `php codecept.phar run --env chrome --env firefox --env ie --env safari` - -## Parallel Testing - -To run sample test in parallel, you need the following two things: -- [Robo](http://robo.li/), a task runner that executes your tests in parallel -- [robo-paracept](https://github.com/Codeception/robo-paracept) - Codeception tasks for parallel execution. - -You can follow these steps: - -1. Install 'Composer' if you haven't yet : `curl -sS https://getcomposer.org/installer | php` - -2. Add the _composer.json_ file present under _Parallel Testing_ folder to the root directory of the project. - -3. Execute the command `composer.json install` to install the two components mentioned above. - -4. Execute `Robo.phar` and press 'Y' to create an empty _RoboFile.php_. - -5. Copy the _RoboFile.php_ file under _Parallel Testing_ to the empty _RoboFile.php_. - -6. Change the env names to `p1`, `p2`... instead of `chrome`, `firefox`..., as given in _acceptance.suite.yml_ file in _Parallel testing_ folder - -7. The following function in _RoboFile_ executes tests in parallel: -``` -public function parallelRun() - { - $parallel = $this->taskParallelExec(); - for ($i = 1; $i <= 4; $i++) { - $parallel->process( - $this->taskCodecept() // use built-in Codecept task - ->suite('acceptance') // run acceptance tests - ->env("p$i") // in its own environment - ->xml("tests/_log/result_$i.xml") - ); - } - return $parallel->run(); - } -``` - -Execute the commmand `robo.phar parallel:run` to execute tests in parallel. - -It would be very confusing to have logs of multiple tests coming at once. Hence it is necessary to merge them. This is where robo-paracept's _MergeReports_ comes into play which merges all results into one file. The following function merges all the results: -``` -use \Codeception\Task\MergeReports; - - function parallelMergeResults() - { - $merge = $this->taskMergeXmlReports(); - for ($i=1; $i<=4; $i++) { - $merge->from("tests/_output/tests/_log/result_$i.xml"); - } - $merge->into("tests/_output/tests/_log/result.xml") - ->run(); - } -``` - -Execute the commmand `robo.phar parallel:merge-results` to merge results. - -You can put the two fuctions together in one function as follows: -``` - function parallelAll() - { - $result = $this->parallelRun(); - $this->parallelMergeResults(); - return $result; - } -``` -Execute the commmand `robo.phar parallel:all` to execute Codeception tests in parallel as well as merge results. - -More details on parallel execution in Codeception available [here](http://codeception.com/docs/12-ParallelExecution#.VkZCm98rJ7q). - - +# codeception-browserstack +[Codeception](http://codeception.com) Integration with BrowserStack. + + + + + + +## Setup +* Clone the repo +* Install dependencies `composer install` +* Update `tests/acceptance.suite.yml` file with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) + +## Running your tests +* To run a single test, run `composer single` +* To run local tests, run `composer local` +* To run parallel tests, run `composer parallel` + + Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) + +## Notes +* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate) +* To test on a different set of browsers, check out our [platform configurator](https://www.browserstack.com/automate/php#setting-os-and-browser) +* You can export the environment variables for the Username and Access Key of your BrowserStack account + + ``` + export BROWSERSTACK_USERNAME= && + export BROWSERSTACK_ACCESS_KEY= + ``` + +## Additional Resources +* [Documentation for writing Automate test scripts in PHP](https://www.browserstack.com/automate/php) +* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities) +* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate) +* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api) diff --git a/Parallel Testing/RoboFile.php b/RoboFile.php similarity index 67% rename from Parallel Testing/RoboFile.php rename to RoboFile.php index 620be28..b5c6c66 100644 --- a/Parallel Testing/RoboFile.php +++ b/RoboFile.php @@ -1,28 +1,37 @@ taskParallelExec(); - for ($i = 1; $i <= 4; $i++) { + for ($i = 0; $i < $this->numParallel; $i++) { $parallel->process( $this->taskCodecept() // use built-in Codecept task ->suite('acceptance') // run acceptance tests - ->env("p$i") // in its own environment + ->env("parallel_$i") // in its own environment + ->group("single") ->xml("tests/_log/result_$i.xml") ); } return $parallel->run(); } - function parallelMergeResults() + function parallelMergeResults() { $merge = $this->taskMergeXmlReports(); - for ($i=1; $i<=4; $i++) { + for ($i=0; $i<$this->numParallel; $i++) { $merge->from("tests/_output/tests/_log/result_$i.xml"); } $merge->into("tests/_output/tests/_log/result.xml") @@ -35,6 +44,5 @@ function parallelAll() $this->parallelMergeResults(); return $result; } - } -?> \ No newline at end of file +?> diff --git a/Sample Test/WelcomeCept.php b/Sample Test/WelcomeCept.php deleted file mode 100644 index 68f2108..0000000 --- a/Sample Test/WelcomeCept.php +++ /dev/null @@ -1,10 +0,0 @@ -wantTo('Search Browserstack on Google'); - $I->amOnPage('/'); - $I->see('Google'); - $I->fillField('q', 'BrowserStack'); - $I->click('btnG'); - $I->makeScreenshot('web_page'); - $I->see('BrowserStack'); -?> \ No newline at end of file diff --git a/Sample Test/acceptance.suite.yml b/Sample Test/acceptance.suite.yml deleted file mode 100644 index 17009f2..0000000 --- a/Sample Test/acceptance.suite.yml +++ /dev/null @@ -1,82 +0,0 @@ -# Codeception Test Suite Configuration - -# suite for acceptance tests. -# perform tests in browser using the WebDriver or PhpBrowser. -# If you need both WebDriver and PHPBrowser tests - create a separate suite. - -class_name: AcceptanceTester - -env: - chrome: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: chrome - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os' : 'Windows' - 'os_version' : '10' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - firefox: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: firefox - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os' : 'OS X' - 'os_version' : 'El Capitan' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - - ie: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: ie - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os': 'Windows' - 'os_version': '8.1' - 'browser_version': '11.0' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' - - safari: - modules: - enabled: - - WebDriver - config: - WebDriver: - url: 'http://www.google.com' - host: 'hub.browserstack.com' - port: 80 - browser: safari - capabilities: - 'browserstack.user': '' - 'browserstack.key' : '' - 'os': 'OS X' - 'os_version': 'Yosemite' - 'browserstack.debug': 'true' - 'build': 'Sample Codeception Tests' \ No newline at end of file diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..3a8fca8 --- /dev/null +++ b/codeception.yml @@ -0,0 +1,21 @@ +actor: Tester +paths: + tests: tests + log: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +settings: + bootstrap: _bootstrap.php + colors: true + memory_limit: 1024M +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Db: + dsn: '' + user: '' + password: '' + dump: tests/_data/dump.sql diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..be1d621 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "require": { + "codeception/codeception": "*", + "browserstack/browserstack-local": "dev-master", + "codegyre/robo": "*", + "codeception/robo-paracept": "@dev" + }, + "scripts": { + "test": "composer single && composer local && composer parallel", + "single": "vendor/bin/codecept run --env single -g single", + "local": "vendor/bin/codecept run --env local -g local", + "parallel": "vendor/bin/robo parallel:all" + }, + "autoload": { + "classmap": ["lib/"] + } +} diff --git a/lib/BrowserStackWebDriver.php b/lib/BrowserStackWebDriver.php new file mode 100644 index 0000000..e25cdfa --- /dev/null +++ b/lib/BrowserStackWebDriver.php @@ -0,0 +1,31 @@ +config["capabilities"]["browserstack.user"] = getenv('BROWSERSTACK_USERNAME')) : 0; + getenv('BROWSERSTACK_ACCESS_KEY') ? ($this->config["capabilities"]["browserstack.key"] = getenv('BROWSERSTACK_ACCESS_KEY')) : 0; + + if(array_key_exists("browserstack.local", $this->config["capabilities"]) && $this->config["capabilities"]["browserstack.local"]) + { + $bs_local_args = array("key" => $this->config["capabilities"]["browserstack.key"]); + $this->bs_local = new BrowserStack\Local(); + $this->bs_local->start($bs_local_args); + } + + parent::_initialize(); + } + + // HOOK: after suite + public function _afterSuite() { + parent::_afterSuite(); + if($this->bs_local) $this->bs_local->stop(); + } +} diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php new file mode 100644 index 0000000..617bf60 --- /dev/null +++ b/tests/_bootstrap.php @@ -0,0 +1,4 @@ +wantTo('Test BrowserStack Local Testing'); + $I->amOnPage('/check'); + $I->see('Up and running'); +?> diff --git a/tests/acceptance/SingleCept.php b/tests/acceptance/SingleCept.php new file mode 100644 index 0000000..63086a0 --- /dev/null +++ b/tests/acceptance/SingleCept.php @@ -0,0 +1,9 @@ +wantTo('Test Google\'s Search Functionality'); + $I->amOnPage('/ncr'); + $I->fillField('q', 'BrowserStack'); + $I->click('btnG'); + $I->seeInTitle('BrowserStack - Google Search'); +?> diff --git a/tests/acceptance/_bootstrap.php b/tests/acceptance/_bootstrap.php new file mode 100644 index 0000000..8a88555 --- /dev/null +++ b/tests/acceptance/_bootstrap.php @@ -0,0 +1,2 @@ +