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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
venv/

# Cake - Uncomment if you are using it
# tools/**
Expand Down
3 changes: 3 additions & 0 deletions samples/experimental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Experimental features

Microsoft Playwright Testing currently only supports the playwright test and NUnit runner. The samples in this folder are for experimental features that show how to use the service with other test runners.
60 changes: 60 additions & 0 deletions samples/experimental/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Run Playwright Python tests with Microsoft Playwright Testing (Experimental)

This sample demonstrates how to run Playwright Python tests at scale using Microsoft Playwright Testing. It showcases the benefits of accelerating test suite completion by leveraging more parallel cloud browsers. The tests are executed using Pytest runner.

**Note** - You won't be able to get the support for reporting and authentication to the service from the client running the tests via Entra ID.

If you have not yet created a workspace, please follow the [Get Started guide](../../../README.md#get-started)

## Sample setup

1. Clone this sample:

```bash
git clone https://github.com/microsoft/playwright-testing-service
cd playwright-testing-service/samples/experimental/python
```

1. Install dependencies:

```bash
pip install -r requirements.txt
```

1. Set up Authentication using Access Tokens:

Currently, only access tokens are supported for Python. See [Set up authentication using access tokens](../../../../README.md#set-up-authentication-using-access-tokens)

1. Set access token generated above as environment variable for your project:

```bash
PLAYWRIGHT_SERVICE_ACCESS_TOKEN= # Paste Access Token value from previous step
```

1. Set up environment:

In the [Playwright portal](https://aka.ms/mpt/portal), copy the command under **Add region endpoint in your set up** and set the following environment variable:

```bash
PLAYWRIGHT_SERVICE_URL= # Paste region endpoint URL
```

## Run tests

Run Playwright tests against browsers managed by the service using the configuration you created above. You can run up to 50 parallel workers with the service

```bash
pytest
```

## Add more configuration

You can use the following environment variables to specify configuration parameters for the service:

1. **PLAYWRIGHT_SERVICE_RUN_ID**: This variable allows you to change the ID of the test run. The run ID is a unique identifier for a test run and is used to track test runs in the portal.

Example :

```bash
export PLAYWRIGHT_SERVICE_RUN_ID = "my_custom_runId"
```
18 changes: 18 additions & 0 deletions samples/experimental/python/playwright_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import uuid


def get_connect_options(os_name="linux", run_id="") -> tuple[str, dict[str, str]]:
if run_id:
os.environ["PLAYWRIGHT_SERVICE_RUN_ID"] = run_id
if not os.environ.get("PLAYWRIGHT_SERVICE_RUN_ID"):
os.environ["PLAYWRIGHT_SERVICE_RUN_ID"] = str(uuid.uuid4())

service_url = os.getenv("PLAYWRIGHT_SERVICE_URL")
service_access_token = os.getenv("PLAYWRIGHT_SERVICE_ACCESS_TOKEN")

headers = {"Authorization": f"Bearer {service_access_token}"}
service_run_id = os.getenv("PLAYWRIGHT_SERVICE_RUN_ID")
ws_endpoint = f"{service_url}?os={os_name}&runId={service_run_id}&api-version=2023-10-01-preview"

return ws_endpoint, headers
2 changes: 2 additions & 0 deletions samples/experimental/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
playwright==1.47
pytest-playwright==0.6.2
35 changes: 35 additions & 0 deletions samples/experimental/python/test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import re
import pytest
from typing import Generator
from playwright.sync_api import Page, BrowserType, expect
from playwright_service import get_connect_options


@pytest.fixture(scope="session")
def browser(browser_type: BrowserType) -> Generator[BrowserType, None, None]:
wsEndpoint, headers = get_connect_options()
browser = browser_type.connect(
ws_endpoint=wsEndpoint,
headers=headers,
timeout=30000,
expose_network="<loopback>",
)
yield browser
browser.close()


def test_has_title(page: Page):
page.goto("https://playwright.dev/")

# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))


def test_get_started_link(page: Page):
page.goto("https://playwright.dev/")

# Click the get started link.
page.get_by_role("link", name="Get started").click()

# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()