Skip to content
Open
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
71 changes: 71 additions & 0 deletions .github/workflows/sdk-sample-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Runs the BrowserStack SDK sample against a given commit and reports a status check.
# Trigger: Actions tab -> "pytest-bdd Appium App Automate SDK sample test" -> Run workflow -> paste the PR's full commit SHA.
# Requires repo secrets: BROWSERSTACK_USERNAME, BROWSERSTACK_ACCESS_KEY.
# NOTE (App Automate): the app under test is referenced via `app: bs://...` in browserstack.yml;
# ensure that uploaded app exists on the account whose secrets are used (re-upload + update if expired).
name: pytest-bdd Appium App Automate SDK sample test

on:
workflow_dispatch:
inputs:
commit_sha:
description: 'The full commit id to build'
required: true

jobs:
sdk-sample:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 3
matrix:
os: [ubuntu-latest]
python: ['3.10', '3.11']
name: pytest-bdd-appium Python ${{ matrix.python }} sample
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
defaults:
run:
working-directory: android
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.commit_sha }}
- name: Mark status check in_progress
uses: actions/github-script@v7
env:
job_name: pytest-bdd-appium Python ${{ matrix.python }} sample
commit_sha: ${{ github.event.inputs.commit_sha }}
with:
github-token: ${{ github.token }}
script: |
await github.rest.checks.create({
owner: context.repo.owner, repo: context.repo.repo,
name: process.env.job_name, head_sha: process.env.commit_sha,
status: 'in_progress'
}).catch(e => console.log('check create failed:', e.status));
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- name: Install
run: |
pip install -r requirements.txt
- name: Run sample test
run: |
browserstack-sdk pytest tests/test_sample.py
- name: Mark status check completed
if: always()
uses: actions/github-script@v7
env:
conclusion: ${{ job.status }}
job_name: pytest-bdd-appium Python ${{ matrix.python }} sample
commit_sha: ${{ github.event.inputs.commit_sha }}
with:
github-token: ${{ github.token }}
script: |
await github.rest.checks.create({
owner: context.repo.owner, repo: context.repo.repo,
name: process.env.job_name, head_sha: process.env.commit_sha,
status: 'completed', conclusion: process.env.conclusion
}).catch(e => console.log('check create failed:', e.status));

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
Comment on lines +17 to +71
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.vscode
__pycache__
.pytest_cache
.venv
env
local.log
log/
*.apk
*.ipa
86 changes: 84 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,84 @@
# pytest-bdd-appium-app-browserstack
We require the following new public repositories under the browserstack GitHub organization to host customer-facing sample projects for the BrowserStack SDK.
# pytest-bdd + Appium with BrowserStack App Automate

Run Appium (mobile app) tests written with **pytest-bdd** on real devices on
[BrowserStack App Automate](https://app-automate.browserstack.com/) using the
BrowserStack Python SDK. No capability boilerplate in your tests — the SDK reads
`browserstack.yml` and routes each session to the BrowserStack device cloud.

This repo has two self-contained platform directories:

```
android/ Android sample (WikipediaSample.apk) + local (LocalSample.apk)
ios/ iOS sample (BStackSampleApp.ipa) + local (LocalSample.ipa)
```

Each directory has its own `browserstack.yml`, `conftest.py` (Appium driver
fixture), `features/` (Gherkin), `tests/` (pytest-bdd step definitions), and
`requirements.txt`.

## Prerequisites

- A [BrowserStack](https://www.browserstack.com/) account (username + access key).
- Python 3.8+.
- An application to test. The Android directory is pre-wired to a pre-uploaded
`WikipediaSample.apk` (`bs://...`); the iOS directory uploads
`BStackSampleApp.ipa` from a local path.

## Setup

```bash
git clone <this-repo>
cd pytest-bdd-appium/android # or: cd pytest-bdd-appium/ios

python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
```

Configure credentials via env vars (recommended) or by editing `browserstack.yml`:

```bash
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
```

## Run Sample Test (Android)

From inside `android/`:

```bash
browserstack-sdk pytest -s tests/
```

This runs the **Wikipedia search** scenario on a real Samsung Galaxy S22 Ultra:
tap "Search Wikipedia", type "BrowserStack", and assert results are returned.
It also runs the **local** scenario (LocalSample.apk over the BrowserStack Local
tunnel — `browserstackLocal: true`).

To run a single scenario:

```bash
browserstack-sdk pytest -s tests/test_sample.py
```

## Run Sample Test (iOS)

From inside `ios/`:

```bash
browserstack-sdk pytest -s tests/
```

## Run Local Test

The local scenarios (`tests/test_local.py`) exercise BrowserStack Local. With
`browserstackLocal: true` in `browserstack.yml` the SDK starts the Local tunnel
automatically — no separate binary to launch.

## Notes / Dashboard

- View runs and shareable session links at
[app-automate.browserstack.com](https://app-automate.browserstack.com/).
- Test Observability (`testObservability: true`) reports also appear at
[observability.browserstack.com](https://observability.browserstack.com/).
- The `app:` value can be a local path (the SDK uploads it and rewrites to
`bs://<hashed-id>`) or a pre-uploaded `bs://<id>`.
49 changes: 49 additions & 0 deletions android/browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# =============================
# 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
# ======================
projectName: BrowserStack Samples
buildName: appauto-pytest-bdd-appium
buildIdentifier: '#${BUILD_NUMBER}'
# `framework` lets the SDK instrument pytest-bdd and send test context to BrowserStack.
framework: pytest-bdd

# ==========================================
# Application under test
# ==========================================
# Pre-uploaded WikipediaSample.apk. Use a local path (./WikipediaSample.apk) to
# have the SDK upload+rewrite it, or a pre-uploaded bs://<hashed-id> as below.
app: bs://92d48b416632f2b1734259565ceab61b05ad0b24

# =======================================
# Platforms (Devices to test)
# =======================================
platforms:
- deviceName: Samsung Galaxy S22 Ultra
osVersion: "12.0"
platformName: android

# =======================
# Parallels per Platform
# =======================
parallelsPerPlatform: 1

source: pytest-bdd-appium-app-browserstack:sample-sdk:v1.0

# ======================
# Test Observability
# ======================
testObservability: true

# ===================
# Debugging features
# ===================
debug: true
networkLogs: true
25 changes: 25 additions & 0 deletions android/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

import pytest
from appium import webdriver
from appium.options.android import UiAutomator2Options


@pytest.fixture(scope="function")
def driver(request):
"""Appium driver fixture shared by the pytest-bdd step definitions.

The BrowserStack SDK injects the app + device capabilities from
browserstack.yml, so an empty UiAutomator2Options object is enough — no
hub URL caps or device caps are set here. When launched with
`browserstack-sdk pytest`, the session is routed to the BrowserStack
cloud automatically.
"""
options = UiAutomator2Options()
options.set_capability("bstack:options", {
"userName": os.environ.get("BROWSERSTACK_USERNAME", "YOUR_USERNAME"),
"accessKey": os.environ.get("BROWSERSTACK_ACCESS_KEY", "YOUR_ACCESS_KEY"),
})
drv = webdriver.Remote("https://hub.browserstack.com/wd/hub", options=options)
yield drv
drv.quit()
9 changes: 9 additions & 0 deletions android/features/local.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: BrowserStack App Automate local test
As a BrowserStack user
I want the local sample app to reach a service over the BrowserStack Local tunnel
So that I can verify BrowserStack Local tunnelling works for mobile apps

Scenario: Reach the local endpoint from the device
Given I have launched the local sample app
When I trigger the local network test
Then the app reports it is up and running
9 changes: 9 additions & 0 deletions android/features/sample.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: BrowserStack App Automate sample test
As a BrowserStack user
I want to search Wikipedia in the WikipediaSample app
So that I can verify search results are returned

Scenario: Search Wikipedia for BrowserStack
Given I have launched the Wikipedia sample app
When I search Wikipedia for "BrowserStack"
Then search results are displayed
6 changes: 6 additions & 0 deletions android/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Appium-Python-Client
selenium>=3.14
pytest==7.4.4
pytest-bdd
browserstack-local
browserstack-sdk @ https://sdk-assets.browserstack.com/python/browserstack_sdk-latest.tar.gz
29 changes: 29 additions & 0 deletions android/tests/test_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from pytest_bdd import scenario, given, when, then
from appium.webdriver.common.appiumby import AppiumBy

FEATURES = os.path.join(os.path.dirname(os.path.dirname(__file__)), "features")


@scenario(os.path.join(FEATURES, "local.feature"), "Reach the local endpoint from the device")
def test_local():
"""LocalSample.apk over the BrowserStack Local tunnel (browserstackLocal: true)."""


@given("I have launched the local sample app")
def launch_local_app(driver):
# The SDK launches the local sample app from browserstack.yml.
pass


@when("I trigger the local network test")
def trigger_local_test(driver):
driver.find_element(
AppiumBy.ID, "com.example.android.basicnetworking:id/test_action").click()


@then("the app reports it is up and running")
def app_up_and_running(driver):
texts = driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
assert any("Up and running" in (t.text or "") for t in texts)
32 changes: 32 additions & 0 deletions android/tests/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import time

from pytest_bdd import scenario, given, when, then, parsers
from appium.webdriver.common.appiumby import AppiumBy

FEATURES = os.path.join(os.path.dirname(os.path.dirname(__file__)), "features")


@scenario(os.path.join(FEATURES, "sample.feature"), "Search Wikipedia for BrowserStack")
def test_search_wikipedia():
"""WikipediaSample.apk: search Wikipedia and assert results appear."""


@given("I have launched the Wikipedia sample app")
def launch_app(driver):
# The SDK launches the app (WikipediaSample.apk) from browserstack.yml.
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Search Wikipedia").click()


@when(parsers.parse('I search Wikipedia for "{query}"'))
def search_wikipedia(driver, query):
search = driver.find_element(
AppiumBy.ID, "org.wikipedia.alpha:id/search_src_text")
search.send_keys(query)
time.sleep(5)


@then("search results are displayed")
def results_displayed(driver):
results = driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
assert len(results) > 0
45 changes: 45 additions & 0 deletions ios/browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# =============================
# Set BrowserStack Credentials
# =============================
userName: YOUR_USERNAME
accessKey: YOUR_ACCESS_KEY

# ======================
# BrowserStack Reporting
# ======================
projectName: BrowserStack Samples
buildName: appauto-pytest-bdd-appium
buildIdentifier: '#${BUILD_NUMBER}'
framework: pytest-bdd

# ==========================================
# Application under test
# ==========================================
# Local path — the SDK uploads BStackSampleApp.ipa and rewrites it to bs://<id>.
app: ./BStackSampleApp.ipa

# =======================================
# Platforms (Devices to test)
# =======================================
platforms:
- deviceName: iPhone 14 Pro
osVersion: "16"
platformName: ios

# =======================
# Parallels per Platform
# =======================
parallelsPerPlatform: 1

source: pytest-bdd-appium-app-browserstack:sample-sdk:v1.0

# ======================
# Test Observability
# ======================
testObservability: true

# ===================
# Debugging features
# ===================
debug: true
networkLogs: true
Loading
Loading