AutomateBrowserPlaywright
First test in .NET
A complete Playwright test in C# you can copy, run, and watch on a real browser.
One file. Copy it, put your access key in, run it, and watch it happen in your dashboard.
What you need
.NET 8 or newer, and one package:
dotnet add package Microsoft.PlaywrightNothing of ours to install. This is the ordinary Playwright library.
The whole test
using System;
using System.Text.Json;
using System.Web;
using Microsoft.Playwright;
class FirstTest
{
const string AccessKey = "xyz_your_access_key_here";
const string TestName = "example.com opens";
static async Task Main()
{
var capabilities = JsonSerializer.Serialize(new
{
browserName = "chrome",
browserVersion = "latest",
testkaseOptions = new
{
accessKey = AccessKey,
platformName = "windows 11",
name = TestName,
build = "first-test",
runId = Guid.NewGuid().ToString(),
recording = true,
},
}).Replace("testkaseOptions", "testkase:options");
var ws = $"wss://hub.testkase.com/playwright?capabilities={HttpUtility.UrlEncode(capabilities)}";
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.ConnectAsync(ws);
var page = await browser.NewPageAsync();
await page.GotoAsync("https://example.com");
var title = await page.TitleAsync();
if (!title.Contains("Example Domain"))
throw new Exception($"wrong title: {title}");
Console.WriteLine($" title: {title}");
await browser.CloseAsync();
Console.WriteLine($"PASS {TestName}");
}
}testkase:options is not a valid C# identifier, so the object is built with a placeholder name and renamed in the serialized JSON. A Dictionary<string, object> works just as well if you prefer.
What each part does
Six things carry the whole file. Read down the middle column and you have its shape.
| The part | What it does | |
|---|---|---|
| 1 | wss://hub.testkase.com/playwright?capabilities=… | Where everything goes. Playwright has no capability argument, so the whole object is URL-encoded onto the connect address. |
| 2 | accessKey | Signs you in. Copy it from the Access key page. There is no username here — the key is the only credential. |
| 3 | platformName | Which operating system. Set it. Leave it out and Chrome, Edge and Firefox fall back to linux, which no machine of ours runs, so the session is refused. |
| 4 | recording: true | Says what to keep. name is how you will recognise the session in a list; recording is the video, and you get none unless you ask. Every option. |
| 5 | playwright.Chromium.ConnectAsync(ws) | Ordinary Playwright from here on. Every command behaves exactly as it does locally — we only carry it to the browser and back. You call chromium.connect whichever brand you asked for; the address decides what you get. |
| 6 | browser.CloseAsync() | Ends the session and frees the machine. Forget it and the session runs until a timeout stops it — which you are paying for. |
Run it
dotnet runThe browser opens on our machines, not yours. Open your dashboard while it runs and the session is there live; when it ends, the video is on it.
