Skip to content

Commit 50e6f6d

Browse files
committed
phpunit
0 parents  commit 50e6f6d

8 files changed

Lines changed: 242 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
vendor/
3+
bin/

parallel-tests-phpunit/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Documentation to write parallel tests in PHP
2+
3+
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).
4+
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:
5+
```
6+
{
7+
"require": {
8+
"brianium/paratest": "dev-master",
9+
"facebook/webdriver": "0.1"
10+
}
11+
}
12+
```
13+
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.
14+
3. Install dependencies using composure. Please call bellow command in the project root containing composer.json
15+
```
16+
composer install
17+
```
18+
4. Or we can update dependencies using
19+
```
20+
composer update
21+
```
22+
5. To use PHPUnit test please create phpunit.xml and place following code in it:
23+
```
24+
<?xml version="1.0" encoding="UTF-8"?>
25+
<phpunit backupGlobals="false"
26+
backupStaticAttributes="false"
27+
colors="true"
28+
convertErrorsToExceptions="true"
29+
convertNoticesToExceptions="true"
30+
convertWarningsToExceptions="true"
31+
processIsolation="false"
32+
stopOnFailure="false"
33+
syntaxCheck="false">
34+
35+
<testsuites>
36+
<testsuite name="ParaTest Fixtures">
37+
<directory>./test/</directory>
38+
</testsuite>
39+
</testsuites>
40+
</phpunit>
41+
```
42+
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.
43+
6. Create test folder in project root if not present and then create SeleniumTest.php with following code in test folder
44+
```
45+
<?php
46+
require 'vendor/autoload.php';
47+
class SeleniumTest extends PHPUnit_Framework_TestCase {
48+
public function testGoogle() {
49+
$web_driver = new RemoteWebDriver(
50+
"http://{your-username}:{your-passkey}@hub.browserstack.com/wd/hub",
51+
array("platform"=>"WINDOWS")
52+
);
53+
$web_driver->get("http://www.google.com");
54+
print $web_driver->getTitle();
55+
$element = $web_driver->findElement(WebDriverBy::name("q"));
56+
if ($element) {
57+
$element->sendKeys("Browserstack");
58+
$element->submit();
59+
}
60+
$web_driver->quit();
61+
}
62+
public function testBrowserStack() {
63+
$web_driver = new RemoteWebDriver(
64+
"http://{your-username}:{your-passkey}@hub.browserstack.com/wd/hub",
65+
array("platform"=>"WINDOWS")
66+
);
67+
$web_driver->get("http://www.browserstack.com");
68+
print $web_driver->getTitle();
69+
$web_driver->quit();
70+
}
71+
public function testFacebook() {
72+
$web_driver = new RemoteWebDriver(
73+
"http://{your-username}:{your-passkey}@hub.browserstack.com/wd/hub",
74+
array("platform"=>"WINDOWS")
75+
);
76+
$web_driver->get("http://www.facebook.com");
77+
print $web_driver->getTitle();
78+
$web_driver->quit();
79+
}
80+
}
81+
?>
82+
```
83+
7. To run this UnitTests in parallel please call following command:
84+
```
85+
vendor/bin/paratest -p 3 -f --phpunit=vendor/bin/phpunit test/SeleniumTest.php
86+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"brianium/paratest": "dev-master",
4+
"facebook/webdriver": "dev-master"
5+
}
6+
}

parallel-tests-phpunit/phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false">
11+
12+
<testsuites>
13+
<testsuite name="ParaTest Fixtures">
14+
<directory>./test/</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
class SeleniumTest extends PHPUnit_Framework_TestCase {
5+
6+
protected static $user_id = "<Your Userid>";
7+
protected static $security_key = "<Your Security Key>";
8+
9+
public function testGoogle() {
10+
$url = "http://" . self::$user_id . ":" . self::$security_key . "@hub.browserstack.com/wd/hub";
11+
$caps = array(
12+
"browser" => "IE",
13+
"browser_version" => "9.0",
14+
"os" => "Windows",
15+
"os_version" => "7",
16+
"browserstack.debug" => "true"
17+
);
18+
$web_driver = RemoteWebDriver::create(
19+
$url,
20+
$caps
21+
);
22+
$web_driver->get("http://www.google.com");
23+
print $web_driver->getTitle();
24+
$element = $web_driver->findElement(WebDriverBy::name("q"));
25+
if ($element) {
26+
$element->sendKeys("Browserstack");
27+
$element->submit();
28+
}
29+
$web_driver->quit();
30+
}
31+
public function testBrowserStack() {
32+
$url = "http://" . self::$user_id . ":" . self::$security_key . "@hub.browserstack.com/wd/hub";
33+
$caps = array(
34+
"browser" => "IE",
35+
"browser_version" => "9.0",
36+
"os" => "Windows",
37+
"os_version" => "7",
38+
"browserstack.debug" => "true"
39+
);
40+
$web_driver = RemoteWebDriver::create(
41+
$url,
42+
$caps
43+
);
44+
$web_driver->get("http://www.browserstack.com");
45+
print $web_driver->getTitle();
46+
$web_driver->quit();
47+
}
48+
public function testFacebook() {
49+
$url = "http://" . self::$user_id . ":" . self::$security_key . "@hub.browserstack.com/wd/hub";
50+
$caps = array(
51+
"browser" => "IE",
52+
"browser_version" => "9.0",
53+
"os" => "Windows",
54+
"os_version" => "7",
55+
"browserstack.debug" => "true"
56+
);
57+
$web_driver = RemoteWebDriver::create(
58+
$url,
59+
$caps
60+
);
61+
$web_driver->get("http://www.facebook.com");
62+
print $web_driver->getTitle();
63+
$web_driver->quit();
64+
}
65+
}
66+
?>

tests-phpunit/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Phpunit testing using Selenium2TestCase
2+
1. Do composer install
3+
2. To run test execute in shell vendor/bin/phpunit test/

tests-phpunit/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"phpunit/phpunit-selenium": "dev-master",
4+
"facebook/webdriver": "dev-master"
5+
}
6+
}

tests-phpunit/test/WebTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
define('BROWSERSTACK_USER', '<Set BrowserStack Username>');
4+
define('BROWSERSTACK_KEY', '<Set BrowserStack Key>');
5+
class WebTest extends PHPUnit_Extensions_Selenium2TestCase
6+
{
7+
public static $browsers = array(
8+
array(
9+
'browserName' => 'chrome',
10+
'host' => 'hub.browserstack.com',
11+
'port' => 80,
12+
'desiredCapabilities' => array(
13+
'version' => '30',
14+
'browserstack.user' => BROWSERSTACK_USER,
15+
'browserstack.key' => 'XQWDewaJsUzqYJRv8zhr',
16+
'os' => 'OS X',
17+
'os_version' => 'Mountain Lion'
18+
)
19+
),
20+
array(
21+
'browserName' => 'chrome',
22+
'host' => 'hub.browserstack.com',
23+
'port' => 80,
24+
'desiredCapabilities' => array(
25+
'version' => '30',
26+
'browserstack.user' => BROWSERSTACK_USER,
27+
'browserstack.key' => BROWSERSTACK_KEY,
28+
'os' => 'Windows',
29+
'os_version' => '8.1'
30+
)
31+
)
32+
);
33+
protected function setUp()
34+
{
35+
parent::setUp();
36+
$this->setBrowserUrl('http://www.example.com/');
37+
}
38+
39+
public function testTitle()
40+
{
41+
$this->url('http://www.example.com/');
42+
$this->assertEquals('Example Domain', $this->title());
43+
}
44+
public function testGoogle()
45+
{
46+
$this->url('http://www.google.com/');
47+
$element = $this->byName('q');
48+
$element->click();
49+
$this->keys('Browserstack');
50+
$button = $this->byName('btnG');
51+
$button->click();
52+
$this->assertEquals('Browserstack - Google zoeken', $this->title());
53+
}
54+
}
55+
?>

0 commit comments

Comments
 (0)