How to Run Playwright Tests With C# on TestMu AI
Run your C# Playwright tests on the TestMu AI cloud grid to reach 50+ real desktop browsers and operating systems without maintaining local infrastructure. You connect Playwright over a CDP WebSocket, pass your capabilities, then run the sample with dotnet.
Prerequisites
Complete these before running the test below.
-
A TestMu AI Username and Access Key. Get them from your TestMu AI Profile. Don't have an account? Sign up for free.
-
The .NET SDK installed. The sample project uses the built-in Playwright tooling for .NET, so
dotnet restoreresolves the dependencies when you build. -
Clone the sample repository and move into the C# subdirectory.
git clone https://github.com/LambdaTest/playwright-sample.git
cd playwright-sample
cd playwright-csharp
- Restore the .NET dependencies from the project directory:
dotnet restore
Set Your Credentials
Your Username and Access Key are read from environment variables. Set them once. Pick your operating system:
- macOS / Linux
- Windows (CMD)
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"
set LT_USERNAME=undefined
set LT_ACCESS_KEY=undefined
How the Sample Test Works
The test builds a capabilities dictionary that carries the browser, version, platform, and your LT:Options, then connects Playwright to the grid at wss://cdp.lambdatest.com/playwright. The credentials come from the LT_USERNAME and LT_ACCESS_KEY environment variables you set above. The full sample lives in PlaywrightTestSingle.cs in the playwright-csharp directory:
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
class PlaywrightTestSingle
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();
string user, accessKey;
user = Environment.GetEnvironmentVariable("LT_USERNAME");
accessKey = Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
Dictionary<string, object> capabilities = new Dictionary<string, object>();
Dictionary<string, string> ltOptions = new Dictionary<string, string>();
ltOptions.Add("name", "Playwright Test");
ltOptions.Add("build", "Playwright C-Sharp tests");
ltOptions.Add("platform", "Windows 10");
ltOptions.Add("user", user);
ltOptions.Add("accessKey", accessKey);
capabilities.Add("browserName", "Chrome");
capabilities.Add("browserVersion", "latest");
capabilities.Add("LT:Options", ltOptions);
string capabilitiesJson = JsonConvert.SerializeObject(capabilities);
string cdpUrl = "wss://cdp.lambdatest.com/playwright?capabilities=" + Uri.EscapeDataString(capabilitiesJson);
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var page = await browser.NewPageAsync();
try {
await page.GotoAsync("https://www.bing.com");
await page.Locator("[aria-label='Enter your search term']").ClickAsync();
await page.FillAsync("[aria-label='Enter your search term']", "LambdaTest");
await page.Keyboard.PressAsync("Enter");
var title = await page.TitleAsync();
if (title.Contains("LambdaTest"))
{
// Use the following code to mark the test status.
await SetTestStatus("passed", "Title matched", page);
}
else {
await SetTestStatus("failed", "Title not matched", page);
}
}
catch (Exception err) {
await SetTestStatus("failed", err.Message, page);
}
await browser.CloseAsync();
}
public static async Task SetTestStatus(string status, string remark, IPage page) {
await page.EvaluateAsync("_ => {}", "lambdatest_action: {\"action\": \"setTestStatus\", \"arguments\": {\"status\":\"" + status + "\", \"remark\": \"" + remark + "\"}}");
}
}
The SetTestStatus helper reports pass or fail back to the grid so the result shows on your dashboard.
Run the Test With dotnet
Build and run the sample from the playwright-csharp directory. You can use your own project or the sample above.
- Build the project:
dotnet build
- Run the single test:
dotnet run single
To run sessions on custom devices with emulation, run dotnet run iphonetest or dotnet run ipadtest.
View Your Results
Your test results, including video, network logs, and command-by-command execution, appear on the TestMu AI Web Automation Dashboard. A green status confirms the test passed.
Related Playwright Guides
Continue with these related guides:
