diff --git a/.gitignore b/.gitignore index 9de86f4..6a823d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,9 @@ +.vscode __pycache__ -.pytest_cache/ -local.log +.pytest_cache env +venv +local.log +*.pyc +.DS_Store +browserstack.err diff --git a/README.md b/README.md index f98d1ee..7a450f2 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,86 @@ # pytest-playwright-browserstack -Playwright with PyTest Test runner Integration with BrowserStack. +Sample project demonstrating how to run [Playwright](https://playwright.dev/python/) tests with [pytest](https://docs.pytest.org/) on [BrowserStack](https://www.browserstack.com) using the BrowserStack SDK. -![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780) -## Prerequisite -* Python3 +## Prerequisites + +- Python 3.8+ +- A BrowserStack account ([sign up for free](https://www.browserstack.com/users/sign_up)) ## Setup -* Clone the repo -* 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 adding your userName and accesKey in the `browserstack.yml` file. -* You can also set the credentials in BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables. +1. Clone this repository: + + ```bash + git clone https://github.com/browserstack/pytest-playwright-browserstack.git + cd pytest-playwright-browserstack + ``` + +2. Create a virtual environment and install dependencies: + + ```bash + python3 -m venv venv + source venv/bin/activate # On Windows: venv\Scripts\activate + pip install -r requirements.txt + playwright install + ``` + +3. Set your BrowserStack credentials in `browserstack.yml`: + + ```yaml + userName: YOUR_USERNAME + accessKey: YOUR_ACCESS_KEY + ``` + + Or export them as environment variables: + + ```bash + export BROWSERSTACK_USERNAME=YOUR_USERNAME + export BROWSERSTACK_ACCESS_KEY=YOUR_ACCESS_KEY + ``` + +## Running Tests + +Run the sample test on BrowserStack: + +```bash +browserstack-sdk pytest -s tests/ +``` + +This will run the Playwright tests across the platforms defined in `browserstack.yml`. + +## Project Structure + +``` +pytest-playwright-browserstack/ +├── tests/ +│ └── test_sample.py # Playwright test using page fixture +├── browserstack.yml # BrowserStack SDK configuration +├── requirements.txt # Python dependencies +└── README.md +``` + +## Configuration + +Edit `browserstack.yml` to customize: + +- **Platforms**: Add or change browser combinations under `platforms` +- **Parallelism**: Adjust `parallelsPerPlatform` to run more tests concurrently +- **Browsers**: Use `playwright-chromium`, `playwright-webkit`, or `playwright-firefox` +- **Debugging**: Set `debug`, `networkLogs`, or `consoleLogs` for troubleshooting + +See the full list of configuration options in the [BrowserStack SDK documentation](https://www.browserstack.com/docs/automate/playwright/getting-started/python). + +## How It Works + +The BrowserStack Python SDK wraps test execution. When you run tests via `browserstack-sdk`, it intercepts Playwright's browser launch and routes sessions to BrowserStack's cloud via CDP (Chrome DevTools Protocol). Your tests use standard Playwright and pytest APIs with no code changes needed. -## Run sample tests -* To run tests, run `browserstack-sdk pytest -s tests/sample-test.py` -* To run local tests, run `browserstack-sdk pytest -s tests/sample-local-test.py`. +The `page` fixture is provided automatically by the `pytest-playwright` plugin — no custom `conftest.py` is required. -## Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) +## Resources -## Notes -* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate) +- [BrowserStack Playwright Getting Started Guide](https://www.browserstack.com/docs/automate/playwright/getting-started/python) +- [Playwright for Python Documentation](https://playwright.dev/python/) +- [pytest-playwright Plugin](https://github.com/microsoft/playwright-pytest) +- [BrowserStack SDK Configuration](https://www.browserstack.com/docs/automate/selenium/sdk-config) +- [Supported Browsers and OS](https://www.browserstack.com/list-of-browsers-and-platforms/automate) diff --git a/browserstack.yml b/browserstack.yml index a7afbd9..e3f1adf 100644 --- a/browserstack.yml +++ b/browserstack.yml @@ -1,77 +1,31 @@ -# ============================= -# 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 +userName: BROWSERSTACK_USERNAME +accessKey: BROWSERSTACK_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 -useW3C: false -# ======================================= -# 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) + +projectName: BrowserStack Samples +buildName: pytest-playwright-browserstack build +buildIdentifier: '#${BUILD_NUMBER}' +source: playwright-pytest:sample-main:v1.0 + platforms: - - os: Windows - osVersion: 11 - browserName: chrome - browserVersion: latest - os: OS X osVersion: Ventura + browserName: playwright-chromium + browserVersion: latest + - os: Windows + osVersion: 11 browserName: playwright-webkit browserVersion: latest - os: Windows osVersion: 11 browserName: playwright-firefox browserVersion: latest -# ======================= -# 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 # (Default false) -# browserStackLocalOptions: -# Options to be passed to BrowserStack local in-case of advanced configurations - # localIdentifier: # (Default: null) Needed if you need to run multiple instances of local. - # forceLocal: true # (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-playwright-browserstack:sample-sdk:v1.0 +parallelsPerPlatform: 1 +browserstackLocal: false +testObservability: true -# =================== -# Debugging features -# =================== -debug: false # # Set to true if you need screenshots for every selenium command ran -networkLogs: false # Set to true to enable HAR logs capturing -consoleLogs: errors # Remote browser's console debug levels to be printed (Default: errors) -# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors) +debug: false +networkLogs: false +consoleLogs: errors diff --git a/requirements.txt b/requirements.txt index ccbbc7c..cc2d66e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,4 @@ -browserstack-local -jsonmerge -multiprocess +browserstack-sdk playwright +pytest pytest-playwright -psutil -pytest==7.4.4 -pytest-variables -pytest-xdist -pytest-base-url -python-dotenv -browserstack-sdk diff --git a/tests/test_sample.py b/tests/test_sample.py new file mode 100644 index 0000000..f990b07 --- /dev/null +++ b/tests/test_sample.py @@ -0,0 +1,13 @@ +from playwright.sync_api import expect + + +def test_add_product_to_cart(page): + page.goto("https://bstackdemo.com/") + page.locator("[id=\"\\31 \"]").wait_for() + + product_name = page.locator("[id=\"\\31 \"] .shelf-item__title").inner_text() + page.locator("[id=\"\\31 \"] .shelf-item__buy-btn").click() + + cart = page.locator(".float-cart__content") + cart.wait_for() + expect(cart.locator(".shelf-item__details .title")).to_have_text(product_name)