DEV Community

Josua Schmid for Renuo AG

Posted on

Catch JS Errors with Playwright

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
Enter fullscreen mode Exit fullscreen mode

The output of a failed test will look like this:

page.execute_script('inexistentFunction()') causes Playwright::Error

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)