TestKase
TestKase
|Docs
AutomateBrowserSelenium

First test in PHP

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

PHP 7.4 or newer, and one package:

composer require php-webdriver/webdriver

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

The whole test

<?php
// first_test.php
require "vendor/autoload.php";

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

$accessKey = "xyz_your_access_key_here";
$hub = "https://x:{$accessKey}@hub.testkase.com/wd/hub";
$testName = "example.com opens";

$caps = DesiredCapabilities::chrome();
$caps->setCapability("browserVersion", "latest");
$caps->setCapability("platformName", "windows 11");
$caps->setCapability("testkase:options", [
    "name" => $testName,
    "build" => "first-test",
    "runId" => bin2hex(random_bytes(8)),
    "recording" => true,
]);

$driver = RemoteWebDriver::create($hub, $caps);
echo "session " . $driver->getSessionID() . PHP_EOL;

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

    $title = $driver->getTitle();
    if (strpos($title, "Example Domain") === false) {
        throw new Exception("wrong title: {$title}");
    }

    $link = $driver->findElement(WebDriverBy::tagName("a"));
    echo "  link says: " . $link->getText() . PHP_EOL;

    $driver->executeScript("testkase-status=passed");
    echo "PASS  {$testName}" . PHP_EOL;
} catch (Throwable $err) {
    $driver->executeScript("testkase-status=failed");
    echo "FAIL  {$testName} -> " . $err->getMessage() . PHP_EOL;
    $driver->quit();
    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.
2DesiredCapabilities::chrome()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. ::firefox() and ::safari() 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.
4$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$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

php first_test.php

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.