Can't make sense of capybara behaviour

时间:2015-07-28 15:40:53

标签: ruby-on-rails capybara

I have this integration test for my Rails App:

require 'test_helper'

class StudyCapybaraTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:archer)
    @vocabs = @user.vocabs
    Capybara.register_driver :selenium_chrome do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end
    Capybara.current_driver = :selenium_chrome
    #Capybara.default_wait_time = 5
    visit login_path
    fill_in "session_email",    with: @user.email
    fill_in "session_password", with: 'password'
    click_button "session_commit"
  end

  test "full study process" do
    assert_title "Home | Word Up"
    visit study_user_path(@user)
    ....
  end
end

Weirdly, when I remove the first line of the first test "full study process"

assert_title "Home | Word Up"

the test fails because the test user doesn't seem to be logged in. The same problem occurs when I move

visit study_user_path(@user)

into the setup function (as it was before).

But that doesn't change anything about the sequence and logic, right? The only thing I can think of, is that the assertion comes to early, and the app doesn't have time to execute the instructions needed to meet the assertions.

Is this a timing issue, and if so, how can I prevent them from happening in the future? Thx!

1 个答案:

答案 0 :(得分:1)

首先,你对它的直觉是一个时间问题是正确的。 click_button就是这样 - 它点击按钮。它不会等待表单发布,它不会等待任何ajax发生等等。因此,如果没有assert_title,您的测试是单击按钮,并立即更改浏览器中的URL。更改浏览器中的URL将具有取消由click_button调用触发的任何表单提交或行为的效果。你需要在click_button之后等待点击按钮后在页面上发生变化的内容,

assert_text('You are now logged in')

其次,在每次测试之前运行setup方法,所以你真的不想在那里注册驱动程序,因为它只需要完成一次。