twalpole / apparition

Capybara driver for Chrome using CDP
MIT License
363 stars 46 forks source link

page.evaluate_script doesn't wait for page to be ready? #77

Open josh-m-sharpe opened 3 years ago

josh-m-sharpe commented 3 years ago

Migrating from chromedriver to apparition, I had to add the helper method below to replace all of our calls to page.evaluate_script(...) because they would fail with Capybara::Apparition::JavascriptError. The script would usually be some jQuery finder, e.g. jQuery('#some_ele').length or jQuery.active. The error would be: ReferenceError: jQuery is not defined implying assets were not loaded and/or referenced script tags were not evaluated yet.

The failures were not consistent - seemingly random - which is why we made it a helper and replaced it everywhere. I suspect the root cause may have something to do with the timing of when the page becomes ready. Maybe?

We never saw these issues while using chromedriver.

  def evaluate_script(cmd)
    Timeout.timeout(Capybara.default_max_wait_time) do
      begin
        page.evaluate_script(cmd)
      rescue Capybara::Apparition::JavascriptError
        # maybe page isn't ready yet?
        sleep 0.1
        retry
      end
    end
  end