module ApiMaker::SpecHelper

Public Instance Methods

chrome_logs() click to toggle source
# File lib/api_maker/spec_helper.rb, line 2
def chrome_logs
  page.driver.browser.manage.logs.get(:browser)
end
expect_no_chrome_errors() click to toggle source
# File lib/api_maker/spec_helper.rb, line 24
def expect_no_chrome_errors
  logs = chrome_logs.map(&:to_s)
  logs = logs.reject { |log| log.include?("Warning: Can't perform a React state update on an unmounted component.") }
  return if !logs || !logs.join("\n").include?("SEVERE ")

  expect_no_chrome_window_errors

  puts logs.join("\n")
  expect(logs).to eq nil
end
expect_no_chrome_window_errors() click to toggle source
# File lib/api_maker/spec_helper.rb, line 6
def expect_no_chrome_window_errors
  sleep 1

  errors = execute_script("if (window.errorLogger) { return window.errorLogger.getErrors() }")
  return if !errors.is_a?(Array) || errors.empty?

  last_error = errors.last

  custom_trace = []
  custom_trace += last_error.fetch("backtrace") if last_error["backtrace"].is_a?(Array)
  custom_trace += caller

  error = RuntimeError.new("#{last_error["errorClass"]}: #{last_error["message"]}")
  error.set_backtrace(custom_trace)

  raise error
end
expect_no_errors() click to toggle source
# File lib/api_maker/spec_helper.rb, line 35
def expect_no_errors
  expect_no_flash_errors
  expect_no_chrome_errors
end
js_fill_in(element_id, with:) click to toggle source
# File lib/api_maker/spec_helper.rb, line 40
def js_fill_in(element_id, with:)
  page.execute_script("document.querySelector(#{element_id.to_json}).value = #{with.to_json}")
end
pretty_html() click to toggle source
# File lib/api_maker/spec_helper.rb, line 44
def pretty_html
  require "htmlbeautifier"
  HtmlBeautifier.beautify(page.html)
end
reset_indexeddb() click to toggle source
# File lib/api_maker/spec_helper.rb, line 49
def reset_indexeddb
  execute_script "
    indexedDB.databases().then(function(databases) {
      var promises = []
      for(var database of databases) {
        promises.push(indexedDB.deleteDatabase(database.name))
      }

      Promise.all(promises).then(function() {
        console.error('All databases was deleted')
      })
    })
  "

  wait_for_condition do
    logs_text = chrome_logs.map(&:message).join("\n")
    logs_text.include?("\"All databases was deleted\"")
  end
end
wait_for_chrome(delay_sec: 0.5, timeout_sec: 6) { || ... } click to toggle source
# File lib/api_maker/spec_helper.rb, line 69
def wait_for_chrome(delay_sec: 0.5, timeout_sec: 6)
  WaitUtil.wait_for_condition("wait for chrome", timeout_sec: timeout_sec, delay_sec: delay_sec) do
    expect_no_chrome_errors
    yield
  end
end
wait_for_flash_message(expected_message, delay_sec: 0.5, timeout_sec: 10) click to toggle source
# File lib/api_maker/spec_helper.rb, line 76
def wait_for_flash_message(expected_message, delay_sec: 0.5, timeout_sec: 10)
  received_messages = []

  begin
    WaitUtil.wait_for_condition("wait for flash message", timeout_sec: timeout_sec, delay_sec: delay_sec) do
      expect_no_chrome_errors
      current_message = flash_message_text
      received_messages << current_message
      current_message == expected_message
    end
  rescue WaitUtil::TimeoutError
    expect(received_messages.uniq.reject(&:blank?)).to eq include expected_message
  end
end
wait_for_selector(selector) click to toggle source
# File lib/api_maker/spec_helper.rb, line 91
def wait_for_selector(selector)
  wait_for_chrome { page.has_selector?(selector) }
end
wait_for_selectors(*selectors) click to toggle source
# File lib/api_maker/spec_helper.rb, line 95
def wait_for_selectors(*selectors)
  selectors.each do |selector|
    wait_for_selector(selector)
  end
end