Cucumber/Capybara: tests fail randomly under Phant

2019-07-15 05:30发布

问题:

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 :).