Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
abb7f64
add: sdk flow for pytest sample repo
Ankit098 Dec 22, 2022
9b5b372
Merge pull request #8 from Ankit098/sdk
karanshah-browserstack Dec 26, 2022
bfbaa0a
chore: restructure files
Ankit098 Dec 28, 2022
c30436f
update: readme run commands
Ankit098 Dec 28, 2022
48df04d
chore: add source
Ankit098 Jan 9, 2023
2349b86
Added github action
agarneha1331 Apr 10, 2023
f3f1a8f
fix codeowners
agarneha1331 Apr 11, 2023
70eee21
fix name in workflow file
agarneha1331 Apr 11, 2023
b49db5b
Test
agarneha1331 Apr 13, 2023
0029786
Added bstack to file names
agarneha1331 Jun 19, 2023
9fef37d
Added bstack to file names
agarneha1331 Jun 19, 2023
0d72120
Small fix
agarneha1331 Jun 20, 2023
152c8b1
fixed workflow file
agarneha1331 Jul 5, 2023
0b12024
Merge pull request #18 from agarneha1331/fix-session-name-sdk
karanshah-browserstack Jul 7, 2023
ce9dc69
delete sdk workflow
agarneha1331 Jul 10, 2023
ccfa08e
1.15.1
karanshah-browserstack Sep 4, 2023
437d925
Pin to 1.15.0
karanshah-browserstack Sep 4, 2023
f375871
Fix selenium
karanshah-browserstack Sep 4, 2023
16d493b
Fix pytest version
karanshah-browserstack Sep 4, 2023
14fccd1
Add pytest-html
karanshah-browserstack Sep 4, 2023
8221bb6
Fix pytest html
karanshah-browserstack Sep 4, 2023
2ddbdc1
Merge pull request #22 from browserstack/pin_version
karanshah-browserstack Sep 4, 2023
f78c152
Update requirements.txt
karanshah-browserstack Mar 28, 2024
caebdb8
Merge pull request #27 from browserstack/fix_pytest_version
karanshah-browserstack Mar 28, 2024
a4a2dfc
Merged sdk branch to merge_sdk_to_main
Jun 19, 2024
69cef2d
Fixed linting issue
Jul 1, 2024
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 .browserstack

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.vscode
__pycache__
.pytest_cache
local.log
env
local.log
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
# PyTest with Browserstack

PyTest Integration with BrowserStack.
PyTest Integration with BrowserStack using SDK.

![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
## Prerequisite
* Python3

## Setup

* Clone the repo
* Clone the repo with `git clone -b sdk https://github.com/browserstack/pytest-browserstack.git`
* It is recommended to use a virtual environment to install dependencies. To create a virtual environment:
```
python3 -m venv env
source env/bin/activate # on Mac
env\Scripts\activate # on Windows
```
* Install dependencies `pip install -r requirements.txt`
* To run your automated tests using BrowserStack, you must provide a valid username and access key. This can be done either by using a .browserstack configuration file in the working directory or your home directory, by setting the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables, or by adding user & key to config file.


## Run tests on locally hosted websites
* To run a local test, (if you have not set the BROWSERSTACK_ACCESS_KEY environment variable) first go to config/local.json then edit key on line 3
* Run `paver run local`

## Run sample tests
* To run parallel tests, run `paver run sample`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
* To run your automated tests using BrowserStack, you must provide a valid username and access key. This can be done either by providing your username and access key in the `browserstack.yml` configuration file, or by setting the `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY` environment variables.

## Run sample test in parallel:
* To run the sample test across platforms defined in the configuration file run:
```
browserstack-sdk pytest -s tests/bstack-sample-test.py
```

## Run a sample local test:
* To run the sample local test across platforms defined in the configuration file, you just need to set `browserstackLocal` key as true in the `browserstack.yml` file and run:
```
browserstack-sdk pytest -s tests/bstack-local-test.py
```

## Notes
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
Expand Down
77 changes: 77 additions & 0 deletions browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
# BROWSERSTACK_ACCESS_KEY as env variables
userName: YOUR_USERNAME
accessKey: YOUR_ACCESS_KEY

# ======================
# BrowserStack Reporting
# ======================
# The following capabilities are used to set up reporting on BrowserStack:
# Set 'projectName' to the name of your project. Example, Marketing Website
projectName: BrowserStack Samples
# Set `buildName` as the name of the job / testsuite being run
buildName: browserstack build
# `buildIdentifier` is a unique id to differentiate every execution that gets appended to
# buildName. Choose your buildIdentifier format from the available expressions:
# ${BUILD_NUMBER} (Default): Generates an incremental counter with every execution
# ${DATE_TIME}: Generates a Timestamp with every execution. Eg. 05-Nov-19:30
# Read more about buildIdentifiers here -> https://www.browserstack.com/docs/automate/selenium/organize-tests
buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${expression}
# Set `framework` of your test suite. Example, `testng`, `cucumber`, `cucumber-testng`
# This property is needed to send test context to BrowserStack (test name, status)
framework: pytest

# =======================================
# 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
browserName: Chrome
browserVersion: latest
- os: Windows
osVersion: 10
browserName: Edge
browserVersion: latest
- deviceName: Samsung Galaxy S22 Ultra
browserName: chrome # Try 'samsung' for Samsung browser
osVersion: 12.0

# =======================
# Parallels per Platform
# =======================
# The number of parallel threads to be used for each platform set.
# BrowserStack's SDK runner will select the best strategy based on the configured value
#
# Example 1 - If you have configured 3 platforms and set `parallelsPerPlatform` as 2, a total of 6 (2 * 3) parallel threads will be used on BrowserStack
#
# Example 2 - If you have configured 1 platform and set `parallelsPerPlatform` as 5, a total of 5 (1 * 5) parallel threads will be used on BrowserStack
parallelsPerPlatform: 1

# ==========================================
# 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 available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections

source: pytest-browserstack:sample-sdk:v1.0

# ===================
# 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 (Default: errors)
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)
24 changes: 0 additions & 24 deletions config/local.json

This file was deleted.

38 changes: 0 additions & 38 deletions config/sample.json

This file was deleted.

41 changes: 0 additions & 41 deletions conftest.py

This file was deleted.

37 changes: 0 additions & 37 deletions pavement.py

This file was deleted.

7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
browserstack-local
jsonmerge
multiprocess
paver
selenium==4.*
psutil
pytest
pytest==7.4.4
pytest-variables
pytest-selenium
pytest-xdist
browserstack-sdk
pytest-html==3.2.0
6 changes: 6 additions & 0 deletions tests/bstack-local-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest

def test_local(selenium):
selenium.get('http://bs-local.com:45454')
local_page_title = selenium.title
assert local_page_title == 'BrowserStack Local'
9 changes: 2 additions & 7 deletions tests/test_sample.py → tests/bstack-sample-test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from browserstack.local import Local
from selenium import webdriver
import pytest
from selenium.webdriver.common.by import By

Expand All @@ -18,8 +16,5 @@ def test_example(selenium):
# locating product in cart and getting name of the product in cart
productCartText = selenium.find_element(By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]').text

# checking whether product has been added to cart by comparing product name and marking test pass or fail
if productText == productCartText:
selenium.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Test Passed Successfully"}}')
else:
selenium.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Product added to the cart not same as selected"}}')
# checking whether product has been added to cart by comparing product name
assert productCartText == productText
10 changes: 0 additions & 10 deletions tests/test_local.py

This file was deleted.