Capybara::ElementNotFound: Unable to find xpath "/html" without webrat - ruby-on-rails

I can't make Capybara to work in many of my integration tests.
When I visit certain pages, I get the following html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
And so when I try to use the have_content() selector, it raises the following error:
Failure/Error: page.should have_content("HELLO")
Capybara::ElementNotFound:
Unable to find xpath "/html"
Some of the pages of my apps I can visit just fine, but some other ones I can't. There are even some pages that work at some places, and not at others:
require 'spec_helper'
describe HomeController do
it "shows the website description" do
visit root_path
puts page.html.length # ==> 108 (no html)
...
end
end
require 'spec_helper'
describe FlowsController do
it "should do stuff" do
visit root_path
puts page.html.length # ==> 4459, works even if the exact same one didn't work in home_controller_spec!
visit flows_path
puts page.html.length # ==> 3402, works
visit new_user_session_path
puts page.html.length # ==> 3330, works
within("form#new_user") do
fill_in 'Email', :with => 'email#example.com'
fill_in 'Password', :with => 'password'
click_on 'Sign in'
end
puts page.html.length # ==> 108, No html
end
end
I read in this post that this was an error that might occur when using Capybara and webrat at the same time. But I'm not using webrat at all, and I still get the error...
Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'pg'
gem 'thin'
gem 'devise'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
# Add rspec for testing
group :test, :development do
gem "rspec-rails", "~> 2.0"
gem "capybara"
gem "factory_girl_rails"
end
gem 'jquery-rails'

Try passing an String to describe, instead of the Controller class
describe "whatever" do
...
end

Related

NoMethodError: undefined method `visit' for #<RSpec::

I'm following the Ruby on Rails tutorial and ran into the following problem:
me#me:~/Ruby/sample_app$ bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x00000001e1df88>
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
Finished in 0.0006 seconds (files took 0.08149 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the content 'Sample App'
Here is my Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rspec-rails'
end
group :assets do
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
end
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
group :test do
gem 'capybara'
end
group :prodcution do
gem 'pg'
end
My spec/requests/static_pages_spec.rb:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
Digging up online suggested putting config.include Capybara::DSL in spec/spec_helper.rb. I did that and didn't work. Another suggestion said I need to move my static_pages_spec.rb to spec/features. I did that and that didn't work either.
Help? :D
visit is a method used in feature specs.
In your request specs you instead use the HTTP verbs get, post, patch etc to send http requests.
require 'rails_helper'
RSpec.describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
get '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
I believe you are using a badly outdated tutorial as well as subject.should has been depreciated for a long time in favor of
expect(subject).to.
While you can mix capybara into your request specs it may be better to use request specs as a low level integration tests and leave the higher level clicking and button pushing to your feature specs.
When you run into trouble you might want to refer to the official RSpec-rails documentation instead.
https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec

RoR Tutorial (2nd Ed.) 3.3.4 Can't get Rspec test to pass

New to Rails and going through this books tutorial. Been scratching my head on this one for nearly day now so I could use some help. Unable to get this test to pass from section 3.3.4 of the 2nd Edition book. (Ruby on Rails Tutorial) See failures and code below:
Failures:
1) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title', :text => 'Ruby on Rails Tutorial Sample App | Home')
expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
2) Static pages Help page should have the title 'Help'
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
expected to find css "title" with text "Ruby on Rails Tutorial Sample App
| Help" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'
3) Static pages About page should have the title 'About Us'
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About")
expected to find css "title" with text "Ruby on Rails Tutorial Sample App
| About" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:41:in `block (3 levels) in <top (required)>'
static_pages_spec.rb
require 'spec_helper'
require 'rails_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title',
:text => 'Ruby on Rails Tutorial Sample App | Home')
end
end
describe "Help page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => 'Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => 'About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | About")
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end
home.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
help.html.erb
<% provide(:title, 'Help') %>
<h1>Help</h1>
<p>
Get help on the Ruby on Rails Tutorial at the
Rails Tutorial help page
. To get help on this sample app, see the
Rails Tutorial book.
</p>
about.html.erb
<% provide(:title, 'About Us') %>
<h1>About Us</h1>
<p>
The Ruby on Rails Tutorial
is a project to make a book and screencasts to teach web development
with Ruby on Rails. This
is the sample application for the tutorial.
</p>
gemfile
source 'https://rubygems.org'
gem 'minitest'
gem 'test-unit'
gem 'rails_12factor'
ruby '2.1.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
gem 'sqlite3'
gem 'capybara'
gem 'rspec-rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
If anyone can get me pointed in the right direction, it would be greatly appreciated. I prefer to stick to solutions closest to the books methods but if I have to do something differently to get this to pass and proceed onwards then I'll take what I can get. Just please to don't suggest me doing anything crazy advanced that I clearly should be getting into yet if at all possible. Thanks. Simple is better here.
You're using RSpec and Capybara matchers in your spec, but you don't have those gems installed.
Here is the Gemfile from the 2nd edition:
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.16'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
Looking at your Gemfile, it appears you're using a newer version of Rails. If you are following Michael's tutorial, you should use the same versions that he specifies. There is a newer edition of the tutorial available, suited for Rails 4 development. You can read it here for free:
https://www.railstutorial.org/book

Ruby on Rails 4 and RSPEC

I am trying to get to grips with Rails and Rspec for testing using Capybara. I’m currently trying to test for uniqueness of a Profile Name for User. However from reading around fixtures should not be used with Rspec. How do I go about adding some base data for me to test against.
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
gem 'devise', '~> 3.2.3'
gem 'simple_form', '~> 3.0.1'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development do
gem 'awesome_print'
end
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
group :test do
gem 'capybara', '~> 2.0'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Create_spec.rb
require 'spec_helper'
describe "Creating a new user" do
def create_user(options={})
options[:first_name] ||= "Adam"
options[:last_name] ||= "Sackfield"
options[:profile_name] ||= "Sacki"
options[:email] ||= "email#email.com"
options[:password] ||= "password"
visit "/users/sign_up"
expect(page).to have_content "Sign up"
fill_in "First name", with: options[:first_name]
fill_in "Last name", with: options[:last_name]
fill_in "Profile name", with: options[:profile_name]
fill_in "Email", with: options[:email]
fill_in "Password", with: options[:password]
fill_in "Password confirmation", with: options[:password]
click_button "Sign up"
end
it "a user can register" do
create_user
expect(page).to have_content "You have signed up"
end
it "a user must enter a first name" do
create_user first_name: ""
expect(page).to_not have_content "You have signed up"
end
it "a user must enter a last name" do
create_user last_name: ""
expect(page).to_not have_content "You have signed up"
end
end
Thanks
Use factory_girl_rails gem.
factory_girl_rails provides Rails integration for factory_girl
which is a fixtures replacement with a straightforward definition
syntax, support for multiple build strategies (saved instances,
unsaved instances, attribute hashes, and stubbed objects), and support
for multiple factories for the same class (user, admin_user, and so
on), including factory inheritance.
Create a folder under spec directory as factories.
In spec_helper.rb,
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods ## Add only this line
end
Create factory files under spec/factories directory and define the required factories
as shown in Factory Girl Documentation
In your spec files, use the defined factories as shown here

Rspec Load Error in Ruby

I am working through Hartl's Ruby on Rails tutorial and am trying to test for user signups with invalid information and am getting a "Load Error" when running rspec. I am unsure how to fix this error, as I have updated my gem files.
$ bundle exec rspec spec/requests/user_pages_spec.rb \ > -e "signup with invalid information"
then I get this message:
/Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': cannot load such file -- /Users/kelvinyu/rails_projects/sample_app/signup with invalid information (LoadError)
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:77:in `rescue in run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:73:in `run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
Here is my spec/requests/user_pages_spec.rb:
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
And my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.1'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment this line on OS X.
# gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Am I missing any other information? What is the appropriate step to fix this error?
EDIT:
Spec helper here:
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the StaticPagesHelper. For example:
# describe StaticPagesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
#describe StaticPagesHelper do
# pending "add some examples to (or delete) #{__FILE__}"
#end
I notice that it is empty, however, the tutorial's steps did not require any changes.
Your error says the framework cannot load the file "/Users/kelvinyu/rails_projects/sample_app/signup with invalid information" which clearly isn't a file—the file is "spec/requests/user_pages_spec.rb". Try having everything in one line—without the \ > (this slash angle bracket only means that there, maybe, was a line break when Michael Hartl was typing up the tutorial.
Also, look to using describe and context interchangeably. This would make you write clearer specs. There is no magic to it. The sourcecode for RSpec shows that context is just another name for describe. But when you write specs with both, the meanings are clearer.

Ruby on Rails , Rspec Error while testing

I am following the tutorial from Michael Hartl . I tried the first test example for testing the app with Rspec and when I execute this command "bundle exec rspec spec\requests\static_pages_spec.rb" I get this error.
F
Failures:
1) Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x39e1510 #example=nil>
# ./spec/requests/static_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.001 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:3 # Home page should have the content 'Sample App'
static_pages_spec.rb
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
Gemfile.rb
source 'https://rubygems.org'
gem 'rails', '3.2.1'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
end
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
You need to require 'spec_helper' in your spec source.
Your spec_helper should include both rspec/rails and capybara/rails in it.
You'll want to use get instead of visit if you want to access the response, however.
If static_pages_spec.rb has string require 'spec_helper' and you get
Failure/Error: visit '/static_pages/home'
add to spec_helper.rb string config.include Capybara::DSL
it helped me

Resources