Selenium has issues. I'm using it with Capybara and the latest encounter was a Net::ReadTimeout
. I switched to Playwright following Justin Searls guide.
One issue I encountered after the migration was that browser console logs were not accessible anymore with page.driver.browser.logs.get(:browser)
. We use them to fail tests on JavaScript errors.
One solution is to record them from the Playwright page with an event handler.
config.before(:each, type: :system, js: true) do |example|
example.metadata[:js_console_messages] ||= []
Capybara.current_session.driver.with_playwright_page do |page|
page.on('console', lambda { |msg|
example.metadata[:js_console_messages] << {
type: msg.type,
text: msg.text,
location: msg.location
}
})
end
end
config.after(:each, type: :system, js: true) do |example|
if example.metadata[:js_console_messages].present?
aggregate_failures 'javascript console messages' do
example.metadata[:js_console_messages].each do |msg|
if msg[:type] == 'error'
expect(msg[:text]).to be_nil, "JS Error: #{msg[:text]}"
elsif msg[:type] == 'warning'
warn "WARN: JS warning at #{msg[:location]}:\n#{msg[:text]}"
end
end
end
end
end
The output of a failed test will look like this:
The previous solution to simply look at page.driver.browser.logs.get(:browser)
felt more natural to me. I'd be happy to receive a hint on how to catch JS errors in Ruby more nicely.
Top comments (0)