For AI agents and LLMs: a machine-readable index is available at llms.txt. A plain-Markdown version of any documentation page is available by appending .md to its URL.
Skip to main content

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.

  1. A TestMu AI Username and Access Key. Get them from your TestMu AI Profile. Don't have an account? Sign up for free.

  2. The .NET SDK installed. The sample project uses the built-in Playwright tooling for .NET, so dotnet restore resolves the dependencies when you build.

  3. Clone the sample repository and move into the C# subdirectory.

Sample repo
Verified
git clone https://github.com/LambdaTest/playwright-sample.git
cd playwright-sample
cd playwright-csharp
  1. Restore the .NET dependencies from the project directory:
Verified
dotnet restore

Set Your Credentials​


Your Username and Access Key are read from environment variables. Set them once. Pick your operating system:

Verified
export LT_USERNAME="undefined"
export 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:

Verified
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.

  1. Build the project:
Verified
dotnet build
  1. Run the single test:
Verified
dotnet run single
note

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.


Continue with these related guides:

Terminal First Testing With Kane CLI

Natural language browser & mobile app tests right from terminal.

×
Schedule Your Personal Demo
Kane CLI terminal

Help and Support

Related Articles