|
| 1 | +RSpec.configure do |config| |
| 2 | + config.after(:each, :js) do |
| 3 | + next unless [:selenium_chrome, :selenium_chrome_headless].include?(Capybara.current_driver) |
| 4 | + |
| 5 | + errors = [] |
| 6 | + |
| 7 | + page.driver.browser.manage.logs.get(:browser).each do |entry| |
| 8 | + next if entry.message =~ /Download the React DevTools for a better development experience/ |
| 9 | + |
| 10 | + # Chrome doesn't allow suppressing log entries for network responses with statuses >= 400 (even if this is valid |
| 11 | + # api behaviour) and always show them in log as SEVERE errors prior to any js error resolving. Some info here: |
| 12 | + # https://bugs.chromium.org/p/chromium/issues/detail?id=677599 |
| 13 | + # https://bugs.chromium.org/p/chromium/issues/detail?id=124534 |
| 14 | + # https://bugs.chromium.org/p/chromium/issues/detail?id=96212 |
| 15 | + # This doesn't skips unhandled js errors which should be properly catched |
| 16 | + next if @skipped_chrome_network_errors&.any? do |e| |
| 17 | + entry.message.include? \ |
| 18 | + "#{e[:path]} - Failed to load resource: the server responded with a status of #{e[:status]}" |
| 19 | + end |
| 20 | + |
| 21 | + # needs external mock server for selenium |
| 22 | + if entry.message =~ /res.cloudinary.com/ |
| 23 | + puts entry.message |
| 24 | + next |
| 25 | + end |
| 26 | + |
| 27 | + # several places SLV |
| 28 | + if entry.message =~ /"Warning: Text content did not match. Server: \\"1000\\" Client: \\"1,000\\""/ |
| 29 | + puts entry.message |
| 30 | + next |
| 31 | + end |
| 32 | + |
| 33 | + pretty_message = if entry.message =~ %r{http://(127.0.0.1|app.lvh.me)[^ ]+ [\d:]+ } |
| 34 | + entry.message[/[^ ]+ [^ ]+ (.*)$/, 1]&.gsub(/\A"|"\Z/, "")&.gsub(/\\n/, "\n") |
| 35 | + else |
| 36 | + entry.message |
| 37 | + end |
| 38 | + |
| 39 | + %w[DEBUG INFO WARNING].include?(entry.level) ? puts(pretty_message) : errors << pretty_message |
| 40 | + end |
| 41 | + |
| 42 | + page.driver.browser.manage.logs.get(:driver).each do |entry| |
| 43 | + %w[DEBUG INFO WARNING].include?(entry.level) ? puts(entry.message) : errors << entry.message |
| 44 | + end |
| 45 | + |
| 46 | + raise("Java Script Error(s) on the page:\n\n" + errors.join("\n")) if errors.present? |
| 47 | + end |
| 48 | +end |
0 commit comments