TestKase
TestKase
|Docs
AutomateBrowserSelenium

First test in Python

A complete Selenium test in Python 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

Python 3.8 or newer, and one package:

pip install selenium

Nothing of ours to install. This is the ordinary Selenium library.

The whole test

# first_test.py
import sys
import uuid

from selenium import webdriver
from selenium.webdriver.common.by import By

ACCESS_KEY = "xyz_your_access_key_here"
HUB = f"https://x:{ACCESS_KEY}@hub.testkase.com/wd/hub"
TEST_NAME = "example.com opens"

options = webdriver.ChromeOptions()
options.browser_version = "latest"
options.platform_name = "windows 11"
options.set_capability(
    "testkase:options",
    {
        "name": TEST_NAME,
        "build": "first-test",
        "runId": str(uuid.uuid4()),
        "recording": True,
    },
)

driver = webdriver.Remote(command_executor=HUB, options=options)
print("session", driver.session_id)

try:
    driver.get("https://example.com")

    title = driver.title
    assert "Example Domain" in title, f"wrong title: {title}"

    link = driver.find_element(By.TAG_NAME, "a")
    print("  link says:", link.text)

    driver.execute_script("testkase-status=passed")
    print("PASS ", TEST_NAME)

except Exception as err:
    driver.execute_script("testkase-status=failed")
    print("FAIL ", TEST_NAME, "->", err)
    driver.quit()
    sys.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 lineWhat it does
1https://x:[email protected]/wd/hubWhere 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.
2webdriver.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": TrueSays 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.
4driver.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.
5driver.execute_script("testkase-status=passed")How we tell a pass from a crash. Without it the session shows no result. The failed one belongs in the except, because a test that raises skips every line after the raise.
6driver.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

python first_test.py

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