TestKase
TestKase
|Docs
AutomateBrowserSelenium

First test in Ruby

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

Ruby 3.0 or newer, and one gem:

gem install selenium-webdriver

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

The whole test

# first_test.rb
require "selenium-webdriver"
require "securerandom"

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

options = Selenium::WebDriver::Options.chrome
options.browser_version = "latest"
options.platform_name = "windows 11"
options.add_option("testkase:options", {
  "name" => TEST_NAME,
  "build" => "first-test",
  "runId" => SecureRandom.uuid,
  "recording" => true,
})

driver = Selenium::WebDriver.for(:remote, url: HUB, capabilities: options)
puts "session #{driver.session_id}"

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

  title = driver.title
  raise "wrong title: #{title}" unless title.include?("Example Domain")

  link = driver.find_element(tag_name: "a")
  puts "  link says: #{link.text}"

  driver.execute_script("testkase-status=passed")
  puts "PASS  #{TEST_NAME}"
rescue => err
  driver.execute_script("testkase-status=failed")
  puts "FAIL  #{TEST_NAME} -> #{err.message}"
  driver.quit
  exit 1
end

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.
2Selenium::WebDriver::Options.chromePicks 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, .edge 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.
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 rescue, because a test that raises skips every line after the raise.
6driver.quitEnds the session and frees the machine. Forget it and the session runs until a timeout stops it — which you are paying for.

Run it

ruby first_test.rb

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.