Skip to content

Commit c89d4ed

Browse files
authored
Merge pull request #14 from Ankit098/sdk
use sdk to run tests
2 parents 0518ca6 + 20cde6d commit c89d4ed

File tree

8 files changed

+143
-239
lines changed

8 files changed

+143
-239
lines changed

.env

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
# python-selenium-browserstack
2+
Run python tests on browserstack using the SDK.
23

34
## Prerequisite
45
```
5-
python3 and pip3 should be installed
6+
python3 should be installed
67
```
78

8-
## Steps to run test session
9-
10-
- Install packages through requirements.txt
11-
```
12-
pip3 install -r requirements.txt
9+
## Setup
10+
* Clone the repo
1311
```
14-
- Update your credentials in .env file
15-
```dotenv
16-
BROWSERSTACK_USERNAME="BROWSERSTACK_USERNAME"
17-
BROWSERSTACK_ACCESS_KEY="BROWSERSTACK_ACCESS_KEY"
18-
URL="https://hub.browserstack.com/wd/hub"
12+
git clone -b sdk https://github.com/browserstack/python-selenium-browserstack.git
13+
```
14+
* Install packages through requirements.txt
1915
```
20-
- Change the capabilities if you wish:
21-
(For single test session, Navigate to ./scripts/single.py)
22-
```python
23-
desired_cap = {
24-
...
25-
'browserName': 'iPhone',
26-
'device': 'iPhone 11',
27-
'realMobile': 'true',
28-
'os_version': '14.0',
29-
'name': 'BStack-[Python] Sample Test', # test name
30-
'build': 'BStack Build Number 1' # CI/CD job or build name
31-
...
32-
}
16+
pip3 install -r requirements.txt
3317
```
3418

35-
- Run tests
19+
## Set BrowserStack Credentials
20+
* Add your BrowserStack username and access key in the `browserstack.yml` config fle.
21+
* You can also export them as environment variables, `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`:
22+
23+
#### For Linux/MacOS
24+
```
25+
export BROWSERSTACK_USERNAME=<browserstack-username>
26+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
27+
```
28+
#### For Windows
29+
```
30+
setx BROWSERSTACK_USERNAME=<browserstack-username>
31+
setx BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
32+
```
3633
37-
a. For single
38-
```
39-
python3 ./scripts/single.py
40-
```
41-
b. For local
42-
```
43-
python3 ./scripts/local.py
44-
```
45-
c. For parallel
46-
```
47-
python3 ./scripts/parallel.py
48-
```
34+
## Running tests
35+
* Run sample test:
36+
- To run the sample test across platforms defined in the `browserstack.yml` file, run:
37+
```
38+
browserstack-sdk ./scripts/test.py
39+
```
40+
* Run tests on locally hosted website:
41+
- To run the local test across platforms defined in the `browserstack.yml` file, run:
42+
```
43+
browserstack-sdk ./scripts/local-test.py
44+
```

browserstack.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and acccessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: YOUR_USERNAME
7+
accessKey: YOUR_ACCESS_KEY
8+
9+
# ======================
10+
# Organizing your tests
11+
# ======================
12+
# Use `projectName`, `buildName`, `name` capabilities to organise your tests
13+
# `name` is the name of your test sessions and is automatically picked from your
14+
# test name and doesn't need to be set manually when using BrowserStack SDK
15+
# `buildName` is used to name your CI/CD job or the execution of your test suite.
16+
# Ensure you add a dynamic identifier, like an incremental build number from your
17+
# CI/CD or timestamp at the end of every build; otherwise tests from different
18+
# executions will be grouped together on BrowserStack
19+
buildName: browserstack-build-1
20+
# Use `projectName` to set the name of your project. Example, Marketing Website
21+
projectName: BrowserStack Samples
22+
23+
# =======================================
24+
# Platforms (Browsers / Devices to test)
25+
# =======================================
26+
# Platforms object contains all the browser / device combinations you want to test on.
27+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
28+
platforms:
29+
- os: OS X
30+
osVersion: Big Sur
31+
browser: Chrome
32+
browserVersion: latest
33+
- os: Windows
34+
osVersion: 10
35+
browser: Edge
36+
browserVersion: latest
37+
- device: Samsung Galaxy S22 Ultra
38+
browserName: chrome # Try 'samsung' for Samsung browser
39+
osVersion: 12.0
40+
41+
# ==========================================
42+
# BrowserStack Local
43+
# (For localhost, staging/private websites)
44+
# ==========================================
45+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
46+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
47+
browserstackLocal: true # <boolean> (Default false)
48+
# browserStackLocalOptions:
49+
# Options to be passed to BrowserStack local in-case of advanced configurations
50+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
51+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
52+
# Entire list of arguments availabe here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
53+
54+
# ===================
55+
# Debugging features
56+
# ===================
57+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
58+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
59+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Available levels: `disable`, `errors`, `warnings`, `info`, `verbose`, Default: errors)

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
python-dotenv
21
browserstack-local >= 1.2.3
3-
selenium == 4.1.0
2+
selenium
3+
browserstack-sdk

scripts/local.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

scripts/parallel.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

tests/local-test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
from selenium import webdriver
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
from selenium.webdriver.support import expected_conditions as EC
5+
from selenium.webdriver.common.by import By
6+
from selenium.webdriver.chrome.options import Options as ChromeOptions
7+
8+
options = ChromeOptions()
9+
10+
# The webdriver management will be handled by the browserstack-sdk
11+
# so this will be overridden and tests will run browserstack -
12+
# without any changes to the test files!
13+
driver = webdriver.Remote(
14+
command_executor='http://localhost:4444/wd/hub',
15+
options=options)
16+
17+
try:
18+
driver.get('http://bs-local.com:45691/check')
19+
body_text = WebDriverWait(driver, 10).until(
20+
EC.visibility_of_element_located((By.CSS_SELECTOR, 'body'))).text
21+
# check if local connected successfully
22+
if body_text == 'Up and running':
23+
# mark test as passed if Local website is accessible
24+
driver.execute_script(
25+
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local Test ran successfully"}}')
26+
else:
27+
# mark test as failed if Local website is not accessible
28+
driver.execute_script(
29+
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local test setup failed"}}')
30+
except Exception as err:
31+
message = 'Exception: ' + str(err.__class__) + str(err.msg)
32+
driver.execute_script(
33+
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": ' + json.dumps(message) + '}}')
34+
35+
# Stop the driver
36+
driver.quit()

0 commit comments

Comments
 (0)