DEV Community

Yusuke Iwaki
Yusuke Iwaki

Posted on

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.

Top comments (2)

Collapse
 
vandriichuk profile image
Viktor Andriichuk

Hi, Yusuke Iwaki. I try to imlement this your solution to my python code. And I have a trouble: flask_main | return function()
flask_main | File "/usr/src/app/app.py", line 121, in getting_cookie_from_tmone
flask_main | with sync_playwright_remote('ws://playwright:8080/ws') as playwright:
flask_main | File "/usr/src/app/playwright_remote/sync_api/sync_playwright_remote.py", line 57, in enter
flask_main | playwright = self._playwright
flask_main | AttributeError: 'SyncPlaywrightRemoteContextManager' object has no attribute '_playwright'

I run Docker for playwright and try to run python code.

Collapse
 
yusukeiwaki profile image
Yusuke Iwaki • Edited

Your comment is assumed to be for the article dev.to/yusukeiwaki/using-playwrigh... :)

Could you share the version of Playwright?
I guess the version of Playwright is 1.15.x, that changed the initialization logic of Playwright.
github.com/microsoft/playwright-py...
Could you post an bugreport into github.com/YusukeIwaki/playwright-... if possible?