Playwright & Ruby
The playwright-ruby-client gem provides a Ruby interface for Playwright, enabling browser automation for testing and web scraping.
See the example below on how to use Ruby with Playwright to run tests on the TestingBot browser grid.
More information about Playwright for Ruby can be found on the playwright-ruby-client documentation.
Installation
To get started, add the playwright-ruby-client gem to your Gemfile:
gem 'playwright-ruby-client'
Then run:
bundle install
Or install it directly:
gem install playwright-ruby-client
Running a Test
Below is an example of how to connect to the TestingBot Playwright grid using the playwright-ruby-client gem:
require 'playwright'
require 'json'
require 'uri'
capabilities = {
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => 'chrome',
'browserVersion' => 'latest',
'platform' => 'WIN10'
}
ws_endpoint = "wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
page.goto('https://testingbot.com/')
puts page.title
page.screenshot(path: 'screenshot.png')
browser.close
end
require 'playwright'
require 'json'
require 'uri'
capabilities = {
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => 'firefox',
'browserVersion' => 'latest',
'platform' => 'WIN10'
}
ws_endpoint = "wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
page.goto('https://testingbot.com/')
puts page.title
page.screenshot(path: 'screenshot.png')
browser.close
end
require 'playwright'
require 'json'
require 'uri'
capabilities = {
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => 'safari',
'browserVersion' => 'latest',
'platform' => 'VENTURA'
}
ws_endpoint = "wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
page.goto('https://testingbot.com/')
puts page.title
page.screenshot(path: 'screenshot.png')
browser.close
end
This example will use Playwright to connect to a browser in the TestingBot cloud.
It will open the TestingBot website, print the title of the page and save a PNG screenshot.
Using with RSpec
You can integrate Playwright with RSpec for running your tests. Here's an example:
require 'playwright'
require 'json'
require 'uri'
RSpec.describe 'TestingBot Playwright Test' do
let(:capabilities) do
{
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => 'chrome',
'browserVersion' => 'latest',
'platform' => 'WIN10'
}
end
let(:ws_endpoint) do
"wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
end
it 'verifies the page title' do
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
page.goto('https://testingbot.com/')
expect(page.title).to include('TestingBot')
browser.close
end
end
end
Parallel Testing with Ruby
One of the great advantages of the TestingBot service is that you can run multiple tests simultaneously.
This drastically shortens the total duration of your test suite, as multiple tests will run concurrently.
You can use the parallel_tests gem to run your RSpec tests in parallel:
gem install parallel_tests
Then run your tests with:
parallel_rspec spec/
Here's an example of running tests across multiple browsers in parallel using threads:
require 'playwright'
require 'json'
require 'uri'
browsers = [
{ name: 'chrome', type: :chromium, platform: 'WIN10' },
{ name: 'firefox', type: :firefox, platform: 'WIN10' },
{ name: 'safari', type: :webkit, platform: 'VENTURA' }
]
threads = browsers.map do |browser_config|
Thread.new do
capabilities = {
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => browser_config[:name],
'browserVersion' => 'latest',
'platform' => browser_config[:platform]
}
ws_endpoint = "wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
page.goto('https://testingbot.com/')
puts "#{browser_config[:name]}: #{page.title}"
page.screenshot(path: "screenshot-#{browser_config[:name]}.png")
browser.close
end
end
end
threads.each(&:join)
This example runs tests on Chrome, Firefox, and Safari simultaneously, taking screenshots on each browser.
Mark Test Status
You can mark your test as passed or failed in the TestingBot dashboard by using the testingbot_executor command:
require 'playwright'
require 'json'
require 'uri'
capabilities = {
'tb:options' => {
'key' => ENV['TB_KEY'],
'secret' => ENV['TB_SECRET']
},
'browserName' => 'chrome',
'browserVersion' => 'latest',
'platform' => 'WIN10'
}
ws_endpoint = "wss://cloud.testingbot.com/playwright?capabilities=#{URI.encode_www_form_component(capabilities.to_json)}"
Playwright.connect_to_browser_server(ws_endpoint) do |browser|
page = browser.new_page
begin
page.goto('https://testingbot.com/')
if page.title.include?('TestingBot')
# Mark test as passed
page.evaluate("testingbot_executor: #{JSON.generate({ action: 'setSessionStatus', arguments: { passed: true, reason: 'Title matched' } })}")
else
raise 'Title did not match'
end
rescue => e
# Mark test as failed
page.evaluate("testingbot_executor: #{JSON.generate({ action: 'setSessionStatus', arguments: { passed: false, reason: e.message } })}")
ensure
browser.close
end
end