使用 CircleCI 和 Minitest 进行前端测试:测试等待页面加载的时间不够长

Frontend testing with CircleCI and Minitest: Tests don't wait long enough for page to load

所以我的测试似乎有 75% 的时间通过了。其他 25% 失败是因为测试套件等待页面加载的时间不够长。在我们的本地机器上,测试大约需要 35 秒,但在 CircleCI 的日志上它只需要 5 秒。 (在本地我 运行 使用 BROWSER=chrome m path/to/test.file 进行测试)

我是这个技术堆栈的新手,所以非常感谢任何帮助,即使它只是适当的参考文档。

it 'should use this form' do

  assert page.has_content?('#target_form')
  within '#target_form' do
    # fill in the form and apply payment
  end

  # it will throw errors here "can't find css..." 
  # the text/element won't even have loaded yet
  # by the time the test is run
  assert_equal '24', find_qa('price').text
end

您编写断言的方式没有利用 Capybara 的 waiting/retrying 行为,因此 运行 在较慢的硬件上(CircleCI 与您的本地机器相比)可能会导致您的测试失败。 assert_equal 评估两个参数并比较它们,然后就完成了。这并不好,因为 Capybara 假定每个操作都可以执行异步操作,因此它不必等待按钮单击来提交和加载新页面(因为它无法知道按钮单击可能产生什么操作)。但是,如果您使用 Capybara 提供的断言,它将 wait/retry 比较最多 Capybara.default_max_wait_time 秒以使比较为真。我不确定您的 find_qa 方法是如何定义的,但是如果您声明了自定义 :qa 选择器,您可以执行类似

的操作
assert_selector :qa, 'price', text: '24'

如果 find_qa 只是做一个 CSS 选择器那么你可以做

assert_selector :css, "whatever find_qa('price') produces as a css selector", text: '24'

或者你可以

find_qa('price').assert_text('24')

由于您使用的是 minitest,因此您可能需要阅读 - https://github.com/teamcapybara/capybara#using-capybara-with-minitest - and configure Capybara's minitest matchers so the counts of assertions run are correct, and to provide a bunch more specific assertions that will utilize Capybara's waiting/retrying behavior. See https://github.com/teamcapybara/capybara/blob/master/lib/capybara/minitest.rb 以了解添加的匹配器,这样您就可以编写类似

的内容
assert_text find_qa('price'), '24'
assert_xpath 'an XPath selector', ...
assert_title ...
assert_current_path ...

等等