Skip to content

Commit ca176c0

Browse files
committed
selenium-4 changes
1 parent bb70ba3 commit ca176c0

File tree

7 files changed

+426
-0
lines changed

7 files changed

+426
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1+
# php-selenium-browserstack
12

3+
## 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:
6+
```
7+
composer require php-webdriver/webdriver
8+
```
9+
10+
## Steps to run test sessions
11+
1. Install dependencies
12+
```
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:
20+
php composer.phar install
21+
22+
# Test the installation by running a simple test file, check out example.php in the main repository.
23+
```
24+
2. Configure test capabilities
25+
(To run single test, navigate to ./scripts/single/php)
26+
27+
```php
28+
$caps = array(
29+
"browserName" => "iPhone",
30+
"device" => "iPhone 11",
31+
"realMobile" => "true",
32+
"os_version" => "14.0",
33+
"name" => "BStack-[Php] Sample Test", // test name
34+
"build" => "BStack Build Number 1" // CI/CD job or build name
35+
);
36+
// IMP: Use your browserstack username and accesskey
37+
$web_driver = RemoteWebDriver::create("https://USERNAME:[email protected]/wd/hub",$caps);
38+
```
39+
40+
3. Run tests
41+
Single test
42+
```
43+
php single.php
44+
```
45+
46+
Local test
47+
Along with step-2 also use your Accesskey to bs_loca_args
48+
```php
49+
# In file ./scripts/local.php
50+
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
51+
$bs_local_args = array("key" => "ACCESS_KEY");
52+
```
53+
54+
```
55+
php single.php
56+
```

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"browserstack/browserstack-local": "^1.1",
4+
"php-webdriver/webdriver": "^1.12"
5+
}
6+
}

composer.lock

Lines changed: 276 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.phar

2.25 MB
Binary file not shown.

scripts/local.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
require_once("vendor/autoload.php");
3+
use Facebook\WebDriver\Remote\RemoteWebDriver;
4+
use Facebook\WebDriver\WebDriverBy;
5+
use Facebook\WebDriver\WebDriverExpectedCondition;
6+
use BrowserStack\Local;
7+
8+
# Creates an instance of Local
9+
$bs_local = new Local();
10+
11+
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
12+
$bs_local_args = array("key" => "ACCESS_KEY");
13+
# Starts the Local instance with the required arguments
14+
$bs_local->start($bs_local_args);
15+
16+
# Check if BrowserStack local instance is running
17+
echo $bs_local->isRunning();
18+
$caps = array(
19+
'bstack:options' => array(
20+
"os" => "OS X",
21+
"osVersion" => "Sierra",
22+
"buildName" => "Final-Snippet-Test",
23+
"sessionName" => "Selenium-4 PHP snippet test",
24+
"local" => "true",
25+
"seleniumVersion" => "4.0.0",
26+
),
27+
"browserName" => "Chrome",
28+
"browserVersion" => "latest",
29+
);
30+
$web_driver = RemoteWebDriver::create("https://USERNAME:[email protected]/wd/hub",$caps);
31+
try{
32+
$web_driver->get("http://bs-local.com:45691/check");
33+
$body_text = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector("body")))->getText();
34+
# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
35+
if ($body_text == "Up and running"){
36+
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local test ran successfully"}}' );
37+
} else {
38+
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Failed to load local test"}}');
39+
}
40+
}
41+
catch(Exception $e){
42+
echo 'Message: ' .$e->getMessage();
43+
}
44+
$web_driver->quit();
45+
# Stop the Local instance
46+
$bs_local->stop();
47+
?>

0 commit comments

Comments
 (0)