简体   繁体   中英

Capybara fails on Capybara::ElementNotFound during cucumber test

I recently got a small Rails-project going to help me learn the environment and the associated languages/tools that go with it (Ruby, Cucumber, RSpec, Haml...).

What got me stuck recently is that I decided to go from one scenario to multiple as seen in this gist . When I introduced the second scenario, I noticed that the total values of the mineral piles were not summarized, this caused by the fact that the step definition for "I have mined of that mineral" looked like this:

Given /^I have mined "([^"]*)" of that mineral$/ do |amount|
  visit(root_path)
  fill_in(@mineral.name, :with => amount)
end

The culprit was obviously the visit(root_path) call which reset the already filled in fields as soon as a new field was to be filled in. Important to note is however that apart from the fact the "Total value: " failed at the end, all of the tests were green.

I decided that since all of the stuff done in this feature requires one to be on the index/root page (I'll probably change this later on) I'll just put it into a Background: block. The corresponding step definition is nothing more than that visit(root_path) line extracted into its own step.

This however has resulted in every fill_in() call to fail with the following (or something very similar):

cannot fill in, no text field, text area or password field with id, name, or label 'tritanium' found (Capybara::ElementNotFound)
(eval):2:in `fill_in'
./features/step_definitions/miner_converts_minerals.rb:12:in `/^I have mined (\d+) of that mineral$/'
features/miner_converts_minerals.feature:12:in `And I have mined <amount> of that mineral'

My take on this is that for some reason the visiting of the page in Background: isn't persistent, and it forgets that it got there... for some reason. Note that (as mentioned above) it all stops working as soon as visit(root_path) is moved out of the "I have mined..."-step into a separate step, leading me to guess that visit must be done inside the same block, but I could be wrong.

EDIT: I tried using the save_and_open_page method to view what is actually displayed on this page that Capybara can't find the elements on. It turns out that the "Calculate" button is there, but none of the fields in the table are there. It looks like the problem lies in the fact that the fields are not generated.

I think all of the relevant code for solving this can be found in above mentioned gist. Thanks for reading!

It turned out to mainly be caused by the fact that the test database had the table for Mineral but no rows imported.

This in turn caused the find_or_create_by_name method to be able to create the minerals mentioned in the tests, but not the other ones, giving the illusion of working somewhat when the visit(root_path) call was added in the "Given I have mined..." -block (in the .feature-file).

The solution was to seed the test database properly and then use find_by_name instead of find_or_create_by_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM