TestKase
TestKase
|Docs
AutomateBrowserSelenium

First test in Java

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

Java 11 or newer, and one dependency:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.27.0</version>
</dependency>

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

The whole test

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FirstTest {

    static final String ACCESS_KEY = "xyz_your_access_key_here";
    static final String HUB = "https://x:" + ACCESS_KEY + "@hub.testkase.com/wd/hub";
    static final String TEST_NAME = "example.com opens";

    public static void main(String[] args) throws Exception {

        ChromeOptions options = new ChromeOptions();
        options.setBrowserVersion("latest");
        options.setPlatformName("windows 11");

        Map<String, Object> tk = new HashMap<>();
        tk.put("name", TEST_NAME);
        tk.put("build", "first-test");
        tk.put("runId", UUID.randomUUID().toString());
        tk.put("recording", true);
        options.setCapability("testkase:options", tk);

        RemoteWebDriver driver = new RemoteWebDriver(new URL(HUB), options);
        System.out.println("session " + driver.getSessionId());

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

            String title = driver.getTitle();
            if (!title.contains("Example Domain")) {
                throw new AssertionError("wrong title: " + title);
            }

            WebElement link = driver.findElement(By.tagName("a"));
            System.out.println("  link says: " + link.getText());

            driver.executeScript("testkase-status=passed");
            System.out.println("PASS  " + TEST_NAME);

        } catch (Throwable t) {
            driver.executeScript("testkase-status=failed");
            System.out.println("FAIL  " + TEST_NAME + " -> " + t.getMessage());
            driver.quit();
            System.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.
2new 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.
3tk.put("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.
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.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 blows up skips every line after the blow-up.
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

mvn -q compile exec:java -Dexec.mainClass=FirstTest

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.