Capybara-Webkit: page.should have_content() not implemented? - ruby-on-rails

I have recently attempted to use:
Then(/^I should see "(.*?)"$/) do |arg1|
page.should have_content(arg1)
end
To query the page and see if a text exists on the page.
This worked fine with the default Capybara driver, but after setting the javascript driver to capybara-webkit and running the tests again I get:
undefined method `find_xpath' for #<Capybara::Webkit::Driver:0x007fa3f00152e8> (NoMethodError)
./features/step_definitions/customer_steps.rb:12:in `/^I should see "(.*?)"$/'
features/manage_customers.feature:10:in `Then I should see "ABC XYZ"'
I am using the javascript driver since I am also using AngularJS to populate my data.
My questions:
Are have_content() and page.has_content?() not implemented in capybara-webkit?
What could be the source of the problem?

Looks like there may be an issue with capybara-webkit: https://github.com/thoughtbot/capybara-webkit/issues/499
I added gem 'capybara', '2.0.3' to my gemfile for now and that seems to fix the issue.

Related

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

Cucumber Strange Behaviour

While using cucumber for BDD, i have found a very strange scenario where my "I should see" method (default implementation is failling).
Here is my scenario definition:
When I go to signup page
And I fill in "Username" with "ben#test.com"
And I press "Sign up"
Then I should see "Anything that i type here. ABC XYZ"
Obviously, the text "Anything that i type here. ABC XYZ" is not on the page but the cucumber wouldnt fail the scenario. Here is the definition of "I should see"
Then /^(?:|I )should see "([^"]*)"$/ do |text|
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
end
A bit more digging, and it seems that the problem is that 1.8.7 is not supported in capybara. Instead of complaining loudly, it simply performs badly. Things like filling in forms work etc but the has_content fails miserably. Maybe it would be better if it shouted loudly that it needed 1.9.x.
Anyway I changed my Gemfile to
gem "capybara", "1.1.4", :group => :test
and it all seems to be working again.
Should I log this as an issue against capybara? The idea would be to just scream that it wont work with 1.8.7.
More on this discussion: https://groups.google.com/forum/?fromgroups=#!topic/cukes/B3UbbyG5k6s

Why is capybara not available in request specs?

Working on a new Rails 3.2.9 app with rspec and capybara.
I have the following in the Gemfile:
gem 'rspec-rails'
gem 'capybara'
and the following in spec/spec_helper.rb:
require 'rspec/rails'
require 'capybara/rspec'
and in spec/requests/asdf_spec.rb:
require 'spec_helper'
describe 'Asdf' do
describe "GET /asdfs" do
it "should list asdfs" do
visit asdfs_path
end
end
end
This test is failing:
Failure/Error: visit asdfs_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x007fa7b68961a0>
# ./spec/requests/asdfs_spec.rb:19:in `block (4 levels) in <top (required)>'
So it looks like Capybara isn't getting loaded. Ack, why not? I feel like I've done this exact same thing a dozen times before... probably blanking on something stupid.
So it was a capybara version 2 change. I found this:
http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
which explains:
Upon upgrading to capybara 2.0, capybara will not be available by
default in RSpec request specs. Instead, a new type of spec--the
feature spec--has been created for use with capybara.
To upgrade to capybara 2.0, you'll need to do a few things:
Upgrade rspec-rails to 2.12.0 or greater
Move any tests that use capybara from spec/requests to spec/features. Capybara
tests use the visit method and usually assert against page.
Just some additional info for anybody having the same problem with the Capybara upgrade to 2.x. Check out rspec-rails docs under the Upgrading to Capybara 2 section.
Basically, In order to use the Capybara DSL(page & visit) you must move your existing specs into the spec/features directory. So you can only use page & visit in acceptance tests. No more page & visit in controller and request specs. Only the rack-test DSL (get|post|put|delete|head/response.body) is allowed in controller and request specs.
This is not recommended but there is a way to keep your specs working as they are:
RSpec.configure do |c|
c.include Capybara::DSL, :example_group => {
:file_path => "spec/requests"
}
end
The docs state that if you go this route then you are overriding the intended behavior and you are taking a risk.
And definitely don't make this as a reason not to upgrade to Capybara 2.x. Feature specs are easy to get used to and are easy to read. feature is just an alias for describe, background is an alias for before, scenario for it, and given for let.
Hope this helps anyone confused by the new changes.
The issue is in capybara gem itself.
gem 'capybara', '1.1.2' solves this issue ( Version 2.0.x fails )

Cucumber/Capybara/Selenium-Webdriver error?

I've recently upgraded to Rails 3 and I'm trying to get my Cucumber tests to run.
When I run the tests, I'm getting the following error:
wrong number of arguments (2 for 1) (ArgumentError)
./features/step_definitions/user_steps.rb:24:in `/^I am logged in$/'
features/account.feature:8:in `Given I am logged in'
This test does nothing more than opening my login page. When I don't include the #javascript flag at the top of my feature file, it runs fine. When I do include it, I get the error and Selenium (I think) opens an instance of Firefox but nothing happens in the browser.
EDIT:
My I am logged in step looks like this:
Given /^I am logged in$/ do
#user = Factory(:user, :email => "cucumber#test.com")
#user.activate
visit path_to("the login page")
end
I tracked down a similar problem using pry.
[1] pry(#<Cucumber::Rails::World>)> step %{I go to login}
ArgumentError: wrong number of arguments (2 for 1)
from ~/.rvm/gems/ruby-1.8.7-p352/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in 'parse'
Which was fixed with gem 'json', :require => 'json/pure' ahead of bson in my Gemfile.
Edit: Look like it was also necessary to force use of 1.9.2 (rvm --rvmrc --create 1.9.2).

Undefined local variable or method 'page' for Cucumber::Rails::World (NameError)

I'm running a basic feature by following RBates RailsCasts tutorial using Rspec 2.5.0 and Cucumber-rails 0.4.1 on a cygwin environment. I am at the step where I am testing "Then I should see"
For example:
Scenario: Stores List
Given I have stores named Pizza, Breadsticks
When I go to the list of stores
**Then I should see "Pizza"**
Running cucumber features gives me the following error message:
Undefined local variable or method 'page' for Cucumber::Rails::World (NameError)
Then I should See is defined in the web_steps file as follows:
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
Any guidance would be appreciated!
Thank you!
Fixed the error. I had commented out:
Capybara.default_selector = :css because of a previous issue (See: https://github.com/aslakhellesoy/cucumber-rails/issues/120). Once i included the following:
require 'capybara/rails'
require 'capybara/cucumber'
It fixed the capybara issue and the page method was available.
Thanks.
I don't know much of RoR, and without seeing more code, it appears you haven't defined the variable 'page', or you defined it in an area that is outside the scope of where you're trying to use it.

Resources