Edit

Share via


Quickstart: Run end-to-end tests at scale with Playwright Workspaces

In this quickstart, you learn how to run your Playwright tests with highly parallel cloud browsers using Playwright Workspaces. Use cloud infrastructure to validate your application across multiple browsers, devices, and operating systems.

After you complete this quickstart, you have a Playwright workspace to run your Playwright tests at scale.

Important

Playwright Workspaces is currently in preview. For legal terms that apply to Azure features that are in beta, in preview, or otherwise not yet released into general availability, see the Supplemental Terms of Use for Microsoft Azure Previews.

Prerequisites

Create a workspace

To get started with running your Playwright tests on cloud browsers, you first need to create a Playwright workspace.

  1. Sign in to the Azure portal.

  2. Select the menu button in the upper-left corner of the portal, and then select Create a resource a resource.

    Screenshot that shows the Azure portal menu to create a new resource.

  3. Enter Playwright Workspaces in the search box.

  4. Select the Playwright Workspaces card, and then select Create.

    Screenshot that shows the Azure Marketplace search page with the Playwright Workspaces search result.

  5. Provide the following information to configure a new Playwright workspace:

    Field Description
    Subscription Select the Azure subscription that you want to use for this Playwright workspace.
    Resource group Select an existing resource group. Or select Create new, and then enter a unique name for the new resource group.
    Name Enter a unique name to identify your workspace.
    The name can only consist of alphanumerical characters, and have a length between 3 and 64 characters.
    Location Select a geographic location to host your workspace.
    This location also determines where the test execution results are stored.

    Note

    Optionally, you can configure more details on the Tags tab. Tags are name/value pairs that enable you to categorize resources and view consolidated billing by applying the same tag to multiple resources and resource groups.

  6. After you're finished configuring the resource, select Review + Create.

  7. Review all the configuration settings and select Create to start the deployment of the Playwright workspace.

    When the process has finished, a deployment success message appears.

  8. To view the new workspace, select Go to resource.

    Screenshot that shows the deployment completion information in the Azure portal.

Install Playwright Workspaces package

To use the service, install the Playwright Workspaces package.

npm init @azure/playwright@beta

This generates a playwright.service.config.ts file which serves to direct and authenticate Playwright to Playwright Workspaces.

If you already have this file, the package asks you to overwrite it.

To use the service, install the Playwright Workspaces package.

dotnet add package Azure.Developer.Playwright.NUnit --prerelease

Configure the service region endpoint

In your setup, you have to provide the region-specific service endpoint. The endpoint depends on the Azure region you selected when creating the workspace.

To get the service endpoint URL, perform the following steps:

  1. Sign in to the Azure portal with your Azure account and navigate to your workspace.

  2. Select the Get Started page.

    Screenshot that shows how to navigate to the Get Started page.

  3. In Add region endpoint in your setup, copy the service endpoint URL.

    Make sure this URL is available in PLAYWRIGHT_SERVICE_URL environment variable.

    Screenshot that shows how to copy the service endpoint URL.

Set up your environment

To set up your environment, you have to configure the PLAYWRIGHT_SERVICE_URL environment variable with the value you obtained in the previous steps.

We recommend that you use the dotenv module to manage your environment. With dotenv, you define your environment variables in the .env file.

  1. Add the dotenv module to your project:

    npm i --save-dev dotenv
    
  2. Create a .env file alongside the playwright.config.ts file in your Playwright project:

    PLAYWRIGHT_SERVICE_URL={MY-REGION-ENDPOINT}
    

    Make sure to replace the {MY-REGION-ENDPOINT} text placeholder with the value you copied earlier.

Set up service configuration

Create a file PlaywrightServiceSetup.cs in your project with the following content.

using Azure.Developer.Playwright.NUnit;
using Azure.Identity;

namespace PlaywrightTests; // Remember to change this as per your project namespace

[SetUpFixture]
public class PlaywrightServiceNUnitSetup  : PlaywrightServiceBrowserNUnit
{
    public PlaywrightServiceNUnitSetup() : base(
        credential: new DefaultAzureCredential()
    ) 
    {}
}

Note

Make sure your project uses Microsoft.Playwright.NUnit version 1.50 or above.

Set up Authentication

To run your Playwright tests in your Playwright workspace, you need to authenticate the Playwright client where you're running the tests with the service. This could be your local dev machine or CI machine.

The service offers two authentication methods: Microsoft Entra ID and Access Tokens.

Microsoft Entra ID uses your Azure credentials, requiring a sign-in to your Azure account for secure access. Alternatively, you can generate an access token from your Playwright workspace and use it in your setup.

Set up authentication using Microsoft Entra ID

Microsoft Entra ID is the default and recommended authentication for the service. From your local dev machine, you can use Azure CLI to sign-in

az login

Note

If you're a part of multiple Microsoft Entra tenants, make sure you sign in to the tenant where your workspace belongs. You can get the tenant ID from Azure portal. See Find your Microsoft Entra Tenant. Once you get the ID, sign-in using the command az login --tenant <TenantID>

Set up authentication using access tokens

You can generate an access token from your Playwright workspace and use it in your setup. However, we strongly recommend Microsoft Entra ID for authentication due to its enhanced security. Access tokens, while convenient, function like long-lived passwords and are more susceptible to being compromised.

  1. Authentication using access tokens is disabled by default. To use, Enable access-token based authentication.

  2. Set up authentication using access tokens.

Caution

We strongly recommend using Microsoft Entra ID for authentication to the service. If you are using access tokens, see How to Manage Access Tokens.

Run your tests at scale with Playwright Workspaces

You've now prepared the configuration for running your Playwright tests in the cloud with Playwright Workspaces. You can either use the Playwright CLI to run your tests, or use the Playwright Test Visual Studio Code extension.

Run a single test with the service

With Playwright Workspaces, you get charged based on the number of total test minutes. If you're a first-time user or getting started with a free trial, you might start with running a single test instead of your full test suite to avoid exhausting your free trial limits.

After you validate that the test runs successfully, you can gradually increase the test load by running more tests with the service.

Perform the following steps to run a single Playwright test with Playwright Workspaces:

To use the Playwright CLI to run your tests with Playwright Workspaces, pass the service configuration file as a command-line parameter.

  1. Open a terminal window.

  2. Enter the following command to run your Playwright test on remote browsers in your workspace:

    Replace the {name-of-file.spec.ts} text placeholder with the name of your test specification file.

    npx playwright test {name-of-file.spec.ts} --config=playwright.service.config.ts
    

    After the test completes, you can view the test status in the terminal.

    Running 1 test using 1 worker
        1 passed (2.2s)
    
    To open last HTML report run:
    
    npx playwright show-report
    

You can now run multiple tests with the service, or run your entire test suite on remote browsers.

Caution

Depending on the size of your test suite, you might incur additional charges for the test minutes and test results beyond your allotted free test minutes and free test results.

Run a full test suite with the service

Now that you've validated that you can run a single test with Playwright Workspaces, you can run a full Playwright test suite at scale.

Perform the following steps to run a full Playwright test suite with Playwright Workspaces:

When you run multiple Playwright tests or a full test suite with Playwright Workspaces, you can optionally specify the number of parallel workers as a command-line parameter.

  1. Open a terminal window.

  2. Enter the following command to run your Playwright test suite on remote browsers in your workspace:

    npx playwright test --config=playwright.service.config.ts --workers=20
    

    Depending on the size of your test suite, this command runs your tests on up to 20 parallel workers.

    After the test completes, you can view the test status in the terminal.

    Running 6 tests using 6 workers
        6 passed (18.2s)
    

Run Playwright tests against browsers managed by the service using the configuration you created above.

dotnet test -- NUnit.NumberOfTestWorkers=20

After the test run completes, you can view the test status in the terminal.

Starting test execution, please wait...

A total of 100 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     100, Skipped:     0, Total:     100, Duration: 59 s - PlaywrightTestsNUnit.dll (net7.0)

Workload updates are available. Run `dotnet workload list` for more information.

Optimize parallel worker configuration

Once your tests are running smoothly with the service, experiment with varying the number of parallel workers to determine the optimal configuration that minimizes test completion time.

With Playwright Workspaces, you can run with up to 50 parallel workers. Several factors influence the best configuration for your project, such as the CPU, memory, and network resources of your client machine, the target application's load-handling capacity, and the type of actions carried out in your tests.

You can specify the number of parallel workers on the Playwright CLI command-line, or configure the workers property in the Playwright service configuration file.

You can specify the number of parallel workers on the Playwright CLI command-line, or configure the NumberOfTestWorkers property in the .runsettings file.

Learn more about how to determine the optimal configuration for optimizing test suite completion.

Next step

You've successfully created a Playwright workspace in the Azure portal and run your Playwright tests on cloud browsers.

Advance to the next quickstart to set up continuous end-to-end testing by running your Playwright tests in your CI/CD workflow.