重定向导致 Capybara-webkit 失败

redirects cause Capybara-webkit to fail

整天都在这个问题上:经过大量调查,似乎任何时候我们都有以下序列,rspec/capybara 完成测试,然后挂起。

顺序是:

Selenium ff 一切正常,但 webkit 测试 运行 成功,然后挂起。然后你必须按 ctrl-c 两次。

我们在三个不同的 mac 操作系统(1 mac os、2 linuxy)上得到了相同的行为,所以问题一定是与正在加载的实际网页的交互,但请注意页面加载正常。

最新的 capybara-webkit、qt 等(来自 mac: 水豚:2.5.0 水豚-webkit:1.7.1 Qt:5.5.1 WebKit:538.1 QtWebKit: 5.5.1)

例如:

it 'redirects an existing logged in user to the dashboard' do
  user = FactoryGirl.create :user
  login(user, then_visit: "/")
  # the above which just does a session/new?redirect_to="/" succeeds but
  # rspec never terminates.
  # if I change it to
  #    login(user)
  #    wait(10)
  #    visit "/"
  # everything works fine.
  find(".tp-dashboard", wait: 10)      
  expect(page.current_path).to eq "/account/#{user.id}/dashboard"
end

登录方法只是执行 session/new 然后让用户登录。

为了让事情更清楚(至少对我自己来说)我添加了这个

  after(:all) do
    puts "**************************** I know I am done, I just can't quit **********************************"
  end

果然我得到了这个输出:

.**************************** I know I am done, I just can't quit **********************************


Finished in 18.35 seconds (files took 13.23 seconds to load)
1 example, 0 failures

我想等多久就等多久,需要按两次 ctrl-c 才能退出。

>^C
RSpec is shutting down and will print the summary report... Interrupt again to force quit.
>^C:mitch$ 

这里有两个日志:第一个是有 10 秒的延迟,足以让页面加载。第二种是有 1 秒的延迟,页面没有加载(所以测试失败,但是 rspec 退出)

https://gist.github.com/catmando/81dafb5212e8163389bd

https://gist.github.com/catmando/264accacf25e98bcb179

这是登录方法的价值所在,但请理解,在 javascript 加载由控制器重定向的页面的任何情况下,我们都会发生同样的事情:

def login(user, opts = {})
  visit "/session/new#{'?return_to='+opts[:then_visit] if opts[:then_visit]}"
  fill_in "user_session[login]", with: user.login
  fill_in "user_session[password]", with: user.password
  click_button "mobile_login_submit"
end

我不得不放弃并使用 phantomjs / poltergeist...此处设置的优秀说明:

我刚刚使用了当前发布的 phantomjs 并为绑定添加了一个 polyfill(如果你遇到错误并且你正在使用 react 或 react.rb 很重要)