cucumber's web_step#follow using capybara produces a NameError - ruby-on-rails

Does anyone know why I get the following error when I use the web_step#follow method?
When I follow "Stuff" within "#main-nav" # features/step_definitions/web_steps.rb:33
undefined local variable or method `node' for #<Capybara::Driver::RackTest::Node:0x00000101409b40> (NameError)
./features/step_definitions/web_steps.rb:35:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `block in with_scope'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:34:in `/^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/'
This is the html output:
<ul id='main-nav'>
<li>Things</li>
<li>Stuff</li>
</ul>
P.S. I have removed webrat and am solely using capybara
Thanks in advance!

per: https://github.com/jnicklas/capybara/issues/110
comment out this line in env.rb:
require 'cucumber/rails/capybara_javascript_emulation'
Note: after commenting that line you'll have to explicitly tag your features/scenarios with #javascript if you want to click links with onclick javascript handlers.
See also: https://github.com/aslakhellesoy/cucumber-rails/issues/77 which eventually takes you on a journey to discover it should be fixed in cucumber-rails v0.4.0 (2011-03-20). This may still be relevant for folks with Rails 2.3.x projects using cucumber-rails v0.3.2

This means that the actual output of your page does not include the element you're trying to search for. For example, if you had with_scope("#my_div") but your content didn't have any divs with the id my_div it would raise this exception.
I'd suggest trying to add a cucumber step of Then show me the page before the failing step, and investigate the source of the generated page.

Related

Capybara Minitest `has_field?` works, but `assert_field` gives error "unused parameters passed to Capybara::Queries::SelectorQuery"

In my page, I have a link like this:
<input value="" data-autofocus="true" class="form-control"
type="text" name="user[login]" id="user_login" />
Using Capybara with Minitest, driver :rack_test, the following selector finds the input, but the parallel assertion gets an error [edited to include the trace]:
(ruby) has_field?("user_login")
true
(ruby) assert_field("user_login")
eval error: Unused parameters passed to Capybara::Queries::SelectorQuery : [:field, "user_login"]
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/queries/selector_query.rb:52:in `initialize'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/node/matchers.rb:842:in `new'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/node/matchers.rb:842:in `_verify_selector_result'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/node/matchers.rb:110:in `assert_selector'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/session.rb:771:in `assert_selector'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/dsl.rb:52:in `call'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/dsl.rb:52:in `assert_selector'
/home/vagrant/.rvm/gems/ruby-3.0.4/gems/capybara-3.37.1/lib/capybara/minitest.rb:288:in `block (2 levels) in <module:Assertions>'
(rdbg)//vagrant/mo/test/integration/capybara_student_test.rb:1:in `block in test_creating_drafts'
nil
The rubydocs for Capybara::Minitest::Assertions#assert_field say that the method is built straight off Node::Matchers#has_field?. I wonder why it isn't working?
EDIT: Belatedly realizing this is probably relevant... I'm including Capybara::Minitest::Assertions in the tests, and using Sean P. Doyle's gem ActionDispatch::Testing::Integration::Capybara to use Capybara in integration tests.
Thanks to the eagle eye of #ThomasWalpole for pointing the way — I didn't know it mattered so much where requires and includes are located in the ancestry of a class.
First of all, the author of the experimental gem i was using informed me that my manual includes of the Capybara classes were redundant; they are already included by the gem. I removed the gem to isolate the problem, which was present already had before adding the gem.
The core of it was, having require capybara/rails, require capybara/minitest and include Capybara::DSL in my test_helper.rb, but include Capybara::Minitest::Assertions in my separate CapybaraIntegrationTestCase.rb seems to have put the call stack out of order. Moving all Capybara requires and includes to the CapybaraIntegrationTestCase cleared this up.

Cucumber feature failing when rendering template with confusing error

I'm trying to make my tests pass, but cucumber is failing with a very confusing error, probably template related, that I'm not able to understand and fix.
Here is my Gemfile and Gemfile.lock:
https://gist.github.com/ngw/dada0c0658a9ef8b5573
You can assume that everything is on its latest version as of today, 28 March 2015, including rails, cucumber, ruby (2.2.0p0), database_cleaner, and so on.
The view that fails to render is a devise view that renders just fine on the browser.
Here is the exception raised:
Background: # features/users/sign_up.feature:6
Given I am not logged in # features/step_definitions/user_steps.rb:9
Scenario: User signs up with valid data # features/users/sign_up.feature:9
When I sign up with valid user data # features/step_definitions/user_steps.rb:13
undefined method `[]' for nil:NilClass
(in /Users/ngw/petswantpats/app/assets/stylesheets/application.css) (ActionView::Template::Error)
./app/views/layouts/application.html.haml:6:in `_app_views_layouts_application_html_haml___302582815389802583_70306431437360'
./features/step_definitions/user_steps.rb:6:in `sign_up'
./features/step_definitions/user_steps.rb:15:in `/^I sign up with valid user data$/'
./features/support/database_cleaner.rb:11:in `block in <top (required)>'
features/users/sign_up.feature:10:in `When I sign up with valid user data'
Then I should see a successful sign up message # features/step_definitions/user_steps.rb:18
Here are the incriminated views:
https://gist.github.com/ngw/95c0a0e6db86cfa0357f
Here is the content of application.css.scss:
https://gist.github.com/ngw/ab766e0cfb5167d2d76a
The failing step:
def sign_up
visit '/users/sign_up'
end
Is it something JS related? I'm using poltergeist with capybara, my env.rb has:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
If I use launchy to open it in the browser I have a white page (???) and no other usable information in my test.log...
Can someone help me on how to debug this? TIA

Using "Bundle Update" in Rails produced bazillion errors on test

I'm trudging through this deeply error-prone tutorial on Ruby on Rails located here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book.
I've been working through a section about testing using rspec. Now, the instructions that this tutorial provided created a whole host of errors (deprecations, array issues, etc.) that filled up my page. After rummaging the internet for several hours, I decided to follow several suggestions to update all my gems.
Having updated my gems and attempted to perform this very basic test (the default test really), I got this whole pile of error that I couldn't begin to understand. All I can say is "please help".
Thank you.
> bundle exec rspec spec/requests/static_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
←[31mF←[0m
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
←[31mFailure/Error:←[0m ←[31mget static_pages_path←[0m
←[31mNameError:←[0m
←[31mundefined local variable or method `static_pages_path' for #<RSpec::
Core::ExampleGroup::Nested_1::Nested_1:0x5168040>←[0m
←[36m # ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.19901 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:5←[0m ←[36m# StaticPages GET /st
atic_pages works! (now write some real specs)←[0m
If you upgraded all your gems to the latest, then one issue you probably have is your newer capybara gem no longer looks for your tests in 'spec/requests'. That test needs to be in 'spec/features' now. If there is no 'spec/features' just create it.
Also, capybara will need this line in your 'spec/spec_helper.rb' if it isn't already:
require 'capybara/rspec'

undefined method page for #<Array:0xc347540> kaminari "page" error. rails_admin

i am using rails_admin. when i go to certain resource. by typin url
localhost:3000/admin/rule
than it give me this error. code is:
scope = Rule.all
scope.page(1).per(2)
. above code is writtten in rails_admin gem.in a file named mongoid.rb placed in adaptors folder. complete log is:
NoMethodError (undefined method `page' for #<Array:0xcea7408>):
mongoid (2.4.8) lib/mongoid/criteria.rb:385:in `method_missing'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/kaminari-809105ad782a/lib/kaminari/models/mongoid_extension.rb:11:in `page'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/adapters/mongoid.rb:37:in `all'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:127:in `get_collection'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:39:in `list_entries'
/home/usman/.rvm/gems/ruby-1.9.2-p290#system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/config/actions/index.rb:30:in `block (2 levels) in <class:Index>'
what should i do to resolve this error?
You can not call Kaminari methods on Array, because Rule.all will return Array.
So you have to do something like this: Rule.page(1).per(2)
Here is documentation and examples of Kaminari usage:
https://github.com/amatsuda/kaminari
I've been running into this issue off and on for a while now using Mongoid. Sometimes refreshing the page in RailsAdmin would fix it.
I figured out that the problem is Kaminari's hooks are not initialized in my environment, so the models that rely on Kaminari's extension methods don't have them available.
I simply took the following line from Kaminari's railtie and put it at the top of my rails_admin initializer:
Kaminari::Hooks.init
Now things seem to be working for me. However, I don't know why the ActiveSupport callback is not running that code.
Use this
Kaminari.paginate_array(Rule.all).page(params[:page])
Kader's solution is great! The only thing is I found I have to add .per to make it work.
Kaminari.paginate_array(Rule.all).page(params[:page]).per(PER_PAGE_RECORDS)

Rails: What gives date/datetime the (1i), (2i), etc values?

I can't seem to find how a date or datetime gets to have (1i), (2i), etc. What method gives this? I'm asking because I have a gem that breaks this functionality in Rails, and I want to make a test for it, and fix it.
This is the new error that happens when I install the gem:
undefined method `published_at(1i)=' for #<Page:0xc25ccd4> (NoMethodError)
./app/controllers/admin/pages_controller.rb:21:in `update'
./vendor/plugins/exception_notification/lib/exception_notifier.rb:19:in `call'
./features/step_definitions/web_steps.rb:29
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:28:in `/^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/'
features/domain_linking.feature:11:in `And I press "Update page"'
You'd need to take a look into the Rails source code, starting from ActionView::Helpers::DateHelper module and digging further in to get to the appropriate function.

Resources