Skip to content

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

Description

@josh-m-sharpe

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions