水豚和恶作剧测试失败

时间:2015-12-03 18:57:18

标签: ruby-on-rails phantomjs capybara poltergeist

我一直在使用:用于capyabara的firefox驱动程序,这个测试正在通过,但是当我切换到poltergeist驱动程序时,测试现在失败,出现以下错误:

Minitest :: UnexpectedError:Capybara :: ElementNotFound:无法找到字段"电子邮件"

这是水豚和恶作剧的设置:

  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

以下是测试:

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

因此,当我将驱动程序更改为:selenium时,测试通过没有错误。

如何设置/修复poltergeist以通过此测试?

我拍了截图,它显示了加载指示符,它是使用以下代码在afterModel Ember挂钩中删除的div:

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

对于Ajax调用,我们有以下函数作为测试帮助程序来等待ajax调用:

  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 个答案:

答案 0 :(得分:1)

我怀疑这是一个时间问题...当使用poltergeist vs浏览器时,你可以得到这些奇怪的行为,其中一个元素存在,但没有真正加载因此错误...一种简单的方法来确认这是通过在fill_in电子邮件之前暂停一下......放置sleep 10只是为了安全...

补充提示:

为了帮助调试无头测试,请考虑添加故障屏幕截图 -

将其添加到Capybara设置的末尾:

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