Manage authentication and authorization for Microsoft Playwright Testing preview

In this article, you learn how to manage authentication and authorization for Microsoft Playwright Testing preview. Authentication is required to run Playwright tests on cloud-hosted browsers and to publish test results and artifacts to the service.

By default, Microsoft Entra ID is used for authentication. This method is more secure and is the recommended authentication method. You can't disable authentication using Microsoft Entra ID. However, you can also use access tokens to authenticate and authorize.

Important

Microsoft Playwright Testing 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.

Background

Microsoft Playwright Testing Preview is built on the Playwright open-source framework. It runs Playwright tests on cloud-hosted browsers and publishes reports and artifacts back to the service.

To use the service, the client must authenticate with the service to access the browsers. Similarly, publishing results and artifacts requires authenticated API interactions. 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. 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.

Enable authentication using access-tokens

Microsoft Playwright Testing service also supports authentication using access tokens. This authentication method is less secure. We recommend using Microsoft Entra ID to authenticate to the service.

Caution

Your workspace access tokens are similar to a password for your Microsoft Playwright Testing workspace. Always be careful to protect your access tokens. Avoid distributing access tokens to other users, hard-coding them, or saving them anywhere in plain text that is accessible to others.

Revoke and recreate your tokens if you believe they are compromised.

To enable authentication using access tokens:

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

  2. Select the settings icon on the home page to go to the workspace settings.

  3. Select the Authentication page and turn on Enable authentication using Access tokens

    Screenshot that shows the access tokens settings page in the Playwright portal.

Caution

Authentication using access tokens is less secure. Learn how to manage access tokens

Set up authentication using access-tokens

  1. While running the tests, enable access token auth in the playwright.service.config.ts file in your setup.

    /* Learn more about service configuration at https://aka.ms/mpt/config */
    export default defineConfig(config, getServiceConfig( config {
        serviceAuthType:'ACCESS_TOKEN'
    }));
    
  1. While running the tests, enable access token auth in the .runsettings file in your setup.

    <TestRunParameters>
        <!-- Use this option when you want to authenticate using access tokens. This mode of auth should be enabled for the workspace. -->
         <Parameter name="ServiceAuthType" value="AccessToken" />
    </TestRunParameters>
    
  1. Create access token

    Follow the steps to create an access token. Copy the value of the access token generated.

  1. Set up your environment

    To set up your environment, configure the PLAYWRIGHT_SERVICE_ACCESS_TOKEN environment variable with the value you obtained in the previous steps. Ensure this environment variable is available in your setup where you are running tests.

    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_ACCESS_TOKEN={MY-ACCESS-TOKEN}
      

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

  1. Set up your environment

    To set up your environment, configure the PLAYWRIGHT_SERVICE_ACCESS_TOKEN environment variable with the value you obtained in the previous steps. Ensure this environment variable is available in your setup where you are running tests.

Run tests on the service and publish results

Run Playwright tests against cloud-hosted browsers and publish the results to the service using the configuration you created above.

npx playwright test --config=playwright.service.config.ts --workers=20
dotnet test --settings:.runsettings --logger "microsoft-playwright-testing" -- NUnit.NumberOfTestWorkers=20