AutomateBrowserSelenium
First test in JavaScript
A complete Selenium test in Node.js 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
Node 18 or newer, and one package:
npm install selenium-webdriverNothing of ours to install. This is the ordinary Selenium library.
The whole test
// first-test.js
const { Builder, By } = require("selenium-webdriver");
const { randomUUID } = require("crypto");
const ACCESS_KEY = "xyz_your_access_key_here";
const HUB = `https://x:${ACCESS_KEY}@hub.testkase.com/wd/hub`;
const TEST_NAME = "example.com opens";
(async () => {
const driver = await new Builder()
.usingServer(HUB)
.withCapabilities({
browserName: "chrome",
browserVersion: "latest",
platformName: "windows 11",
"testkase:options": {
name: TEST_NAME,
build: "first-test",
runId: randomUUID(),
recording: true,
},
})
.build();
console.log("session", (await driver.getSession()).getId());
try {
await driver.get("https://example.com");
const title = await driver.getTitle();
if (!title.includes("Example Domain")) {
throw new Error(`wrong title: ${title}`);
}
const link = await driver.findElement(By.tagName("a"));
console.log(" link says:", await link.getText());
await driver.executeScript("testkase-status=passed");
console.log("PASS ", TEST_NAME);
} catch (err) {
await driver.executeScript("testkase-status=failed");
console.log("FAIL ", TEST_NAME, "->", err.message);
await driver.quit();
process.exit(1);
}
await 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 | browserName: "chrome" | Picks the browser. Three lines decide the machine: which browser, which version, which operating system. Leave the last two out for the newest Chrome on the newest macOS. |
| 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 | await driver.get("https://example.com") | Ordinary Selenium from here on. Every command behaves exactly as it does locally — we only carry it to the browser and back. |
| 5 | await 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 | await 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
node first-test.jsThe 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.
