Skip to content

Commit b57e3fb

Browse files
Merge pull request #34 from browserstack/merge_sdk_to_main
Merge sdk to main
2 parents 4f5a751 + 2a56a02 commit b57e3fb

8 files changed

Lines changed: 147 additions & 233 deletions

File tree

.env

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

README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +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
9+
## Setup
10+
* Clone the repo
1111
```
12-
pip3 install -r requirements.txt
12+
git clone -b sdk https://github.com/browserstack/python-selenium-browserstack.git
13+
```
14+
* Install packages through requirements.txt
1315
```
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"
16+
pip3 install -r requirements.txt
1917
```
2018

21-
- 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+
```
2233
23-
a. For parallel
24-
```
25-
python3 ./scripts/parallel.py
26-
```
27-
b. For local
28-
```
29-
python3 ./scripts/local.py
30-
```
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 ./tests/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 ./tests/local-test.py
44+
```

browserstack.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and accessKey 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+
# BrowserStack Reporting
11+
# ======================
12+
# The following capabilities are used to set up reporting on BrowserStack:
13+
# Set 'projectName' to the name of your project. Example, Marketing Website
14+
projectName: BrowserStack Samples
15+
# Set `buildName` as the name of the job / testsuite being run
16+
buildName: browserstack build
17+
# `buildIdentifier` is a unique id to differentiate every execution that gets appended to
18+
# buildName. Choose your buildIdentifier format from the available expressions:
19+
# ${BUILD_NUMBER} (Default): Generates an incremental counter with every execution
20+
# ${DATE_TIME}: Generates a Timestamp with every execution. Eg. 05-Nov-19:30
21+
# Read more about buildIdentifiers here -> https://www.browserstack.com/docs/automate/selenium/organize-tests
22+
buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${expression}
23+
24+
# =======================================
25+
# Platforms (Browsers / Devices to test)
26+
# =======================================
27+
# Platforms object contains all the browser / device combinations you want to test on.
28+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
29+
platforms:
30+
- os: OS X
31+
osVersion: Big Sur
32+
browserName: Chrome
33+
browserVersion: latest
34+
- os: Windows
35+
osVersion: 10
36+
browserName: Edge
37+
browserVersion: latest
38+
- deviceName: Samsung Galaxy S22 Ultra
39+
browserName: chrome # Try 'samsung' for Samsung browser
40+
osVersion: 12.0
41+
42+
# ==========================================
43+
# BrowserStack Local
44+
# (For localhost, staging/private websites)
45+
# ==========================================
46+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
47+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
48+
browserstackLocal: true # <boolean> (Default false)
49+
# browserStackLocalOptions:
50+
# Options to be passed to BrowserStack local in-case of advanced configurations
51+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
52+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
53+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
54+
55+
source: python-browserstack:sample-sdk:v1.0
56+
57+
# ===================
58+
# Debugging features
59+
# ===================
60+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
61+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
62+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
63+
# Available options are `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 & 64 deletions
This file was deleted.

scripts/parallel.py

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

tests/local-test.py

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

0 commit comments

Comments
 (0)