Download and launch TestingBot Tunnel from your Ruby tests, so browsers and devices on TestingBot can reach websites on your machine, in CI or on your private network.
The gem downloads the tunnel the first time, starts it, waits until it is ready and stops it again when your tests are done. When Java 17 or newer is not installed, it uses the TestingBot Tunnel download with Java included, so nothing else has to be installed. It has no dependencies outside the Ruby standard library.
Requires Ruby 3.0 or newer.
# Gemfile
group :test do
gem "testingbot-tunnel"
endrequire "testingbot/tunnel"
tunnel = TestingBot::Tunnel.new(tunnel_identifier: "my-build-42")
tunnel.start # returns once the tunnel is ready
# ... run your tests, with "tb:options" => { "tunnel-identifier" => "my-build-42" }
tunnel.stop # returns once the tunnel process has exitedOr with a block, which stops the tunnel however the block ends and returns its value:
TestingBot::Tunnel.run(tunnel_identifier: "my-build-42") do |tunnel|
# ... run your tests
end| Method | |
|---|---|
#start |
Download the tunnel when needed, start it and wait until it is ready. Returns the tunnel. |
#stop |
Stop the tunnel and wait until its process has exited. Safe to call more than once. |
#running? |
Whether the tunnel is ready and its process still runs. |
#pid, #exit_status |
The tunnel process. |
#version, #java, #jar |
What is running, e.g. "5.2" with "java". |
TestingBot::Tunnel.run(**options) { |tunnel| } |
Start, yield, stop. |
TestingBot::Tunnel::Credentials.resolve |
The key and secret the tunnel uses, to build your capabilities with. |
new raises an ArgumentError for options that are not valid. start raises a TestingBot::Tunnel::Error when the tunnel can not be downloaded or started. The message says why, in the words of TestingBot where it gave a reason, for example:
Invalid credentials. Please supply the correct key/secret obtained from TestingBot.comYou already have 2 tunnels active - please close another tunnel firstTunnel failed to launch in 90 seconds.Could not start TestingBot Tunnel. Exit code 1 signal: nilfollowed by the last lines the tunnel wrote
Start one tunnel for the whole run, and point Capybara's remote browser at it with tunnel-identifier:
# spec/support/testingbot.rb
require "securerandom"
require "testingbot/tunnel"
require "capybara/rspec"
require "selenium-webdriver"
TUNNEL_IDENTIFIER = "rspec-#{SecureRandom.hex(6)}"
Capybara.register_driver :testingbot do |app|
credentials = TestingBot::Tunnel::Credentials.resolve
options = Selenium::WebDriver::Options.chrome(browser_version: "latest", platform_name: "WIN11")
options.add_option("tb:options", {
"key" => credentials.key,
"secret" => credentials.secret,
"tunnel-identifier" => TUNNEL_IDENTIFIER,
"name" => "My app"
})
Capybara::Selenium::Driver.new(app, browser: :remote, url: "https://hub.testingbot.com/wd/hub", options: options)
end
Capybara.default_driver = :testingbot
Capybara.javascript_driver = :testingbot
# Serve the app on a fixed port, on a host name the remote browser sends through the tunnel
Capybara.server = :puma, { Silent: true }
Capybara.server_host = "127.0.0.1"
Capybara.server_port = 3000
Capybara.app_host = "http://local.testingbot.com:3000"
RSpec.configure do |config|
tunnel = TestingBot::Tunnel.new(tunnel_identifier: TUNNEL_IDENTIFIER)
config.before(:suite) { tunnel.start }
config.after(:suite) { tunnel.stop }
end# spec/features/home_page_spec.rb
RSpec.describe "The home page", type: :feature do
it "shows the title" do
visit "/"
expect(page).to have_title("My App")
end
endUse http://local.testingbot.com:<port> rather than localhost: it resolves to 127.0.0.1, and browsers refuse to send localhost through a proxy. See Localhost.
RSpec.describe "Checkout" do
around do |example|
TestingBot::Tunnel.run(tunnel_identifier: "checkout") { example.run }
end
it "loads" do
credentials = TestingBot::Tunnel::Credentials.resolve
options = Selenium::WebDriver::Options.firefox(browser_version: "latest", platform_name: "WIN11")
options.add_option("tb:options", { "key" => credentials.key, "secret" => credentials.secret,
"tunnel-identifier" => "checkout" })
driver = Selenium::WebDriver.for(:remote, url: "https://hub.testingbot.com/wd/hub", options: options)
driver.navigate.to("http://local.testingbot.com:3000/checkout")
expect(driver.title).to eq("Checkout")
ensure
driver&.quit
end
endThe TestingBot key and secret are looked up in this order:
api_key:andapi_secret:- the
TB_KEYandTB_SECRETenvironment variables - the
TESTINGBOT_KEYandTESTINGBOT_SECRETenvironment variables ~/.testingbot, holdingkey:secret
They are handed to the tunnel through the TESTINGBOT_KEY/TESTINGBOT_SECRET environment variables of its process, never on its command line, so they do not show up in the process list. They are redacted from everything the gem logs, and Credentials#inspect does not show the secret.
TestingBot::Tunnel.new(
# Launcher options
api_key: "key",
api_secret: "secret",
tunnel_version: "5.2", # the latest when not given
timeout: 120, # seconds to wait for the tunnel to be ready, default 90
java_runtime: :auto, # :auto, :system or :bundled, see below
verbose: true, # log the tunnel output
log: Logger.new($stderr), # an IO, a Logger or anything that responds to call; default $stdout, nil for silence
# Tunnel options, see https://testingbot.com/support/tunnel/tunnelcommand
tunnel_identifier: "my-build",
shared: true,
se_port: 4445,
proxy: "proxy.example.com:3128",
proxy_userpwd: "user:pwd",
fast_fail_regexps: '.*\.google-analytics\.com',
no_bump: true,
no_cache: true,
logfile: "tunnel.log"
)Every option of TestingBot Tunnel can be given, named after the command line option: dns, auth, pac, hub_port, local_proxy, no_proxy, web, extra_headers, debug, metrics_port and metrics_auth.
Tunnel 5.0 added allow_hosts, bind_address, cacert_file, config, connect_to, dns_round_robin, dns_timeout, header, response_header, http_dial_timeout, http_idle_timeout, krb5_hosts, krb5_keytab, krb5_principal, localhost_policy, log_format, log_http, log_level, request_id_header, no_bump_domains, pac_local, pac_local_sha256, proxy_auth_scheme, proxy_spn, proxy_testingbot, proxy_testingbot_userpwd, ssh_host_key, ssh_host_key_policy and ws_proxy_mode; tunnel 5.1 added ssh_connections (1 to 8). true passes a flag, a list passes the option once for every entry (header: ["X-Test: 1", "-Cookie"]). Using one of them with an older tunnel_version fails before the tunnel is started, with a message that says which version it needs.
An option the gem does not know raises an ArgumentError, so a typo does not go unnoticed. extra_args: ["--new-option", "value"] passes arguments to the tunnel as they are, for options added to the tunnel after this gem was released.
TestingBot Tunnel is a Java program: 5.0 and up need Java 17 or newer, older versions Java 11.
TestingBot also publishes the tunnel with Java included, for Linux and macOS (x64 and arm64) and Windows (x64). java_runtime picks the Java:
:auto(default): thejavaon thePATHwhen it is recent enough for the tunnel, the download with Java included otherwise:system: always thejavaon thePATH:bundled: always the download with Java included
The download with Java included (about 30 MB, once per version) is checked against the SHA-256 checksum TestingBot publishes next to it, and is not used when it does not match. Set TESTINGBOT_TUNNEL_RELEASES_URL to download it from a mirror of the TestingBot Tunnel releases, laid out the same way.
All downloads are made over https; a redirect to plain http is refused.
In the cache directory of the user: ~/Library/Caches/testingbot-tunnel-launcher on macOS, %LOCALAPPDATA%\testingbot-tunnel-launcher on Windows and ~/.cache/testingbot-tunnel-launcher (or $XDG_CACHE_HOME) elsewhere. This is the same directory the Node.js launcher uses, so they share their downloads. Set TESTINGBOT_TUNNEL_CACHE_DIR to store it somewhere else, for example in a directory your CI caches.
stop asks the tunnel to stop the way Ctrl+C does (SIGINT), so it can close its connection to TestingBot, and kills it when it has not stopped after 10 seconds. On Windows the tunnel process is ended straight away. A tunnel that is still running when your Ruby process exits is stopped as well.
Several tunnels can run next to each other: give each its own tunnel_identifier. TestingBot::Tunnel.active lists the ones that are running.
bundle install
bundle exec rake # specs and rubocopThe specs use a stand-in for java and stub the TestingBot downloads, they need no network or account.
examples/live_check.rb is a real end-to-end check: it starts a tunnel with your credentials, serves a page on this machine, opens it in a remote Chrome on TestingBot through the tunnel and checks its title.
bundle exec ruby -Ilib examples/live_check.rb # the installed Java
bundle exec ruby -Ilib examples/live_check.rb --bundled # the download with Java includedMIT, see LICENSE. Copyright (c) TestingBot <[email protected]>