A Ruby port of Puppeteer
[!IMPORTANT]
- This library now requires Ruby 3.2 or later.
- Firefox support has been moved to puppeteer-bidi.
- For older versions, please refer to the ref-2025 tag.
A Ruby port of puppeteer.
REMARK: This Gem covers just a part of Puppeteer APIs. See API Coverage list for detail. Feedbacks and feature requests are welcome :)
Add this line to your application's Gemfile:
gem 'puppeteer-ruby'And then execute:
$ bundle installrequire 'puppeteer-ruby'
Puppeteer.launch(headless: false) do |browser|
page = browser.new_page
page.goto("https://github.com/YusukeIwaki")
page.screenshot(path: "YusukeIwaki.png")
endNOTE: require 'puppeteer-ruby' is not necessary in Rails.
…require 'puppeteer-ruby'
Puppeteer.launch do |browser|
page = browser.new_page
page.goto 'https://github.com/YusukeIwaki'
# Get the "viewport" of the page, as reported by the page.
dimensions = page.evaluate( {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
}
JAVASCRIPT
puts "dimensions: #{dimensions}"
# => dimensions: {"width"=>800, "height"=>600, "deviceScaleFactor"=>1}
endMore usage examples can be found here
Following packages are required.
google-chrome-stablechromiumAlso, CJK font will be required for Chinese, Japanese, Korean sites.
It is really remarkable that we can use puppeteer functions in existing Selenium or Capybara codes, with a few configuration in advance.
…The detailed step of configuration can be found here.
We can also use puppeteer-ruby as it is without Capybara DSL. When you want to just test a Rails application simply with Puppeteer, refer this section.
Also, if you have trouble with handling flaky/unstable testcases in existing feature/system specs, consider replacing Capybara::DSL with raw puppeteer-ruby codes like page.wait_for_selector(...) or page.wait_for_navigation { ... }.
Capybara prepares test server even when Capybara DSL is not used.
Sample configuration is shown below. You can use it by putting the file at spec/support/puppeteer_ruby.rb or another location where RSpec loads on initialization.
RSpec.configure do |config|
require 'capybara'
# This driver only requests Capybara to launch test server.
# Remark that no Capybara::DSL is available with this driver.
class CapybaraNullDriver el.textContent')
expect(h1_text).to eq('It works!')
end
endhttps://yusukeiwaki.github.io/puppeteer-ruby-docs/
This library supports Chrome/Chromium only. For Firefox automation, consider using playwright-ruby-client or puppeteer-bidi.
Bug reports and pull requests are welcome on GitHub at https://github.com/YusukeIwaki/puppeteer-ruby.
No open issues yet, or sync has not completed.