Using 🎭Playwright in Ruby/Rails

yusukeiwaki

Yusuke Iwaki

Posted on August 10, 2021

Using 🎭Playwright in Ruby/Rails

🎭Playwright is a really awesome library for browser automation. Microsoft already published several language bindings.

And also playwright-go is planned to be official.
https://github.com/microsoft/playwright/issues/6856

No Ruby client?

Ruby or Rails developers would like to use Playwright for acceptance testing or other use-cases, but it is not available at this moment.

Playwright team explicitly says there is no plan to release Ruby client.
https://github.com/microsoft/playwright/issues/5874

Ruby users should be happy with Playwright!!

It is clear that Ruby is not so famous as Python or TypeScript, but many Rails users still use it.

Rails already included an acceptance testing framework (called SystemTestCase). It uses Capybara and most users already noticed Capybara tends to produce flaky testcases.

Playwright provides very clever DOM selectors with auto-waiting feature, and useful utility functions. As Internet Explorer is already dead, no reason to keep using Selenium and Capybara.

Fortunately, Playwright is server-client architecture. We can use the useful features on any languages by just implementing a Playwright API client.

image

Try it on Ruby!!

I actually created a Playwright client for Ruby.
https://github.com/YusukeIwaki/playwright-ruby-client



Playwright.connect_to_playwright_server('ws://127.0.0.1:8080') do |playwright|
  playwright.chromium.launch do |browser|
    page = browser.new_page
    page.goto('https://github.com/YusukeIwaki')
    page.screenshot(path: './YusukeIwaki.png')
  end
end


Enter fullscreen mode Exit fullscreen mode

Note that we have to launch Playwright server in advance, like this: npx playwright install && npx playwright run-server 8080

Try it on Rails!!

I also created a Capybara driver of Playwright.
https://github.com/YusukeIwaki/capybara-playwright-driver



Capybara.register_driver(:playwright) do |app|
  Capybara::Playwright::Driver.new(app, browser_type: :firefox, headless: false)
end
Capybara.default_max_wait_time = 15
Capybara.default_driver = :playwright


Enter fullscreen mode Exit fullscreen mode

Capybara DSL is supported, and we can also use Playwright-native features such as recording video during testing.
https://playwright-ruby-client.vercel.app/docs/article/guides/rails_integration#available-functions-and-limitations

Conclusion

Playwright's architecture is great, and we Ruby/Rails users can use Playwright features by developing our own Playwright client API library.

Unfortunately Microsoft doesn't make effort for Ruby users, and difficult to ask for official support for Ruby.

💖 💪 🙅 🚩
yusukeiwaki
Yusuke Iwaki

Posted on August 10, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related