azure-resource-manager-playwright-dotnet
microsoft/skills
通过 Azure 资源管理器管理 Microsoft Playwright Testing 工作区:使用 .NET SDK 创建、更新、删除工作区,检查名称可用性,并管理配额。
...展开全部Azure.ResourceManager.Playwright (.NET)
用于通过 Azure 资源管理器配置和管理 Microsoft Playwright Testing 工作区的管理平面 SDK。
⚠️ 管理与测试执行
- 此 SDK (Azure.ResourceManager.Playwright):创建工作区、管理配额、检查名称可用性
- 测试执行 SDK (Azure.Developer.MicrosoftPlaywrightTesting.NUnit):在云浏览器上大规模运行 Playwright 测试
安装
dotnet add package Azure.ResourceManager.Playwright
dotnet add package Azure.Identity
当前版本:稳定版 v1.0.0,预览版 v1.0.0-beta.1
环境变量
AZURE_SUBSCRIPTION_ID=<your-subscription-id> # 必需:Azure 订阅 ID
AZURE_TOKEN_CREDENTIALS=prod # 仅在使用生产环境中的 DefaultAzureCredential 时必需
AZURE_TENANT_ID=<tenant-id> # 用于服务主体身份验证(可选)
AZURE_CLIENT_ID=<client-id> # 用于服务主体身份验证(可选)
AZURE_CLIENT_SECRET=<client-secret> # 用于服务主体身份验证(可选)
</client-secret></client-id></tenant-id></your-subscription-id>身份验证
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Playwright;
// 本地开发:DefaultAzureCredential。生产环境:设置 AZURE_TOKEN_CREDENTIALS=prod 或 AZURE_TOKEN_CREDENTIALS=<specific_credential>
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// 或者在生产环境中直接使用特定的凭据:
// 请参阅 https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var armClient = new ArmClient(credential);
// 获取订阅
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var subscription = armClient.GetSubscriptionResource(
new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
</specific_credential>资源层次结构
ArmClient
└── SubscriptionResource
├── PlaywrightQuotaResource (订阅级配额)
└── ResourceGroupResource
└── PlaywrightWorkspaceResource
└── PlaywrightWorkspaceQuotaResource (工作区级配额)
核心工作流
1. 创建 Playwright 工作区
using Azure.ResourceManager.Playwright;
using Azure.ResourceManager.Playwright.Models;
// 获取资源组
var resourceGroup = await subscription
.GetResourceGroupAsync("my-resource-group");
// 定义工作区
var workspaceData = new PlaywrightWorkspaceData(AzureLocation.WestUS3)
{
// 可选:配置区域亲和性和本地身份验证
RegionalAffinity = PlaywrightRegionalAffinity.Enabled,
LocalAuth = PlaywrightLocalAuth.Enabled,
Tags =
{
["Team"] = "Dev Exp",
["Environment"] = "Production"
}
};
// 创建工作区(长时间运行的操作)
var workspaceCollection = resourceGroup.Value.GetPlaywrightWorkspaces();
var operation = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-playwright-workspace",
workspaceData);
PlaywrightWorkspaceResource workspace = operation.Value;
// 获取用于运行测试的数据平面 URI
Console.WriteLine($"Data Plane URI: {workspace.Data.DataplaneUri}");
Console.WriteLine($"Workspace ID: {workspace.Data.WorkspaceId}");
2. 获取现有工作区
// 按名称获取
var workspace = await workspaceCollection.GetAsync("my-playwright-workspace");
// 或者先检查是否存在
bool exists = await workspaceCollection.ExistsAsync("my-playwright-workspace");
if (exists)
{
var existingWorkspace = await workspaceCollection.GetAsync("my-playwright-workspace");
Console.WriteLine($"Workspace found: {existingWorkspace.Value.Data.Name}");
}
3. 列出工作区
// 列出资源组中的工作区
await foreach (var workspace in workspaceCollection.GetAllAsync())
{
Console.WriteLine($"Workspace: {workspace.Data.Name}");
Console.WriteLine($" Location: {workspace.Data.Location}");
Console.WriteLine($" State: {workspace.Data.ProvisioningState}");
Console.WriteLine($" Data Plane URI: {workspace.Data.DataplaneUri}");
}
// 列出订阅中的所有工作区
await foreach (var workspace in subscription.GetPlaywrightWorkspacesAsync())
{
Console.WriteLine($"Workspace: {workspace.Data.Name}");
}
4. 更新工作区
var patch = new PlaywrightWorkspacePatch
{
Tags =
{
["Team"] = "Dev Exp",
["Environment"] = "Staging",
["UpdatedAt"] = DateTime.UtcNow.ToString("o")
}
};
var updatedWorkspace = await workspace.Value.UpdateAsync(patch);
5. 检查名称可用性
using Azure.ResourceManager.Playwright.Models;
var checkRequest = new PlaywrightCheckNameAvailabilityContent
{
Name = "my-new-workspace",
ResourceType = "Microsoft.LoadTestService/playwrightWorkspaces"
};
var result = await subscription.CheckPlaywrightNameAvailabilityAsync(checkRequest);
if (result.Value.IsNameAvailable == true)
{
Console.WriteLine("Name is available!");
}
else
{
Console.WriteLine($"Name unavailable: {result.Value.Message}");
Console.WriteLine($"Reason: {result.Value.Reason}");
}
6. 获取配额信息
// 订阅级配额
await foreach (var quota in subscription.GetPlaywrightQuotasAsync(AzureLocation.WestUS3))
{
Console.WriteLine($"Quota: {quota.Data.Name}");
Console.WriteLine($" Limit: {quota.Data.Limit}");
Console.WriteLine($" Used: {quota.Data.Used}");
}
// 工作区级配额
var workspaceQuotas = workspace.Value.GetAllPlaywrightWorkspaceQuota();
await foreach (var quota in workspaceQuotas.GetAllAsync())
{
Console.WriteLine($"Workspace Quota: {quota.Data.Name}");
}
7. 删除工作区
// 删除(长时间运行的操作)
await workspace.Value.DeleteAsync(WaitUntil.Completed);
关键类型参考
| 类型 | 用途 |
|---|---|
| `ArmClient` | 所有 ARM 操作的入口点 |
| `PlaywrightWorkspaceResource` | 表示 Playwright Testing 工作区 |
| `PlaywrightWorkspaceCollection` | 工作区 CRUD 集合 |
| `PlaywrightWorkspaceData` | 工作区创建/响应负载 |
| `PlaywrightWorkspacePatch` | 工作区更新负载 |
| `PlaywrightQuotaResource` | 订阅级配额信息 |
| `PlaywrightWorkspaceQuotaResource` | 工作区级配额信息 |
| `PlaywrightExtensions` | ARM 资源的扩展方法 |
| `PlaywrightCheckNameAvailabilityContent` | 名称可用性检查请求 |
工作区属性
| 属性 | 描述 |
|---|---|
| `DataplaneUri` | 运行测试的 URI(例如,`https://api.dataplane.{guid}.domain.com`) |
| `WorkspaceId` | 唯一的工作区标识符(GUID) |
| `RegionalAffinity` | 启用/禁用测试执行的区域亲和性 |
| `LocalAuth` | 启用/禁用本地身份验证(访问令牌) |
| `ProvisioningState` | 当前配置状态(成功、失败等) |
最佳实践
- 使用
WaitUntil.Completed用于必须在继续之前完成的操�� - 使用
WaitUntil.Started当您希望手动轮询或并行运行操作时 - 始终使用
DefaultAzureCredential— 切勿硬编码密钥 - 处理
RequestFailedException以应对 ARM API 错误 - 使用
CreateOrUpdateAsync用于幂等操作 - *通过 `Get
方法导航层次结构**(例如,resourceGroup.GetPlaywrightWorkspaces()`) - 在工作区创建后存储 DataplaneUri 用于测试执行配置
错误处理
using Azure;
try
{
var operation = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed, workspaceName, workspaceData);
}
catch (RequestFailedException ex) when (ex.Status == 409)
{
Console.WriteLine("Workspace already exists");
}
catch (RequestFailedException ex) when (ex.Status == 400)
{
Console.WriteLine($"Bad request: {ex.Message}");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"ARM Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");
}
与测试执行的集成
创建工作区后,使用 DataplaneUri 配置您的 Playwright 测试:
// 1. 创建工作区(此 SDK)
var workspace = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed, "my-workspace", workspaceData);
// 2. 获取服务 URL
var serviceUrl = workspace.Value.Data.DataplaneUri;
// 3. 为测试执行设置环境变量
Environment.SetEnvironmentVariable("PLAYWRIGHT_SERVICE_URL", serviceUrl.ToString());
// 4. 使用 Azure.Developer.MicrosoftPlaywrightTesting.NUnit 运行测试
// (用于测试执行的单独包)
相关 SDK
| SDK | 用途 | 安装 |
|---|---|---|
| `Azure.ResourceManager.Playwright` | 管理平面(此 SDK) | `dotnet add package Azure.ResourceManager.Playwright` |
| `Azure.Developer.MicrosoftPlaywrightTesting.NUnit` | 大规模运行 NUnit Playwright 测试 | `dotnet add package Azure.Developer.MicrosoftPlaywrightTesting.NUnit --prerelease` |
| `Azure.Developer.Playwright` | Playwright 客户端库 | `dotnet add package Azure.Developer.Playwright` |
API 信息
- 资源提供程序:
Microsoft.LoadTestService - 默认 API 版本:
2025-09-01 - 资源类型:
Microsoft.LoadTestService/playwrightWorkspaces
文档链接
- Azure.ResourceManager.Playwright API 参考
- Microsoft Playwright Testing 概述
- 快速入门:大规模运行 Playwright 测试
---
name: azure-resource-manager-playwright-dotnet
description: Manage Microsoft Playwright Testing workspaces via Azure Resource Manager: create, update, delete workspaces, check name availability, and manage quotas using the .NET SDK.
license: MIT
---
# Azure.ResourceManager.Playwright (.NET)
Management plane SDK for provisioning and managing Microsoft Playwright Testing workspaces via Azure Resource Manager.
> **⚠️ Management vs Test Execution**
> - **This SDK (Azure.ResourceManager.Playwright)**: Create workspaces, manage quotas, check name availability
> - **Test Execution SDK (Azure.Developer.MicrosoftPlaywrightTesting.NUnit)**: Run Playwright tests at scale on cloud browsers
## Installation
```bash
dotnet add package Azure.ResourceManager.Playwright
dotnet add package Azure.Identity
```
**Current Versions**: Stable v1.0.0, Preview v1.0.0-beta.1
## Environment Variables
```bash
AZURE_SUBSCRIPTION_ID=<your-subscription-id> # Required: Azure subscription ID
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
AZURE_TENANT_ID=<tenant-id> # For service principal auth (optional)
AZURE_CLIENT_ID=<client-id> # For service principal auth (optional)
AZURE_CLIENT_SECRET=<client-secret> # For service principal auth (optional)
```
## Authentication
```csharp
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Playwright;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var armClient = new ArmClient(credential);
// Get subscription
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var subscription = armClient.GetSubscriptionResource(
new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
```
## Resource Hierarchy
```
ArmClient
└── SubscriptionResource
├── PlaywrightQuotaResource (subscription-level quotas)
└── ResourceGroupResource
└── PlaywrightWorkspaceResource
└── PlaywrightWorkspaceQuotaResource (workspace-level quotas)
```
## Core Workflow
### 1. Create Playwright Workspace
```csharp
using Azure.ResourceManager.Playwright;
using Azure.ResourceManager.Playwright.Models;
// Get resource group
var resourceGroup = await subscription
.GetResourceGroupAsync("my-resource-group");
// Define workspace
var workspaceData = new PlaywrightWorkspaceData(AzureLocation.WestUS3)
{
// Optional: Configure regional affinity and local auth
RegionalAffinity = PlaywrightRegionalAffinity.Enabled,
LocalAuth = PlaywrightLocalAuth.Enabled,
Tags =
{
["Team"] = "Dev Exp",
["Environment"] = "Production"
}
};
// Create workspace (long-running operation)
var workspaceCollection = resourceGroup.Value.GetPlaywrightWorkspaces();
var operation = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-playwright-workspace",
workspaceData);
PlaywrightWorkspaceResource workspace = operation.Value;
// Get the data plane URI for running tests
Console.WriteLine($"Data Plane URI: {workspace.Data.DataplaneUri}");
Console.WriteLine($"Workspace ID: {workspace.Data.WorkspaceId}");
```
### 2. Get Existing Workspace
```csharp
// Get by name
var workspace = await workspaceCollection.GetAsync("my-playwright-workspace");
// Or check if exists first
bool exists = await workspaceCollection.ExistsAsync("my-playwright-workspace");
if (exists)
{
var existingWorkspace = await workspaceCollection.GetAsync("my-playwright-workspace");
Console.WriteLine($"Workspace found: {existingWorkspace.Value.Data.Name}");
}
```
### 3. List Workspaces
```csharp
// List in resource group
await foreach (var workspace in workspaceCollection.GetAllAsync())
{
Console.WriteLine($"Workspace: {workspace.Data.Name}");
Console.WriteLine($" Location: {workspace.Data.Location}");
Console.WriteLine($" State: {workspace.Data.ProvisioningState}");
Console.WriteLine($" Data Plane URI: {workspace.Data.DataplaneUri}");
}
// List across subscription
await foreach (var workspace in subscription.GetPlaywrightWorkspacesAsync())
{
Console.WriteLine($"Workspace: {workspace.Data.Name}");
}
```
### 4. Update Workspace
```csharp
var patch = new PlaywrightWorkspacePatch
{
Tags =
{
["Team"] = "Dev Exp",
["Environment"] = "Staging",
["UpdatedAt"] = DateTime.UtcNow.ToString("o")
}
};
var updatedWorkspace = await workspace.Value.UpdateAsync(patch);
```
### 5. Check Name Availability
```csharp
using Azure.ResourceManager.Playwright.Models;
var checkRequest = new PlaywrightCheckNameAvailabilityContent
{
Name = "my-new-workspace",
ResourceType = "Microsoft.LoadTestService/playwrightWorkspaces"
};
var result = await subscription.CheckPlaywrightNameAvailabilityAsync(checkRequest);
if (result.Value.IsNameAvailable == true)
{
Console.WriteLine("Name is available!");
}
else
{
Console.WriteLine($"Name unavailable: {result.Value.Message}");
Console.WriteLine($"Reason: {result.Value.Reason}");
}
```
### 6. Get Quota Information
```csharp
// Subscription-level quotas
await foreach (var quota in subscription.GetPlaywrightQuotasAsync(AzureLocation.WestUS3))
{
Console.WriteLine($"Quota: {quota.Data.Name}");
Console.WriteLine($" Limit: {quota.Data.Limit}");
Console.WriteLine($" Used: {quota.Data.Used}");
}
// Workspace-level quotas
var workspaceQuotas = workspace.Value.GetAllPlaywrightWorkspaceQuota();
await foreach (var quota in workspaceQuotas.GetAllAsync())
{
Console.WriteLine($"Workspace Quota: {quota.Data.Name}");
}
```
### 7. Delete Workspace
```csharp
// Delete (long-running operation)
await workspace.Value.DeleteAsync(WaitUntil.Completed);
```
## Key Types Reference
| Type | Purpose |
|------|---------|
| `ArmClient` | Entry point for all ARM operations |
| `PlaywrightWorkspaceResource` | Represents a Playwright Testing workspace |
| `PlaywrightWorkspaceCollection` | Collection for workspace CRUD |
| `PlaywrightWorkspaceData` | Workspace creation/response payload |
| `PlaywrightWorkspacePatch` | Workspace update payload |
| `PlaywrightQuotaResource` | Subscription-level quota information |
| `PlaywrightWorkspaceQuotaResource` | Workspace-level quota information |
| `PlaywrightExtensions` | Extension methods for ARM resources |
| `PlaywrightCheckNameAvailabilityContent` | Name availability check request |
## Workspace Properties
| Property | Description |
|----------|-------------|
| `DataplaneUri` | URI for running tests (e.g., `https://api.dataplane.{guid}.domain.com`) |
| `WorkspaceId` | Unique workspace identifier (GUID) |
| `RegionalAffinity` | Enable/disable regional affinity for test execution |
| `LocalAuth` | Enable/disable local authentication (access tokens) |
| `ProvisioningState` | Current provisioning state (Succeeded, Failed, etc.) |
## Best Practices
1. **Use `WaitUntil.Completed`** for operations that must finish before proceeding
2. **Use `WaitUntil.Started`** when you want to poll manually or run operations in parallel
3. **Always use `DefaultAzureCredential`** — never hardcode keys
4. **Handle `RequestFailedException`** for ARM API errors
5. **Use `CreateOrUpdateAsync`** for idempotent operations
6. **Navigate hierarchy** via `Get*` methods (e.g., `resourceGroup.GetPlaywrightWorkspaces()`)
7. **Store the DataplaneUri** after workspace creation for test execution configuration
## Error Handling
```csharp
using Azure;
try
{
var operation = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed, workspaceName, workspaceData);
}
catch (RequestFailedException ex) when (ex.Status == 409)
{
Console.WriteLine("Workspace already exists");
}
catch (RequestFailedException ex) when (ex.Status == 400)
{
Console.WriteLine($"Bad request: {ex.Message}");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"ARM Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");
}
```
## Integration with Test Execution
After creating a workspace, use the `DataplaneUri` to configure your Playwright tests:
```csharp
// 1. Create workspace (this SDK)
var workspace = await workspaceCollection.CreateOrUpdateAsync(
WaitUntil.Completed, "my-workspace", workspaceData);
// 2. Get the service URL
var serviceUrl = workspace.Value.Data.DataplaneUri;
// 3. Set environment variable for test execution
Environment.SetEnvironmentVariable("PLAYWRIGHT_SERVICE_URL", serviceUrl.ToString());
// 4. Run tests using Azure.Developer.MicrosoftPlaywrightTesting.NUnit
// (separate package for test execution)
```
## Related SDKs
| SDK | Purpose | Install |
|-----|---------|---------|
| `Azure.ResourceManager.Playwright` | Management plane (this SDK) | `dotnet add package Azure.ResourceManager.Playwright` |
| `Azure.Developer.MicrosoftPlaywrightTesting.NUnit` | Run NUnit Playwright tests at scale | `dotnet add package Azure.Developer.MicrosoftPlaywrightTesting.NUnit --prerelease` |
| `Azure.Developer.Playwright` | Playwright client library | `dotnet add package Azure.Developer.Playwright` |
## API Information
- **Resource Provider**: `Microsoft.LoadTestService`
- **Default API Version**: `2025-09-01`
- **Resource Type**: `Microsoft.LoadTestService/playwrightWorkspaces`
## Documentation Links
- [Azure.ResourceManager.Playwright API Reference](https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.playwright)
- [Microsoft Playwright Testing Overview](https://learn.microsoft.com/en-us/azure/playwright-testing/overview-what-is-microsoft-playwright-testing)
- [Quickstart: Run Playwright Tests at Scale](https://learn.microsoft.com/en-us/azure/playwright-testing/quickstart-run-end-to-end-tests)
所有文件
0 个文件安装 azure-resource-manager-playwright-dotnet
将技能文件下载并解压至你的 .claude/skills/ 目录。
下载ZIP克隆仓库并复制技能文件到您的项目中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-dotnet/skills/azure-resource-manager-playwright-dotnet # Copy SKILL.md to your .claude/skills/ directory
复制





首页
