From 289f148ac4d60379114a5940f06aeaf72f5205e5 Mon Sep 17 00:00:00 2001 From: Siddharth Singha Roy Date: Wed, 8 Jan 2025 14:49:31 +0530 Subject: [PATCH 1/2] chore(): add experimental support for playwright python --- .gitignore | 1 + samples/experimental/README.md | 3 + samples/experimental/python/README.md | 58 +++++++++++++++++++ .../experimental/python/playwright_service.py | 18 ++++++ samples/experimental/python/requirements.txt | 2 + samples/experimental/python/test_example.py | 35 +++++++++++ 6 files changed, 117 insertions(+) create mode 100644 samples/experimental/README.md create mode 100644 samples/experimental/python/README.md create mode 100644 samples/experimental/python/playwright_service.py create mode 100644 samples/experimental/python/requirements.txt create mode 100644 samples/experimental/python/test_example.py diff --git a/.gitignore b/.gitignore index dfcfd56..9b47671 100644 --- a/.gitignore +++ b/.gitignore @@ -305,6 +305,7 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ *.pyc +venv/ # Cake - Uncomment if you are using it # tools/** diff --git a/samples/experimental/README.md b/samples/experimental/README.md new file mode 100644 index 0000000..5b48f73 --- /dev/null +++ b/samples/experimental/README.md @@ -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. diff --git a/samples/experimental/python/README.md b/samples/experimental/python/README.md new file mode 100644 index 0000000..2f0bf3c --- /dev/null +++ b/samples/experimental/python/README.md @@ -0,0 +1,58 @@ +# 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. + +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" + ``` diff --git a/samples/experimental/python/playwright_service.py b/samples/experimental/python/playwright_service.py new file mode 100644 index 0000000..dd802ba --- /dev/null +++ b/samples/experimental/python/playwright_service.py @@ -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 diff --git a/samples/experimental/python/requirements.txt b/samples/experimental/python/requirements.txt new file mode 100644 index 0000000..0738cbf --- /dev/null +++ b/samples/experimental/python/requirements.txt @@ -0,0 +1,2 @@ +playwright==1.47 +pytest-playwright==0.6.2 \ No newline at end of file diff --git a/samples/experimental/python/test_example.py b/samples/experimental/python/test_example.py new file mode 100644 index 0000000..018ce36 --- /dev/null +++ b/samples/experimental/python/test_example.py @@ -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="", + ) + 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() From c1af79d406f0559f810bad7019e42d80317b9f55 Mon Sep 17 00:00:00 2001 From: Siddharth Singha Roy Date: Thu, 9 Jan 2025 01:21:03 +0530 Subject: [PATCH 2/2] docs(): add note for current support --- samples/experimental/python/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/experimental/python/README.md b/samples/experimental/python/README.md index 2f0bf3c..d3854bc 100644 --- a/samples/experimental/python/README.md +++ b/samples/experimental/python/README.md @@ -2,6 +2,8 @@ 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