我正在从 capybara-webkit 切换到 poltergeist/phantomjs。我遇到了计时问题,并且已确定哪一行代码需要预热时间,但我无法确定这是为什么或如何解决它。
我有一个 225 行规范文件,其中包含几十个测试。在任何给定的测试运行中,其中 1 或 2 个将始终失败。可以是其中任何一个,但不一致。
这个 before 块适用于所有测试。我已经注释了代码来解释情况。
before do
# --> Create several objects using factory girl
# --> stub some methods
visit root_path
# sleep 0.5 here always fixes the problem. sleeping before this line does not.
find("a#sign-in-link").click
within "form#new_session" do
fill_in 'user[email]', with: user.email
fill_in 'user[password]', with: user.password
end
click_button "Sign in"
# On the specs which fail, this is the line which fails.
# Capybara::ElementNotFound: Unable to find link "People"
click_link "People"
end
click_button "Sign in"
结果导致页面加载。因此,人们可能会认为问题与该页面没有时间加载有关。save_screenshot
or save_and_open_page
登录后立即显示身份验证实际上未成功(与失败消息一致)。
这表明问题是因为在某些情况下测试数据没有时间加载。然而,为什么会sleep
必须来after visit root_path
?即使我的测试数据的某些方面需要通过访问页面来“预热”(据我想象,情况并非如此),我的根路径中没有任何内容可以加载此数据。提交登录表单时更有可能发生这种情况。
可能是什么原因造成的?
更多信息
Using this https://gist.github.com/jjb/b04a7385cc058a18d1a3我尝试输入的代码wait_for_ajax_completion
after visit root_path
,还是失败了。
然后一时兴起,我尝试把它放在前面click_button "Sign in"
-- 这会持续增加失败次数(但数量不会一致,它仍然随每次运行而变化)。