From 52ac09813b80f576b5667393170ddbb69acd39b3 Mon Sep 17 00:00:00 2001 From: Harish Ved Date: Tue, 3 May 2016 16:34:02 +0530 Subject: [PATCH] restructure --- .gitignore | 1 + README.md | 44 ++++++++-- composer.json | 14 ++++ parallel-tests-phpunit/README.md | 87 -------------------- parallel-tests-phpunit/composer.json | 6 -- parallel-tests-phpunit/phpunit.xml | 17 ---- parallel-tests-phpunit/test/SeleniumTest.php | 66 --------------- tests-phpunit/README.md | 84 ------------------- tests-phpunit/composer.json | 6 -- tests-phpunit/test/SeleniumTest.php | 55 ------------- tests/LocalSeleniumTest.php | 52 ++++++++++++ tests/ParallelSeleniumTest.php | 71 ++++++++++++++++ tests/SampleSeleniumTest.php | 33 ++++++++ 13 files changed, 210 insertions(+), 326 deletions(-) create mode 100644 composer.json delete mode 100644 parallel-tests-phpunit/README.md delete mode 100644 parallel-tests-phpunit/composer.json delete mode 100644 parallel-tests-phpunit/phpunit.xml delete mode 100644 parallel-tests-phpunit/test/SeleniumTest.php delete mode 100644 tests-phpunit/README.md delete mode 100644 tests-phpunit/composer.json delete mode 100644 tests-phpunit/test/SeleniumTest.php create mode 100644 tests/LocalSeleniumTest.php create mode 100644 tests/ParallelSeleniumTest.php create mode 100644 tests/SampleSeleniumTest.php diff --git a/.gitignore b/.gitignore index 282c2ed..56908e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock +local.log vendor/ bin/ diff --git a/README.md b/README.md index 553f3fa..b0d9026 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,47 @@ -phpunit-browserstack -========= +# PHPUnit-Browserstack -This repository provides information and helpful tweaks to run your PhpUnit tests on the BrowserStack selenium cloud infrastructure. +Run [PHPUnit](https://github.com/sebastianbergmann/phpunit) scripts on BrowserStack. -You can run parallel as well as single test on the BrowserStack. Follow the steps in the respective folders for further information. +## Usage +### Prerequisites + +composer and php + +### Clone the repo + +`git clone https://github.com/browserstack/phpunit-browserstack.git` + +### Install dependencies + +Navigate to the root directory for testing and then install the dependencies by running + +`composer install` + +### BrowserStack Authentication + +Export the environment variables for the username and access key of your BrowserStack account. +These can be found on the automate accounts page on [BrowserStack](https://www.browserstack.com/accounts/automate) + +`export BROWSERSTACK_USERNAME=` + +`export BROWSERSTACK_KEY=` + +### Run the tests + + - To start a single test run: `composer test` or `composer test_single` + - To start parallel tests run: `composer test_parallel` + - To start local tests run: `composer test_local` + +----- + +#### Further Reading -###Further Reading - [PHPUnit](https://phpunit.de/) - [BrowserStack documentation for Automate](https://www.browserstack.com/automate/php) +#### How to specify the capabilities + +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. + Happy Testing! diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..15dac02 --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "require": { + "brianium/paratest": "dev-master", + "browserstack/local": "dev-master", + "phpunit/phpunit-selenium": "dev-master", + "facebook/webdriver": "dev-master" + }, + "scripts": { + "test": "vendor/bin/phpunit tests/SampleSeleniumTest.php", + "test_local": "vendor/bin/phpunit tests/LocalSeleniumTest.php", + "test_single": "vendor/bin/phpunit tests/SampleSeleniumTest.php", + "test_parallel": "vendor/bin/paratest -p 3 -f --phpunit=vendor/bin/phpunit tests/ParallelSeleniumTest.php" + } +} diff --git a/parallel-tests-phpunit/README.md b/parallel-tests-phpunit/README.md deleted file mode 100644 index 9fe267c..0000000 --- a/parallel-tests-phpunit/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# Documentation to write parallel tests in PHP - -1. Install composer on your computer. Please follow instructions given on http://getcomposer.org/doc/00-intro.md. For this documentation composer.phar was renamed as composer and placed in /usr/local/bin(could be placed in any folder included in system path). -2. In your composer.json add enteries for paratest and webdriver. PHP Unit is included as dependency of paratest so will be also installed. This composer.json should be placed in project root and should look like: -``` -{ - "require": { - "brianium/paratest": "dev-master", - "facebook/webdriver": "0.1" - } -} -``` -for php5.4+ please use dev-master version of facebook/webdriver and for php versions less then 5.3 please use facebook/webdriver version 0.1. -3. Install dependencies using composure. Please call bellow command in the project root containing composer.json -``` -composer install -``` -4. Or we can update dependencies using -``` -composer update -``` -5. To use PHPUnit test please create phpunit.xml and place following code in it: -``` - - - - - - ./test/ - - - -``` -testsuites entry is used to integrate ParaTest with PHPUnit. In this tutorial tests are placed in test folder placed in the root folder of project. -6. Create test folder in project root if not present and then create SeleniumTest.php with following code in test folder -``` -"WINDOWS") - ); - $web_driver->get("http://www.google.com"); - print $web_driver->getTitle(); - $element = $web_driver->findElement(WebDriverBy::name("q")); - if ($element) { - $element->sendKeys("Browserstack"); - $element->submit(); - } - $web_driver->quit(); - } - public function testBrowserStack() { - $web_driver = new RemoteWebDriver( - "http://{your-username}:{your-passkey}@hub.browserstack.com/wd/hub", - array("platform"=>"WINDOWS") - ); - $web_driver->get("http://www.browserstack.com"); - print $web_driver->getTitle(); - $web_driver->quit(); - } - public function testFacebook() { - $web_driver = new RemoteWebDriver( - "http://{your-username}:{your-passkey}@hub.browserstack.com/wd/hub", - array("platform"=>"WINDOWS") - ); - $web_driver->get("http://www.facebook.com"); - print $web_driver->getTitle(); - $web_driver->quit(); - } -} -?> -``` -7. Replace BROWSERSTACK_KEY and BROWSERSTACK_USERNAME with your BrowserStack Username and access key to run the tests. -To run this UnitTests in parallel please call following command: -``` -vendor/bin/paratest -p 3 -f --phpunit=vendor/bin/phpunit test/SeleniumTest.php -``` \ No newline at end of file diff --git a/parallel-tests-phpunit/composer.json b/parallel-tests-phpunit/composer.json deleted file mode 100644 index 6f3ea65..0000000 --- a/parallel-tests-phpunit/composer.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "require": { - "brianium/paratest": "dev-master", - "facebook/webdriver": "dev-master" - } -} diff --git a/parallel-tests-phpunit/phpunit.xml b/parallel-tests-phpunit/phpunit.xml deleted file mode 100644 index 80b81d8..0000000 --- a/parallel-tests-phpunit/phpunit.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - ./test/ - - - diff --git a/parallel-tests-phpunit/test/SeleniumTest.php b/parallel-tests-phpunit/test/SeleniumTest.php deleted file mode 100644 index faf8389..0000000 --- a/parallel-tests-phpunit/test/SeleniumTest.php +++ /dev/null @@ -1,66 +0,0 @@ - "IE", - "browser_version" => "9.0", - "os" => "Windows", - "os_version" => "7", - "browserstack.debug" => "true" - ); - $web_driver = RemoteWebDriver::create( - $url, - $caps - ); - $web_driver->get("http://www.google.com"); - print $web_driver->getTitle(); - $element = $web_driver->findElement(WebDriverBy::name("q")); - if ($element) { - $element->sendKeys("Browserstack"); - $element->submit(); - } - $web_driver->quit(); - } - public function testBrowserStack() { - $url = "http://" . self::$user_id . ":" . self::$security_key . "@hub.browserstack.com/wd/hub"; - $caps = array( - "browser" => "IE", - "browser_version" => "9.0", - "os" => "Windows", - "os_version" => "7", - "browserstack.debug" => "true" - ); - $web_driver = RemoteWebDriver::create( - $url, - $caps - ); - $web_driver->get("http://www.browserstack.com"); - print $web_driver->getTitle(); - $web_driver->quit(); - } - public function testFacebook() { - $url = "http://" . self::$user_id . ":" . self::$security_key . "@hub.browserstack.com/wd/hub"; - $caps = array( - "browser" => "IE", - "browser_version" => "9.0", - "os" => "Windows", - "os_version" => "7", - "browserstack.debug" => "true" - ); - $web_driver = RemoteWebDriver::create( - $url, - $caps - ); - $web_driver->get("http://www.facebook.com"); - print $web_driver->getTitle(); - $web_driver->quit(); - } -} -?> diff --git a/tests-phpunit/README.md b/tests-phpunit/README.md deleted file mode 100644 index 9a1e427..0000000 --- a/tests-phpunit/README.md +++ /dev/null @@ -1,84 +0,0 @@ -# Documentation to write simple tests in PHP - -1. Install composer on your computer. Please follow instructions given on http://getcomposer.org/doc/00-intro.md. For this documentation composer.phar was renamed as composer and placed in /usr/local/bin(could be placed in any folder included in system path). -2. In your composer.json add enteries for phpunit and webdriver. This composer.json should be placed in project root and should look like: -``` -{ - "require": { - "phpunit/phpunit-selenium": "dev-master", - "facebook/webdriver": "dev-master" - } -} -``` -for php5.4+ please use dev-master version of facebook/webdriver and for php versions less then 5.3 please use facebook/webdriver version 0.1. -3. Install dependencies using composure. Please call bellow command in the project root containing composer.json -``` -composer install -``` -4. Or we can update dependencies using -``` -composer update -``` -5. Create test folder in project root if not present and then create SeleniumTest.php with following code in test folder -``` - 'chrome', - 'host' => 'hub.browserstack.com', - 'port' => 80, - 'desiredCapabilities' => array( - 'version' => '30', - 'browserstack.user' => BROWSERSTACK_USER, - 'browserstack.key' => BROWSERSTACK_KEY, - 'os' => 'OS X', - 'os_version' => 'Mountain Lion' - ) - ), - array( - 'browserName' => 'chrome', - 'host' => 'hub.browserstack.com', - 'port' => 80, - 'desiredCapabilities' => array( - 'version' => '30', - 'browserstack.user' => BROWSERSTACK_USER, - 'browserstack.key' => BROWSERSTACK_KEY, - 'os' => 'Windows', - 'os_version' => '8.1' - ) - ) - ); - protected function setUp() - { - parent::setUp(); - $this->setBrowserUrl('http://www.example.com/'); - } - - public function testTitle() - { - $this->url('http://www.example.com/'); - $this->assertEquals('Example Domain', $this->title()); - } - public function testGoogle() - { - $this->url('http://www.google.com/'); - $element = $this->byName('q'); - $element->click(); - $this->keys('Browserstack'); - $button = $this->byName('btnG'); - $button->click(); - $this->assertEquals('Browserstack - Google zoeken', $this->title()); - } -} -?> -``` -6. Replace BROWSERSTACK_KEY and BROWSERSTACK_USERNAME with your BrowserStack Username and access key to run the tests. -To run this UnitTests in please call following command: -``` -vendor/bin/phpunit test/ -``` \ No newline at end of file diff --git a/tests-phpunit/composer.json b/tests-phpunit/composer.json deleted file mode 100644 index 3e2b3de..0000000 --- a/tests-phpunit/composer.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "require": { - "phpunit/phpunit-selenium": "dev-master", - "facebook/webdriver": "dev-master" - } -} diff --git a/tests-phpunit/test/SeleniumTest.php b/tests-phpunit/test/SeleniumTest.php deleted file mode 100644 index c073c6f..0000000 --- a/tests-phpunit/test/SeleniumTest.php +++ /dev/null @@ -1,55 +0,0 @@ - 'chrome', - 'host' => 'hub.browserstack.com', - 'port' => 80, - 'desiredCapabilities' => array( - 'version' => '30', - 'browserstack.user' => BROWSERSTACK_USER, - 'browserstack.key' => BROWSERSTACK_KEY, - 'os' => 'OS X', - 'os_version' => 'Mountain Lion' - ) - ), - array( - 'browserName' => 'chrome', - 'host' => 'hub.browserstack.com', - 'port' => 80, - 'desiredCapabilities' => array( - 'version' => '30', - 'browserstack.user' => BROWSERSTACK_USER, - 'browserstack.key' => BROWSERSTACK_KEY, - 'os' => 'Windows', - 'os_version' => '8.1' - ) - ) - ); - protected function setUp() - { - parent::setUp(); - $this->setBrowserUrl('http://www.example.com/'); - } - - public function testTitle() - { - $this->url('http://www.example.com/'); - $this->assertEquals('Example Domain', $this->title()); - } - public function testGoogle() - { - $this->url('http://www.google.com/'); - $element = $this->byName('q'); - $element->click(); - $this->keys('Browserstack'); - $button = $this->byName('btnG'); - $button->click(); - $this->assertEquals('Browserstack - Google zoeken', $this->title()); - } -} -?> \ No newline at end of file diff --git a/tests/LocalSeleniumTest.php b/tests/LocalSeleniumTest.php new file mode 100644 index 0000000..2680a24 --- /dev/null +++ b/tests/LocalSeleniumTest.php @@ -0,0 +1,52 @@ + BROWSERSTACK_ACCESS_KEY, "forcelocal" => true); + self::$bs_local->start($bs_local_args); + } + + public function testGoogle() { + $url = "https://" . BROWSERSTACK_USERNAME . ":" . BROWSERSTACK_ACCESS_KEY . "@hub.browserstack.com/wd/hub"; + $caps = array( + "build" => "Sample phpunit Tests", + "name" => "sample local phpunit tests", + "browser" => "IE", + "browser_version" => "9.0", + "os" => "Windows", + "os_version" => "7", + "browserstack.local" => true, + "browserstack.debug" => "true" + ); + $web_driver = RemoteWebDriver::create( + $url, + $caps + ); + $web_driver->get("http://www.google.com"); + echo $web_driver->getTitle(); + $element = $web_driver->findElement(WebDriverBy::name("q")); + if ($element) { + $element->sendKeys("Browserstack"); + $element->submit(); + } + $web_driver->quit(); + } + + public static function tearDownAfterClass() { + if(!is_null(self::$bs_local) && self::$bs_local->isRunning()) { + self::$bs_local->stop(); + } + } +} +?> diff --git a/tests/ParallelSeleniumTest.php b/tests/ParallelSeleniumTest.php new file mode 100644 index 0000000..189f299 --- /dev/null +++ b/tests/ParallelSeleniumTest.php @@ -0,0 +1,71 @@ + "Sample phpunit Tests", + "name" => "phpunit parallel tests", + "browser" => "IE", + "browser_version" => "9.0", + "os" => "Windows", + "os_version" => "7", + "browserstack.debug" => "true" + ); + $web_driver = RemoteWebDriver::create( + $url, + $caps + ); + $web_driver->get("http://www.google.com"); + print $web_driver->getTitle(); + $element = $web_driver->findElement(WebDriverBy::name("q")); + if ($element) { + $element->sendKeys("Browserstack"); + $element->submit(); + } + $web_driver->quit(); + } + public function testBrowserStack() { + $url = "https://" . BROWSERSTACK_USERNAME . ":" . BROWSERSTACK_ACCESS_KEY . "@hub.browserstack.com/wd/hub"; + $caps = array( + "build" => "Sample phpunit Tests", + "name" => "phpunit parallel tests", + "browser" => "Safari", + "browser_version" => "9.0", + "os" => "OS X", + "os_version" => "El Capitan", + "browserstack.debug" => "true" + ); + $web_driver = RemoteWebDriver::create( + $url, + $caps + ); + $web_driver->get("http://www.browserstack.com"); + print $web_driver->getTitle(); + $web_driver->quit(); + } + public function testFacebook() { + $url = "https://" . BROWSERSTACK_USERNAME . ":" . BROWSERSTACK_ACCESS_KEY . "@hub.browserstack.com/wd/hub"; + $caps = array( + "build" => "Sample phpunit Tests", + "name" => "phpunit parallel tests", + "browser" => "edge", + "browser_version" => "13.0", + "os" => "Windows", + "os_version" => "10", + "browserstack.debug" => "true" + ); + $web_driver = RemoteWebDriver::create( + $url, + $caps + ); + $web_driver->get("http://www.facebook.com"); + print $web_driver->getTitle(); + $web_driver->quit(); + } +} +?> diff --git a/tests/SampleSeleniumTest.php b/tests/SampleSeleniumTest.php new file mode 100644 index 0000000..cdde693 --- /dev/null +++ b/tests/SampleSeleniumTest.php @@ -0,0 +1,33 @@ + "Sample phpunit Tests", + "name" => "sample phpunit tests", + "browser" => "IE", + "browser_version" => "9.0", + "os" => "Windows", + "os_version" => "7", + "browserstack.debug" => "true" + ); + $web_driver = RemoteWebDriver::create( + $url, + $caps + ); + $web_driver->get("http://www.google.com"); + print $web_driver->getTitle(); + $element = $web_driver->findElement(WebDriverBy::name("q")); + if ($element) { + $element->sendKeys("Browserstack"); + $element->submit(); + } + $web_driver->quit(); + } +} +?>