Skip to main content
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Load testing with Playwright Python

Run a browser load test on BrowserStack Load Testing with your existing pytest-playwright tests, without rewriting them in JavaScript.

BrowserStack installs the packages in your requirements.txt file, then runs your pytest suite for every virtual user in its own Chrome or Microsoft Edge browser. Load Testing measures every page that your tests open, including page load times, Web Vitals, and network requests.

Prerequisites

  • BrowserStack username and access key. You can find these under your account profile.
  • A pytest project that uses pytest-playwright with the synchronous API, through the page, context, or browser fixtures.
  • A requirements.txt file at the root of the project that lists pytest-playwright, or both playwright and pytest.
  • Test files that pytest can discover. By default, pytest discovers files named test_*.py or *_test.py.
  • Access from the BrowserStack load generator IP addresses to your private package index, if the index is behind a firewall. For the list of IP addresses to allowlist, see Test private websites and APIs.

Tests that use the page fixture run without code changes.

Supported versions

BrowserStack preinstalls the following versions for Playwright Python load tests:

Framework Python version Test framework Preinstalled packages
Playwright Python Python 3.10 pytest with pytest-playwright, synchronous API pytest 9.2.0, pytest-playwright 0.10.0, Playwright 1.60

BrowserStack installs the packages in your requirements.txt file on top of these versions. A version that you pin in requirements.txt replaces the preinstalled version, so pinned versions must install on Python 3.10. Your tests run on the same browser regardless of which Playwright version you pin.

The load test settings select the browser. Chrome is the default. To run on Microsoft Edge, select Microsoft Edge under Select Browser on the Advanced Settings tab of the Load Configuration step. In browserstack-load.yml, set browserName: edge.

Playwright Python load tests don’t support the following:

  • Async tests: Projects that use pytest-asyncio or pytest-playwright-asyncio stop before the load starts. Use the synchronous page fixture.
  • Firefox, WebKit, and launch_persistent_context(): Load tests run on Chrome or Microsoft Edge only.
  • pytest-xdist: Each virtual user runs one pytest process.
  • Selenium: Selenium code in your project doesn’t drive the virtual user’s browser. For Selenium tests, see Load testing with Selenium Python.

Prepare your project

Keep the requirements.txt file at the root of your project. The folder that contains requirements.txt is the folder that you zip and upload. The following example shows the structure of a project:

Project structure

The following requirements.txt file is the minimum that a Playwright Python load test needs:

requirements.txt

The following test uses the page fixture and runs without changes:

tests/test_checkout.py

Before you zip your project, make the following changes:

  1. Open pages with the page, context, or browser fixture. The load test settings select the browser, not your code. A chromium.launch() call connects to the virtual user’s browser, and BrowserStack ignores its launch options except slow_mo. A firefox, webkit, or launch_persistent_context() call fails the iteration that runs it, and the load test keeps running.
  2. Remove -n, --numprocesses, and --dist from addopts in your pytest configuration. Each virtual user runs one pytest process, and your virtual user count sets the concurrency.
  3. Use paths relative to the project root. Your tests run from the project root, so absolute paths from your machine don’t exist during the load test.
  4. Leave out the __pycache__/, .pytest_cache/, .venv/, and venv/ folders. The CLI excludes these folders automatically. The ZIP file size limit is 250 MB.

BrowserStack installs only the requirements.txt file at the project root. If you split your requirements into several files, reference the other files from the root file with -r, such as -r requirements-test.txt.

If your test files don’t follow the default pytest naming, set python_files in your pytest configuration. The following pytest.ini file makes pytest discover test files named check_*.py:

pytest.ini

Install packages from a private package index

BrowserStack sets the environment variables of your test before it installs your packages. To install packages from a private package index, follow these steps:

  1. Add the PIP_INDEX_URL environment variable and set it to the URL of your index. To keep the public index as well, use PIP_EXTRA_INDEX_URL instead.
  2. If the URL contains a token, mask the variable. In the UI Builder, add the variable on the Env Variables tab and click the closed-eye icon next to it. In browserstack-load.yml, set isMasked: true for the variable.
  3. Make sure that the index is reachable from the internet. If the index is behind a firewall, allowlist the BrowserStack load generator IP addresses. For the list of IP addresses, see Test private websites and APIs.

The following configuration installs packages from a private index and masks the token in its URL:

browserstack-load.yml

BrowserStack masks credentials and tokens in the pip output in the logs. For more information about masked variables, see Environment variables.

Run a test

Perform the following steps to run a load test for your Playwright test suite written in Python:

Create a load test

You can start a new test from the Quick Start page or the Load Tests page on the Load Testing dashboard.

On the dashboard, click Create Load Test.

Load Testing dashboard with the Create Load Test button highlighted

Enter a Test Name for your load test, select Browser Only, and click Upload scripts.

Test name field and load test type options on the Load Testing dashboard

Upload scripts

Zip the folder that contains your requirements.txt file. Leave out the __pycache__/, .pytest_cache/, .venv/, and venv/ folders. The ZIP file size limit is 250 MB.

On the Load Sources step, under Select Browser Load Source, select Upload Automation Project. From the dropdown, select Playwright - Pytest. Then, drag and drop your ZIP file or click Upload a Playwright Project to select it.

Load Sources step with Upload Automation Project and Playwright - Pytest selected, ready for a ZIP upload

After you upload the ZIP file, BrowserStack validates your project and lists any issues to fix. For a list of validation messages and fixes, see Troubleshoot Python load tests.

From the Select dependencies dropdown, select your requirements.txt file. This field is required.

Optionally, under Run Command, click Add flag, select a flag such as --timeout, and enter its value, such as 600. Each row contains one flag. For the flags that you can use, see Pass pytest flags to your test run.

Validated Playwright Python project with requirements.txt selected under Select dependencies and a --timeout flag under Run Command

To try Playwright Python load testing before you upload your own project, click Use Sample Scripts on the Load Sources step to run a load test with the Playwright - Pytest sample.

After you verify the configuration, click Configure Load to move to the next step.

Configure load and run test

You can configure the following load parameters to customize your load test:

By default, each virtual user runs your entire suite. To give parts of your suite their own virtual users, turn on Multiple Scenarios in the Load Configuration section. For each scenario, under Scenario Configuration, enter a Scenario name, select a selector from the Test Selector dropdown, and select or enter its value. To add more scenarios, click Add Another Scenario.

You can use the following test selectors:

UI builder selector YAML type Value Example
Marker tag One marker name, without @. Expressions such as smoke and not slow aren’t supported. smoke
Class (nodeid) class path/to/test_file.py::TestClass. For a test function outside a class, use path/to/test_file.py::test_function. tests/test_checkout.py::TestCheckout
Method (nodeid) method A method in a class, as path/to/test_file.py::TestClass::test_method. tests/test_checkout.py::TestCheckout::test_pay_by_card
Directory / file package A folder or file in the project. The path can’t contain .. and can’t start with / or .. tests/test_search.py
pytest.ini / conftest.py configFile A pytest configuration file in the project. The scenario uses this file instead of the root configuration. pytest.ini

The following rules apply to scenarios:

  • You can add up to 10 scenarios.
  • Scenario names are limited to 50 characters, and selector values can be up to 256 characters.
  • You can’t select a parametrized test ID, such as test_login[admin]. Add a marker to the test and use the Marker selector instead.

Load Configuration step with Multiple Scenarios turned on and the Test Selector dropdown listing the five pytest selectors

Your test runs on Chrome by default. To run it on Microsoft Edge, open the Advanced Settings tab and select Microsoft Edge under Select Browser.

After you finish configuring load parameters, click Save & Run Test. To save the test without running it, click Save Only.

Pass pytest flags to your test run

Use runCommand to add flags to the pytest command that runs your tests. BrowserStack starts pytest for you, so add flags only. All flags must fit on one line of up to 4,096 characters. You can also add test paths, such as tests/test_search.py. If a scenario also has a test selector, pytest runs the tests that match either one.

You can set runCommand in the following places:

  • In the CLI, at the top level of browserstack-load.yml.
  • In the UI Builder of a Browser Only test, under Run Command on the Load Sources step. Click Add flag for each flag, select the flag, and enter its value.

runCommand applies to the whole load test. Don’t set runCommand inside scenarios. BrowserStack rejects the configuration.

You can use the following flags:

Flag Limits and requirements
-k N/A
-m N/A
--ignore, --ignore-glob, --deselect N/A
--timeout 0 to 3600 seconds. Add pytest-timeout to requirements.txt.
--maxfail 0 to 1000
-x N/A
--reruns 0 to 10. Add pytest-rerunfailures to requirements.txt.
--reruns-delay 0 to 300 seconds
-v, -q, --tb N/A
--strict-markers, --strict-config N/A
--base-url N/A

BrowserStack blocks the following flags. If you add one, BrowserStack doesn’t save the test, and the message names the flag:

Flags Reason
--browser, --browser-channel, --headed, --device, --video, --tracing, --screenshot, --output BrowserStack sets these options. The load test settings select the browser. To retain the recordings, turn on Capture artifacts in the load test settings.
-n, --numprocesses, --dist, --forked, --boxed Each virtual user runs one pytest process. Set the concurrency using your virtual user count.
--lf, --ff, --nf, --sw Each run starts fresh without a pytest cache.
-c, --config, -o, --noconftest, --rootdir, --confcutdir BrowserStack sets the configuration and root folder of pytest. Add a pytest.ini or pyproject.toml file to your project, or use the configFile test selector.
--slowmo, --pdb, --pdbcls, --trace, -s These flags change the timing, wait for input, or flood the logs.
--collect-only, --setup-only These flags don’t run tests, so they generate no load.
--junit-xml, --html, --alluredir, --cov, --benchmark-enable, --pastebin Load tests generate their own reports and timings, and don’t send output to external services.

BrowserStack rejects any other flag as unknown, and the CLI suggests the closest allowed flag.

BrowserStack doesn’t check the flags in addopts in your pytest configuration. If addopts sets --browser firefox or --browser webkit, the test stops before the load starts. BrowserStack ignores --browser-channel, --headed, and --video in addopts and adds a note in the logs.

The following configuration runs only the tests with the smoke marker and stops each iteration after five failures:

browserstack-load.yml

How BrowserStack runs your Python tests

Review the following behaviors before you analyze your test results:

  • Setup: Before the load starts, each virtual user installs the packages in requirements.txt and checks that pytest can collect your tests. Package installation must finish within 10 minutes, and test collection within 2 minutes. A large requirements.txt file adds to the start-up time of every virtual user.
  • Iterations: Each virtual user runs pytest once per iteration. A failed test fails only that iteration, and the virtual user starts the next iteration.
  • Test timeout: Each test has a 90-second limit by default. To change the limit, add pytest-timeout to requirements.txt and set timeout in pytest.ini, or pass --timeout in runCommand.
  • Browser settings: Custom headers and the screen resolution of the load test apply to the page and context fixtures and to the contexts that your tests create, unless a test sets its own value. Network throttling applies to the virtual user’s browser.
  • Test data: To read a test data file, open it by its path in your project, such as data/users.csv. Each virtual user reads its own slice of the file at that path.
  • Test results: The Tests tab isn’t available for Playwright Python tests yet.

View test results

The test report includes page performance, Web Vitals, and network requests for the pages that your tests open. The report header shows the run status, the number of virtual users, the duration, and the load zones of the run. It also has the Summary, Browser, Logs, and Engine Health tabs.

Load test report header for a Playwright Python run with the run status, virtual users, duration, and load zone

To check the framework of a run, click View metadata in the report header. On the Scripts & data tab, Test type shows Playwright - Pytest.

To check the installed versions, open the logs of the run. In the output of each iteration, the pytest header shows the pytest version, and the plugins line lists pytest-playwright as playwright-<version>. The -q flag hides this header. If package installation fails, the test doesn’t start, and the setup log shows the reason followed by the pip output.

For more information, see View browser test results.

Troubleshoot Python load tests

BrowserStack validates your project when you upload it and when the test starts. If a problem stops your project from running, BrowserStack blocks the upload. The following table lists the messages you might see:

Message How to fix
We couldn’t find a requirements.txt in your zip. Add a requirements.txt file at the root of the folder that you zip, and upload the project again.
Your requirements.txt doesn’t list pytest-playwright. Add pytest-playwright, or both playwright and pytest, to requirements.txt and upload the project again.
We couldn’t find any pytest test files in your zip. Name your test files test_*.py or *_test.py, or set python_files in your pytest configuration. Then, upload the project again.

The following table lists the warnings you might see when you upload your project:

Warning What to do
Your requirements.txt includes Selenium. Playwright - Pytest runs your Playwright tests only; Selenium code in this project won’t drive the load-test Chrome. To load test Selenium code, create a Selenium load test. Remove the Selenium code from this project, or create a Selenium load test for it.
Your requirements.txt includes <package>. Playwright - Pytest load tests support only the synchronous pytest-playwright fixtures, so async tests aren’t supported. Write your tests with the sync page fixture. Rewrite async tests with the synchronous page fixture, and remove pytest-asyncio or pytest-playwright-asyncio.
Your pytest config runs tests in parallel (-n). Each virtual user runs one pytest process, and the virtual-user count sets concurrency. Remove -n from addopts, or the test will fail when it starts. Remove -n from addopts in your pytest configuration and upload the project again.
<file> launches its own browser (<API>). Load tests redirect a Chromium launch to the load-test Chrome and ignore its launch options. Use the page fixture instead. Use the page fixture. The test still runs, but BrowserStack ignores the launch options.
<file> launches its own browser (<API>). Load tests run every test in the load-test Chrome, so this call will fail. Use the page fixture instead. Replace the Firefox, WebKit, or launch_persistent_context() call with the page fixture. Otherwise, every iteration that runs this call fails.

The following table lists the messages you might see when you run the test from the CLI:

Message How to fix
Playwright - Pytest tests require language set to python. Set language to python, or remove the language key.
Playwright - Pytest load tests install your project from requirements.txt. The first dependency file must be requirements.txt at the root of your project, but got <file>. Add ./requirements.txt as the first entry under files.dependencies and run again. Set ./requirements.txt as the first entry under files.dependencies.

If setup fails, the run shows the Error status, and the Summary tab shows an Initialization error banner. AI Insights below the banner explains the cause and suggests a fix. Click See logs to open the logs of the run, which contain the full pip or pytest output.

Failed run Summary tab with an Initialization error banner and AI Insights about a package install failure

The following table lists common problems, the messages in the logs, and how to fix them:

Problem Message in the logs How to fix
Packages failed to install [ERROR] [LTS-PYTEST] Dependency install failed: pip install -r requirements.txt (exit 1) Check the package names and versions in requirements.txt. Run pip install -r requirements.txt in a clean Python 3.10 environment and fix any errors. For a private index, check PIP_INDEX_URL and the network access to the package index.
Package installation timed out [ERROR] [LTS-PYTEST] setup timeout after 600s (pip install) Remove unused packages from requirements.txt and pin their versions. Package installation must finish within 10 minutes.
Test collection timed out [ERROR] [LTS-PYTEST] setup timeout after 120s (test collection), with the message “Collecting your tests took longer than 2 minutes. Check for slow imports or network calls at import time, then run again.” Move network calls and slow setup out of import time, such as into fixtures.
No tests found [ERROR] [LTS-PYTEST] No tests collected, with the message “No tests were collected. Check the selector, the test paths and the pytest configuration, then run again.” Check the value of the scenario test selector, testpaths and python_files in your pytest configuration, and your marker names.
Tests failed to import [ERROR] [LTS-PYTEST] Test collection failed, with the message “pytest could not import or collect your tests. Fix the errors shown below, then run again.” The pytest output follows. Read the pytest output below the message. The cause is usually a syntax error, a package missing from requirements.txt, or a failed import in conftest.py.
Unsupported option [ERROR] [LTS-PYTEST] Unsupported option: <option>, such as parallel workers (pytest-xdist) or --browser firefox. Or Unsupported option or invalid pytest arguments. A line that says what to remove follows. Remove the option that the log names from addopts or runCommand. If the log says the pytest arguments are invalid, check runCommand and your pytest configuration for a typo or a nonexistent path.
Tests failed [LTS-PYTEST] Tests failed: 2 failed. The counts vary, such as 1 failed, 2 errors. Read the failed assertion in the pytest output. Under load, failures often come from timeouts, so increase your test timeout or your application’s capacity. If the output says Unsupported browser API, your test calls firefox, webkit, or launch_persistent_context(). Use the page, context, or browser fixture instead. Only that iteration fails.
Unexpected error The test environment could not be prepared. This is a problem on our side; please contact support with the test ID. Contact support with the test ID. If you pinned old versions of pytest, pytest-playwright, or Playwright, remove the pins and run the test again.

If you run into issues, try the troubleshooting steps or contact us for help.

What’s next

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle