Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .env

This file was deleted.

70 changes: 33 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
# python-selenium-browserstack
Run python tests on browserstack using the SDK.

## Prerequisite
```
python3 and pip3 should be installed
python3 should be installed
```

## Steps to run test session

- Install packages through requirements.txt
```
pip3 install -r requirements.txt
## Setup
* Clone the repo
```
- Update your credentials in .env file
```dotenv
BROWSERSTACK_USERNAME="BROWSERSTACK_USERNAME"
BROWSERSTACK_ACCESS_KEY="BROWSERSTACK_ACCESS_KEY"
URL="https://hub.browserstack.com/wd/hub"
git clone -b sdk https://github.com/browserstack/python-selenium-browserstack.git
```
* Install packages through requirements.txt
```
- Change the capabilities if you wish:
(For single test session, Navigate to ./scripts/single.py)
```python
desired_cap = {
...
'browserName': 'iPhone',
'device': 'iPhone 11',
'realMobile': 'true',
'os_version': '14.0',
'name': 'BStack-[Python] Sample Test', # test name
'build': 'BStack Build Number 1' # CI/CD job or build name
...
}
pip3 install -r requirements.txt
```

- Run tests
## Set BrowserStack Credentials
* Add your BrowserStack username and access key in the `browserstack.yml` config fle.
* You can also export them as environment variables, `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`:

#### For Linux/MacOS
```
export BROWSERSTACK_USERNAME=<browserstack-username>
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```
#### For Windows
```
setx BROWSERSTACK_USERNAME=<browserstack-username>
setx BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```

a. For single
```
python3 ./scripts/single.py
```
b. For local
```
python3 ./scripts/local.py
```
c. For parallel
```
python3 ./scripts/parallel.py
```
## Running tests
* Run sample test:
- To run the sample test across platforms defined in the `browserstack.yml` file, run:
```
browserstack-sdk ./scripts/test.py
```
* Run tests on locally hosted website:
- To run the local test across platforms defined in the `browserstack.yml` file, run:
```
browserstack-sdk ./scripts/local-test.py
```
59 changes: 59 additions & 0 deletions browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and acccessKey here or set BROWSERSTACK_USERNAME and
# BROWSERSTACK_ACCESS_KEY as env variables
userName: YOUR_USERNAME
accessKey: YOUR_ACCESS_KEY

# ======================
# Organizing your tests
# ======================
# Use `projectName`, `buildName`, `name` capabilities to organise your tests
# `name` is the name of your test sessions and is automatically picked from your
# test name and doesn't need to be set manually when using BrowserStack SDK
# `buildName` is used to name your CI/CD job or the execution of your test suite.
# Ensure you add a dynamic identifier, like an incremental build number from your
# CI/CD or timestamp at the end of every build; otherwise tests from different
# executions will be grouped together on BrowserStack
buildName: browserstack-build-1
# Use `projectName` to set the name of your project. Example, Marketing Website
projectName: BrowserStack Samples

# =======================================
# Platforms (Browsers / Devices to test)
# =======================================
# Platforms object contains all the browser / device combinations you want to test on.
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
platforms:
- os: OS X
osVersion: Big Sur
browser: Chrome
browserVersion: latest
- os: Windows
osVersion: 10
browser: Edge
browserVersion: latest
- device: Samsung Galaxy S22 Ultra
browserName: chrome # Try 'samsung' for Samsung browser
osVersion: 12.0

# ==========================================
# BrowserStack Local
# (For localhost, staging/private websites)
# ==========================================
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
browserstackLocal: true # <boolean> (Default false)
# browserStackLocalOptions:
# Options to be passed to BrowserStack local in-case of advanced configurations
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
# Entire list of arguments availabe here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections

# ===================
# Debugging features
# ===================
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Available levels: `disable`, `errors`, `warnings`, `info`, `verbose`, Default: errors)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python-dotenv
browserstack-local >= 1.2.3
selenium == 4.1.0
selenium
browserstack-sdk
63 changes: 0 additions & 63 deletions scripts/local.py

This file was deleted.

105 changes: 0 additions & 105 deletions scripts/parallel.py

This file was deleted.

36 changes: 36 additions & 0 deletions tests/local-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()

# The webdriver management will be handled by the browserstack-sdk
# so this will be overridden and tests will run browserstack -
# without any changes to the test files!
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options)

try:
driver.get('http://bs-local.com:45691/check')
body_text = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, 'body'))).text
# check if local connected successfully
if body_text == 'Up and running':
# mark test as passed if Local website is accessible
driver.execute_script(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local Test ran successfully"}}')
else:
# mark test as failed if Local website is not accessible
driver.execute_script(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local test setup failed"}}')
except Exception as err:
message = 'Exception: ' + str(err.__class__) + str(err.msg)
driver.execute_script(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": ' + json.dumps(message) + '}}')

# Stop the driver
driver.quit()
Loading