AutomateBrowserSelenium
First test in C#
A complete Selenium 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 6 or newer, and one package:
dotnet add package Selenium.WebDriverNothing of ours to install. This is the ordinary Selenium library.
The whole test
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
class FirstTest
{
const string AccessKey = "xyz_your_access_key_here";
const string TestName = "example.com opens";
static void Main()
{
var hub = new Uri($"https://x:{AccessKey}@hub.testkase.com/wd/hub");
var options = new ChromeOptions();
options.BrowserVersion = "latest";
options.PlatformName = "windows 11";
options.AddAdditionalOption("testkase:options", new Dictionary<string, object>
{
["name"] = TestName,
["build"] = "first-test",
["runId"] = Guid.NewGuid().ToString(),
["recording"] = true,
});
var driver = new RemoteWebDriver(hub, options);
Console.WriteLine($"session {driver.SessionId}");
try
{
driver.Navigate().GoToUrl("https://example.com");
var title = driver.Title;
if (!title.Contains("Example Domain"))
throw new Exception($"wrong title: {title}");
var link = driver.FindElement(By.TagName("a"));
Console.WriteLine($" link says: {link.Text}");
driver.ExecuteScript("testkase-status=passed");
Console.WriteLine($"PASS {TestName}");
}
catch (Exception err)
{
driver.ExecuteScript("testkase-status=failed");
Console.WriteLine($"FAIL {TestName} -> {err.Message}");
driver.Quit();
Environment.Exit(1);
}
driver.Quit();
}
}What each part does
Six lines carry the whole thing. Read down the middle column and you have the shape of the file.
| The line | What it does | |
|---|---|---|
| 1 | https://x:[email protected]/wd/hub | Where your key goes. This address is the only difference between running on your own machine and running on ours. The x is a placeholder for a username we do not check. |
| 2 | new ChromeOptions() | Picks the browser. It already says Chrome, so you only add the version and the operating system — or leave those out for the newest Chrome on the newest macOS. FirefoxOptions, EdgeOptions and SafariOptions work the same way. |
| 3 | ["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. |
| 4 | driver.Navigate().GoToUrl(...) | Ordinary Selenium from here on. Every command behaves exactly as it does locally — we only carry it to the browser and back. |
| 5 | driver.ExecuteScript("testkase-status=passed") | How we tell a pass from a crash. Without it the session shows no result. The failed one belongs in the catch, because a test that throws skips every line after the throw. |
| 6 | driver.Quit() | 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 console prints a session id, then PASS. Open your dashboard while it is running and you can watch it live; when it ends, the video is on the session.
