1
votes

I been using :firefox drivers for capyabara and this test was passing but when I switch to poltergeist driver the test been failing now with the following error:

Minitest::UnexpectedError: Capybara::ElementNotFound: Unable to find field "email"

Here is the capybara and poltergeist setup:

  def setup
    # FactoryGirl.lint
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.start
    Capybara.run_server = true
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app, :js_errors => false)
    end

    Capybara.default_driver = :poltergeist
    Capybara.current_driver = :poltergeist
    Capybara.javascript_driver = :poltergeist
    Capybara.app_host = 'http://localhost:4200'
    Capybara.server_port = 3000
    Capybara.default_max_wait_time = 5
  end

Here is the test:

test "User should be able to signin" do
    visit '/'
    wait_for_ajax
    fill_in 'email', with: @user.email
    fill_in 'password', with: @user.password
    assert true
  end

So, when I changed the driver to :selenium the test pass with no error.

How do I setup/fix poltergeist to pass this test?

I took screenshot and it shows loading indicator which is a div that is removed in afterModel Ember hook using the following code:

_ember['default'].$('.ember-load-indicator').remove();

For Ajax calls we have the following function as test helper to wait for ajax calls:

  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
1

1 Answers

1
votes

I suspect it's a timing issue... When using poltergeist vs a browser, you can get these weird behaviours, where an element is present, but not really loaded hence the error... An easy way to confirm this is by putting a sleep prior to fill_in email... put sleep 10 just to be safe...

Additional tip:

To help debug headless tests, consider adding screenshots on failures -

Add this to the end of your Capybara setup:

Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
  "screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//,'')}"
end

screenshot_path = "#{PROJECT_ROOT}/screenshot/"
Capybara.save_and_open_page_path = screenshot_path