【发布时间】:2016-04-16 07:00:45
【问题描述】:
整天在这个问题上:经过大量调查,似乎任何时候我们有以下序列,rspec/capybara 完成测试,然后挂起。
顺序是:
- 访问页面
- 执行一些操作(点击)导致 JS 加载一个新页面
- 控制器对该页面进行重定向
使用 Selenium ff 一切正常,但使用 webkit 测试成功运行,然后挂起。然后你必须按两次 ctrl-c。
我们在三台不同的机器(1 个 mac os,2 个 linuxy)上得到了相同的行为,所以问题一定是与正在加载的实际网页的交互,但请注意页面加载正常。
最新的capybara-webkit、qt等(来自mac: 水豚:2.5.0 水豚webkit:1.7.1 数量:5.5.1 网络套件: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
登录方法只是执行一次会话/新建,然后将用户登录。
为了让事情清楚(至少对我自己来说)我添加了这个
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
【问题讨论】:
-
不确定为什么您的测试会挂起,但不是您所期望的,只要您使用 Capybara 2.5+,您就可以使用
expect(page).to have_current_path("/account/#{user.id}/dashboard"),它使用 Capybaras 等待页面更改的行为并消除了查找或睡眠的需要 -
Tom - 是的,意识到...正如围绕睡眠笔记的评论,我们通常不会睡觉。这样做只是为了证明它似乎正在等待导致问题的页面加载,无论使用什么机制等待。不过谢谢提醒!
-
当你按下 ctrl-c 来显示代码在哪里时,它会给出堆栈跟踪或任何东西吗?
-
不 - 刚刚添加了完整的输出......
-
你有没有其他的 after(:all/:suite) 块或者你正在使用任何在块之后添加的 gem?
标签: rspec capybara capybara-webkit