你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Playwright Workspaces 使用服务包中的可用选项

本文介绍如何使用系统生成的 playwright.service.config.ts 文件中可用的选项。 如果代码中没有此文件,请参阅 快速入门:利用 Playwright 工作区大规模执行端到端测试

本文介绍如何使用 PlaywrightServiceSetup.cs 文件中可用的选项。 如果代码中没有此文件,请参阅 快速入门:利用 Playwright 工作区大规模执行端到端测试

先决条件

下面是显示所有受支持的配置选项的文件的完整示例 playwright.service.config.ts

import { createAzurePlaywrightConfig, ServiceOS, ServiceAuth } from "@azure/playwright";
import { defineConfig } from "@playwright/test";
import { AzureCliCredential } from "@azure/identity";
import config from "./playwright.config";

export default defineConfig(
  config,
  createAzurePlaywrightConfig(config, {
    serviceAuthType: ServiceAuth.ACCESS_TOKEN // Use this option if authenticating with access tokens. This mode of authentication must be explicitly enabled in your workspace.
    os: ServiceOS.WINDOWS, // Specify the browser's OS your tests will automate.
    runName: "Sample-Run-Name1", // Set a Name for every test run to distinguish them in the azure portal.
    credential: new AzureCliCredential(), // Select the authentication method you want to use with Entra.
    exposeNetwork: '<loopback>', // Allows cloud browsers to access local resources from your Playwright test code without additional firewall config.
    connectTimeout: 30000 // Set the timeout for your tests (in milliseconds).
  })
);

playwright.service.config.ts 文件中的设置

  • serviceAuthType:

    • 说明:此设置允许你选择要用于测试运行的身份验证方法
    • 可用选项
      • ServiceAuth.ACCESS_TOKEN 表示使用访问令牌。 如果你要使用此选项,则需要启用使用访问令牌的身份验证,具体请参阅管理身份验证
      • ServiceAuth.ENTRA_ID 表示使用 Microsoft Entra ID 进行身份验证。 它是默认模式。
    • 默认值:ServiceAuth.ENTRA_ID
    • 示例:
      serviceAuthType: ServiceAuth.ENTRA_ID
      
  • os:

    • 说明:此设置允许你选择运行操作系统,其中托管了运行 Playwright 测试的浏览器
    • 可用选项
      • ServiceOS.WINDOWS(适用于 Windows 操作系统)。
      • ServiceOS.LINUX(适用于 Linux 操作系统)。
    • 默认值:ServiceOS.LINUX
    • 示例:
      os: ServiceOS.WINDOWS
      
  • runName:

    • 说明:此设置允许为每个测试运行设置自定义名称,以便在门户中区分它们。
    • 示例:
      runName: "Sample-Run-Name1"
      
  • credential:

    • 说明:此设置允许你选择要与 Microsoft Entra ID 配合使用的身份验证方法。 如果 serviceAuthType 没有被设置为 ,则必须指定此项。
    • 示例:
      credential: new AzureCliCredential()
      
  • exposeNetwork

    • 说明:此设置允许你从 Playwright 测试代码连接到本地资源,而无需配置其他防火墙设置。 有关详细信息,请参阅如何测试本地应用程序
    • 示例:
      exposeNetwork: '<loopback>'
      
  • connectTimeout

    • 说明:此设置允许你设置将测试连接到云托管的浏览器的超时期限
    • 示例:
      connectTimeout: 30000,
      

下面是安装文件版本,其中包含所有可用选项:

using Azure.Developer.Playwright.NUnit;
using Azure.Developer.Playwright;
using Azure.Identity;
using System.Runtime.InteropServices;
using System;

namespace PlaywrightService.SampleTests; // Remember to change this as per your project namespace

[SetUpFixture]
public class PlaywrightServiceNUnitSetup : PlaywrightServiceBrowserNUnit
{
    public PlaywrightServiceNUnitSetup() : base(
        credential: new ManagedIdentityCredential(), // Select the authentication method you want to use with Entra.
        options: new PlaywrightServiceBrowserClientOptions()
        {
            OS = OSPlatform.Linux, // Specify the browser's OS your tests will automate.
            ExposeNetwork = "<loopback>", // Allows cloud browsers to access local resources from your Playwright test code without additional firewall config.
            RunName = 'CustomRun', // Set a name for every test run to distinguish them in the portal.
            ServiceAuth = ServiceAuthType.EntraId // Use this option if authenticating with access tokens. This mode of authentication must be explicitly enabled in your workspace.
        }
    )
    {
        // no-op
    }
}

安装程序文件中的配置选项

  • ServiceAuth:

    • 说明:此设置允许你选择要用于测试运行的身份验证方法
    • 可用选项
      • ServiceAuthType.AccessToken 表示使用访问令牌。 如果你要使用此选项,则需要启用使用访问令牌的身份验证,具体请参阅管理身份验证
      • ServiceAuthType.EntraId 表示使用 Microsoft Entra ID 进行身份验证。 它是默认模式。
    • 默认值:ServiceAuthType.EntraId
  • OS:

    • 说明:此设置允许你选择运行操作系统,其中托管了运行 Playwright 测试的浏览器
    • 可用选项
      • OSPlatform.Windows(适用于 Windows 操作系统)。
      • OSPlatform.Linux(适用于 Linux 操作系统)。
    • 默认值:OSPlatform.Linux
  • RunName:

    • 说明:此设置允许为每个测试运行设置一个名称,以便在服务门户中区分它们。 如果未设置,服务包将在每次触发测试运行时生成唯一 ID。
  • credential:

    • 说明:此设置允许你选择要与 Microsoft Entra ID 配合使用的身份验证方法。 如果 ServiceAuth 没有被设置为 ,则必须指定此项。
  • ExposeNetwork

    • 说明:此设置允许你从 Playwright 测试代码连接到本地资源,而无需配置其他防火墙设置。 有关详细信息,请参阅如何测试本地应用程序