Skip to content

Instantly share code, notes, and snippets.

@joekrump
Last active June 27, 2023 15:27
Show Gist options
  • Save joekrump/32b35c62d082fd0c37ba791e911b02a6 to your computer and use it in GitHub Desktop.
Save joekrump/32b35c62d082fd0c37ba791e911b02a6 to your computer and use it in GitHub Desktop.
Config for RSpec + capybara with playwright
describe 'test something', driver: :playwright do
let(:user) { create(:user) }
before do
login_as user
end
it 'goes to a page and checks to see that it contains some content when not authorized' do
page.goto(restricted_path)
expect(page.text_content('body')).to include('You are not authorized to access this page.')
# Docs on the playwright ruby API: https://playwright-ruby-client.vercel.app/docs/article/api_coverage
# Playwright docs: https://playwright.dev/docs/api
# Playwright ruby client GitHub: https://github.com/YusukeIwaki/playwright-ruby-client/tree/7c4651e224977aa5b2e1837d7fb964c4957b34a9
end
end
# Requires gem 'playwright-ruby-client' and `yarn add playwright -D`
# Some docs: https://playwright-ruby-client.vercel.app/docs/article/getting_started
require 'rspec/rails'
require 'playwright'
class CapybaraNullDriver < Capybara::Driver::Base
def needs_server?
true
end
end
RSpec.configure do |config|
config.around(driver: :playwright) do |example|
Capybara.current_driver = :playwright
# Rails server is launched here, at the first time of accessing Capybara.current_session.server
base_url = Capybara.current_session.server.base_url
Playwright.create(playwright_cli_executable_path: Rails.root.join('node_modules/.bin/playwright')) do |playwright|
# pass any option for Playwright#launch and Browser#new_page as you prefer.
playwright.chromium.launch(headless: true) do |browser| # Could make this more dynamic. Could set headless true/false based on metadata
@browser = browser
@playwright_page = browser.new_page(baseURL: base_url)
# Override the save_screenshot method that is used by capybara at the end of spec run if there is a failure.
@playwright_page.define_singleton_method(:save_screenshot) do |path|
screenshot(path: path)
end
def page
@playwright_page
end
example.run
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment