Skip to content

Commit d2207e1

Browse files
Merge pull request #6 from Ankit098/fix/main
fix: test scripts, README; add: env variables, config
2 parents f01ac23 + b2d022f commit d2207e1

File tree

6 files changed

+102
-83
lines changed

6 files changed

+102
-83
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
/vendor
2-
/composer.phar
1+
vendor
2+
composer.phar
3+
local.log

README.md

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,49 @@
11
# php-selenium-browserstack
2+
Code samples for php selenium tests on browserstack.
23

34
## Prerequisites
4-
1. `php` and `composer` should be installed in your system
5-
2. Go to the directory where you have your test scripts and run the command below:
5+
1. `php` and `composer` should be installed in your system.
6+
* If you don't already use Composer, you can download the composer.phar binary:
67
```
7-
composer require php-webdriver/webdriver
8+
curl -sS https://getcomposer.org/installer | php
89
```
910

1011
## Steps to run test sessions
11-
1. Install dependencies
12+
1. Clone the repo
13+
```
14+
git clone https://github.com/browserstack/php-selenium-browserstack.git
15+
```
16+
2. Install dependencies
1217
```
13-
# If you don't already use Composer, you can download the composer.phar binary:
14-
curl -sS https://getcomposer.org/installer | php
15-
16-
# Include the binding:
17-
php composer.phar require browserstack/local:dev-master
18-
19-
# Install all the dependencies:
2018
php composer.phar install
21-
22-
# Test the installation by running a simple test file, check out example.php in the main repository.
2319
```
24-
2. Configure test capabilities
25-
(To run single test, navigate to ./scripts/single.php)
26-
27-
```php
28-
$caps = array(
29-
'bstack:options' => array(
30-
"os" => "OS X",
31-
"osVersion" => "Sierra",
32-
"buildName" => "Final-Snippet-Test",
33-
"sessionName" => "Selenium-4 PHP snippet test",
34-
"local" => "false",
35-
"seleniumVersion" => "4.0.0",
36-
),
37-
"browserName" => "Chrome",
38-
"browserVersion" => "latest",
39-
);
20+
3. Set your credentials in the `config.php` file. Update `YOUR_USERNAME` and `YOUR_ACCESS_KEY` with your username and access key.
21+
You can also set them as environment variables `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`, as follows:
22+
* For Unix-like or Mac machines:
23+
```
24+
export BROWSERSTACK_USERNAME=<browserstack-username> &&
25+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
26+
```
4027

41-
// Set you credentails
42-
$BROWSERSTACK_USERNAME = "BROWSERSTACK_USERNAME";
43-
$BROWSERSTACK_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
44-
```
28+
* For Windows:
29+
```
30+
set BROWSERSTACK_USERNAME=<browserstack-username>
31+
set BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
32+
```
4533

4634
## To run tests
4735
### Single test
4836
Run single test session by running.
4937
```
50-
php single.php
38+
php scripts/single.php
5139
```
5240
### Parallel test
5341
Run parallel test session by running.
5442
```
55-
php parallel.php
43+
php scripts/parallel.php
5644
```
5745
### Local test
5846
Run local test session by running.
59-
```php
60-
# Update "BROWSERSTACK_ACCESS_KEY" in bs_local.
61-
$bs_local_args = array("key" => "ACCESS_KEY");
62-
```
6347
```
64-
php local.php
48+
php scripts/local.php
6549
```

config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
define('BROWSERSTACK_USERNAME', 'YOUR_USERNAME');
3+
define('BROWSERSTACK_ACCESS_KEY', 'YOUR_ACCESS_KEY');
4+
?>

scripts/local.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
require_once("vendor/autoload.php");
3+
require_once('config.php');
34
use Facebook\WebDriver\Remote\RemoteWebDriver;
45
use Facebook\WebDriver\WebDriverBy;
56
use Facebook\WebDriver\WebDriverExpectedCondition;
@@ -8,30 +9,38 @@
89
$caps = array(
910
'bstack:options' => array(
1011
"os" => "OS X",
11-
"osVersion" => "Sierra",
12-
"buildName" => "BStack Build Number 1",
13-
"sessionName" => "BStack-[Php] Sample Test",
12+
"osVersion" => "Big Sur",
13+
"buildName" => "browserstack-build-1",
14+
"sessionName" => "BStack [php] Local test",
1415
"local" => "true",
1516
"seleniumVersion" => "4.0.0",
1617
),
1718
"browserName" => "Chrome",
1819
"browserVersion" => "latest",
1920
);
20-
$BROWSERSTACK_USERNAME = "BROWSERSTACK_USERNAME";
21-
$BROWSERSTACK_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
21+
22+
# read credentials from environment variables
23+
$USERNAME = getenv('BROWSERSTACK_USERNAME');
24+
$ACCESS_KEY = getenv('BROWSERSTACK_ACCESS_KEY');
25+
26+
# if not provided in env vars, read credentials from config file
27+
if (!isset($USERNAME)) {
28+
$USERNAME = constant('BROWSERSTACK_USERNAME');
29+
}
30+
if (!isset($ACCESS_KEY)) {
31+
$ACCESS_KEY = constant('BROWSERSTACK_ACCESS_KEY');
32+
}
2233

2334
# Creates an instance of Local
2435
$bs_local = new Local();
25-
26-
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
27-
$bs_local_args = array("key" => $BROWSERSTACK_ACCESS_KEY);
36+
$bs_local_args = array("key" => $ACCESS_KEY);
2837
# Starts the Local instance with the required arguments
2938
$bs_local->start($bs_local_args);
3039

3140
# Check if BrowserStack local instance is running
3241
echo $bs_local->isRunning();
3342

34-
$web_driver = RemoteWebDriver::create("https://$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);
43+
$web_driver = RemoteWebDriver::create("https://$USERNAME:$ACCESS_KEY@hub.browserstack.com/wd/hub", $caps);
3544
try{
3645
$web_driver->get("http://bs-local.com:45691/check");
3746
$body_text = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector("body")))->getText();
@@ -46,6 +55,6 @@
4655
echo 'Message: ' .$e->getMessage();
4756
}
4857
$web_driver->quit();
49-
# Stop the Local instance
58+
# Stop the Local instance
5059
$bs_local->stop();
5160
?>

scripts/parallel.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
<?php
2-
require_once('vendor/autoload.php');
3-
use Facebook\WebDriver\Remote\RemoteWebDriver;
4-
use Facebook\WebDriver\WebDriverBy;
5-
use Facebook\WebDriver\WebDriverExpectedCondition;
2+
require_once('vendor/autoload.php');
3+
require_once('config.php');
4+
use Facebook\WebDriver\Remote\RemoteWebDriver;
5+
use Facebook\WebDriver\WebDriverBy;
6+
use Facebook\WebDriver\WebDriverExpectedCondition;
7+
8+
# read credentials from environment variables
9+
$USERNAME = getenv('BROWSERSTACK_USERNAME');
10+
$ACCESS_KEY = getenv('BROWSERSTACK_ACCESS_KEY');
11+
12+
# if not provided in env vars, read credentials from config file
13+
if (!isset($USERNAME)) {
14+
$USERNAME = constant('BROWSERSTACK_USERNAME');
15+
}
16+
if (!isset($ACCESS_KEY)) {
17+
$ACCESS_KEY = constant('BROWSERSTACK_ACCESS_KEY');
18+
}
19+
620
function executeTestCase($caps) {
21+
global $USERNAME, $ACCESS_KEY;
22+
723
$web_driver = RemoteWebDriver::create(
8-
"https://$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
24+
"https://$USERNAME:$ACCESS_KEY@hub.browserstack.com/wd/hub",
925
$caps
1026
);
1127
try{
12-
$web_driver->get("https://bstackdemo.com/");
13-
$web_driver->wait(10000)->until(WebDriverExpectedCondition::titleIs("StackDemo"));
28+
$web_driver->get('https://bstackdemo.com/');
29+
$web_driver->wait(10000)->until(WebDriverExpectedCondition::titleIs('StackDemo'));
1430
# getting text of the product
1531
$product_on_page = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::XPath("//*[@id='1']/p")))->getText();
1632
# clicking the 'Add to cart' button
@@ -35,22 +51,20 @@ function executeTestCase($caps) {
3551
array(
3652
'bstack:options' => array(
3753
"os" => "OS X",
38-
"osVersion" => "Sierra",
39-
"buildName" => "BStack Build Number 1",
40-
"sessionName" => "Thread 1",
41-
"local" => "false",
54+
"osVersion" => "Big Sur",
55+
"buildName" => "browserstack-build-1",
56+
"sessionName" => "BStack [php] Parallel 1",
4257
"seleniumVersion" => "4.0.0",
4358
),
4459
"browserName" => "Chrome",
4560
"browserVersion" => "latest",
4661
),
4762
array(
4863
'bstack:options' => array(
49-
"os" => "OS X",
50-
"osVersion" => "Sierra",
51-
"buildName" => "BStack Build Number 1",
52-
"sessionName" => "Thread 2",
53-
"local" => "false",
64+
"os" => "Windows",
65+
"osVersion" => "10",
66+
"buildName" => "browserstack-build-1",
67+
"sessionName" => "BStack [php] Parallel 2",
5468
"seleniumVersion" => "4.0.0",
5569
),
5670
"browserName" => "Firefox",
@@ -60,10 +74,8 @@ function executeTestCase($caps) {
6074
'bstack:options' => array(
6175
"osVersion" => "10.0",
6276
"deviceName" => "Samsung Galaxy S20",
63-
"realMobile" => "true",
64-
"buildName" => "BStack Build Number 1",
65-
"sessionName" => "Thread 3",
66-
"local" => "false",
77+
"buildName" => "browserstack-build-1",
78+
"sessionName" => "BStack [php] Parallel 3",
6779
"seleniumVersion" => "4.0.0",
6880
),
6981
"browserName" => "Chrome",
@@ -73,4 +85,4 @@ function executeTestCase($caps) {
7385
foreach ( $caps as $cap ) {
7486
executeTestCase($cap);
7587
}
76-
?>
88+
?>

scripts/single.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
<?php
22
require_once('vendor/autoload.php');
3+
require_once('config.php');
34
use Facebook\WebDriver\Remote\RemoteWebDriver;
45
use Facebook\WebDriver\WebDriverBy;
56
use Facebook\WebDriver\WebDriverExpectedCondition;
67

78
$caps = array(
89
'bstack:options' => array(
910
"os" => "OS X",
10-
"osVersion" => "Sierra",
11-
"buildName" => "BStack Build Number 1",
12-
"sessionName" => "BStack-[Php] Sample Test",
13-
"local" => "false",
11+
"osVersion" => "Big Sur",
12+
"buildName" => "browserstack-build-1",
13+
"sessionName" => "BStack [php] Sample test",
1414
"seleniumVersion" => "4.0.0",
1515
),
1616
"browserName" => "Chrome",
1717
"browserVersion" => "latest",
1818
);
1919

20-
$BROWSERSTACK_USERNAME = "BROWSERSTACK_USERNAME";
21-
$BROWSERSTACK_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
20+
# read credentials from environment variables
21+
$USERNAME = getenv('BROWSERSTACK_USERNAME');
22+
$ACCESS_KEY = getenv('BROWSERSTACK_ACCESS_KEY');
2223

23-
$web_driver = RemoteWebDriver::create("https://$BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);
24+
# if not provided in env vars, read credentials from config file
25+
if (!isset($USERNAME)) {
26+
$USERNAME = constant('BROWSERSTACK_USERNAME');
27+
}
28+
if (!isset($ACCESS_KEY)) {
29+
$ACCESS_KEY = constant('BROWSERSTACK_ACCESS_KEY');
30+
}
31+
32+
$web_driver = RemoteWebDriver::create("https://$USERNAME:$ACCESS_KEY@hub.browserstack.com/wd/hub", $caps);
2433
try{
25-
$web_driver->get("https://bstackdemo.com/");
26-
$web_driver->wait(10000)->until(WebDriverExpectedCondition::titleIs("StackDemo"));
34+
$web_driver->get('https://bstackdemo.com/');
35+
$web_driver->wait(10000)->until(WebDriverExpectedCondition::titleIs('StackDemo'));
2736
# getting text of the product
2837
$product_on_page = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::XPath("//*[@id='1']/p")))->getText();
2938
# clicking the 'Add to cart' button
@@ -33,7 +42,7 @@
3342
# getting text of the product
3443
$product_in_cart = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::XPath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")))->getText();
3544
# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
36-
if ($product_on_page == $product_in_cart){
45+
if ($product_on_page == $product_in_cart) {
3746
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Product has been successfully added to the cart!"}}' );
3847
} else {
3948
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Failed to add product to the cart or some elements might have failed to load."}}');

0 commit comments

Comments
 (0)