YusukeIwaki / capybara-playwright-driver

Playwright driver for Capybara
MIT License
154 stars 13 forks source link

How to get console output from browser? #73

Closed amkisko closed 8 months ago

amkisko commented 8 months ago

Hey! I would like to get JavaScript output from browser side to ruby. Are there any possibilities to do so with this driver?

amkisko commented 8 months ago

As for reference: https://github.com/dbalatero/capybara-chromedriver-logger, it also has raise_js_errors feature which could be very useful.

YusukeIwaki commented 8 months ago

The function is not provided by this capybara driver, however we can use https://playwright-ruby-client.vercel.app/docs/api/console_message for observing the console log output via page.

page.driver.with_playwright_page do |page|
  page.on("console", ->(msg) {
    if msg.type == 'error'
      puts "error: #{msg.text}"
    end
  })
end

Could you try it?

amkisko commented 8 months ago

Thank you, works perfect!

I tried it in different places, seems that the callback is correctly set only after visit (probably when the page has been initialised), although page instance and with_playwright_page do not raise exceptions in the other places.

stale[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.