Skip to content

Commit 246d3ed

Browse files
authored
Adding dontet library sample (#200)
1 parent 2b24b99 commit 246d3ed

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

samples/.NET/lib/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.Playwright;
2+
using System.Runtime.InteropServices;
3+
4+
// Function to get connection options
5+
static (string wsEndpoint, BrowserTypeConnectOptions options) GetConnectOptions()
6+
{
7+
var runId = Guid.NewGuid().ToString();
8+
var os = "linux"; // Change to "windows" if wants to run on Windows
9+
var apiVersion = "2023-10-01-preview";// DONOT change
10+
11+
var serviceUrl = Environment.GetEnvironmentVariable("PLAYWRIGHT_SERVICE_URL") ?? "";
12+
var accessToken = Environment.GetEnvironmentVariable("PLAYWRIGHT_SERVICE_ACCESS_TOKEN") ?? "";
13+
14+
var wsEndpoint = $"{serviceUrl}?runId={Uri.EscapeDataString(runId)}&os={os}&api-version={apiVersion}";
15+
16+
var options = new BrowserTypeConnectOptions
17+
{
18+
Timeout = 3 * 60 * 1000, // 3 minutes
19+
Headers = new Dictionary<string, string>
20+
{
21+
{ "Authorization", $"Bearer {accessToken}" }
22+
},
23+
ExposeNetwork = "<loopback>", // Use loopback to expose network
24+
};
25+
26+
return (wsEndpoint, options);
27+
}
28+
29+
using var playwright = await Playwright.CreateAsync();
30+
31+
// Get connection options
32+
var (wsEndpoint, connectOptions) = GetConnectOptions();
33+
34+
await using var browser = await playwright.Chromium.ConnectAsync(wsEndpoint, connectOptions);
35+
var page = await browser.NewPageAsync();
36+
await page.GotoAsync("https://playwright.dev/dotnet");
37+
var title = await page.TitleAsync();
38+
Console.WriteLine($"Title: {title}");
39+
await page.ScreenshotAsync(new()
40+
{
41+
Path = "screenshot.png"
42+
});

samples/.NET/lib/ReadMe.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Run Playwright .NET tests with Microsoft Playwright Testing
2+
3+
This sample demonstrates how to run Playwright .NET to use browser 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 dotnet library without Any runner.
4+
5+
If you have not yet created a workspace, please follow the [Get Started guide](../../../README.md#get-started)
6+
7+
### Sample setup
8+
1. Clone this sample:
9+
```powershell
10+
git clone https://github.com/microsoft/playwright-testing-service
11+
cd playwright-workspaces/samples/.NET/lib
12+
```
13+
14+
1. Install dependencies:
15+
```powershell
16+
dotnet add package Microsoft.Playwright.NUnit
17+
```
18+
19+
1. Build the project so the playwright.ps1 is available inside the bin directory:
20+
```powershell
21+
dotnet build
22+
```
23+
24+
1. Install required browsers by replacing netX with the actual output folder name, e.g. net6.0:
25+
26+
```powershell
27+
pwsh bin/Debug/netX/playwright.ps1 install
28+
```
29+
30+
If pwsh is not available, you have to [install PowerShell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell).
31+
32+
33+
1. Set up environment:
34+
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:
35+
36+
```bash
37+
PLAYWRIGHT_SERVICE_URL= # Paste region endpoint URL
38+
```
39+
- Generate access token following [guide](https://aka.ms/mpt/generate-access-token)
40+
- Set the token generated in the previous step
41+
```
42+
$env:PLAYWRIGHT_SERVICE_ACCESS_TOKEN="TOKEN_VALUE"
43+
```
44+
45+
### Run program
46+
47+
Run Playwright script against browsers managed by the service using the configuration you created above. You can run up to 50 parallel workers with the service
48+
```powershell
49+
dotnet run
50+
```
51+
52+
53+
## Next steps
54+
1. Follow the [quickstart guide](https://learn.microsoft.com/en-us/azure/playwright-testing/quickstart-run-end-to-end-tests?tabs=playwrightcli&pivots=nunit-test-runner)
55+
2. [Integrate CI/CD pipelines](https://learn.microsoft.com/en-us/azure/playwright-testing/quickstart-automate-end-to-end-testing?tabs=github&pivots=nunit-test-runner)
56+
3. Learn about [package options](https://learn.microsoft.com/en-us/azure/playwright-testing/how-to-use-service-config-file?pivots=nunit-test-runner).

samples/.NET/lib/lib.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Playwright" Version="1.54.0" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)