Cucumber/Capybara: tests fail randomly under PhantomJS

烂漫一生 提交于 2020-01-04 09:56:50

问题


When I run cucumber scenarios under PhantomJS I get

Capybara::ElementNotFound: Unable to find css ".given_class"

exceptions in random places

it looks like driver does not wait for element appearance

I'm using: Ruby 2.0 Cucumber 1.3.6 Capybara 2.1.0 Selenium-webdriver 2.35.1 PhantomJS 1.9.1


回答1:


Capybara, particularly with PhantomJS will load a page really quickly, and perform checks for elements. As such, some elements may not have loaded and tests fail. By default capybara has a wait time of 2, which you can increase. Maybe try:

Capybara.default_wait_time = 5

like they suggest in the docs here.

You can also add a Sleep(1) in your step definition, but that is considered bad form.

If that doesn't do it for you, try specifying where on the page the element should be found.

For example:

Then /^I should see "(.*?)" on the dashboard$/ do |your_element|
  within("#your") do
    expect(page).to have_content(your_element)
  end
end



回答2:


Check the Capybara.automatic_reload parameter. By default it is true and capybara floods server with requests every 50 ms, if it does not see what it wants :).



来源:https://stackoverflow.com/questions/18693683/cucumber-capybara-tests-fail-randomly-under-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!