user2290820
user2290820

Reputation: 2749

Cucumber fails with error undefined method `visit'

running

rake cucumber 

passes even untested features. while running

cucumber features/something.feature

throws

undefined method `visit' for #<Object:0x00000001b13950> (NoMethodError)

I have googled some github issues where they talk about it but to no relief. This Running Capybara without rack produces errors when using url parameters was helpful but didn't resovle my issue

UPDATE I did touch on the following from capybara readme

Using Capybara with Cucumber

The cucumber-rails gem comes with Capybara support built-in. If you are not using Rails, manually load the capybara/cucumber module:

require 'capybara/cucumber' 
Capybara.app = MyRackApp

But in which file to include the above? I tried adding the above to env.rb and got this error:

uninitialized constant ActionController (NameError)

Now after commenting it, I still get the same error.

Here is the gemfile:

source 'https://rubygems.org'

#add dependency
gem 'diff-lcs', ">= 1.2.0"
gem 'rspec-expectations', "~> 3.0.0"

#add cucumber
group :test do
  gem 'cucumber-rails', :require => false
  # database_cleaner is not required, but highly recommended
  #gem 'database_cleaner', "~> 1.2.0"
  gem 'database_cleaner'
end

#add rspec
group :development, :test do
  gem 'rspec-rails', '~> 3.0'
  gem "capybara"
  gem 'factory_girl_rails'
  gem 'watir-webdriver'
  gem 'selenium-webdriver', '2.35.0'
  gem 'rubyzip'
  gem 'zip-zip'
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.7'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# 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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

Here is spec/spec_helper.rb (truncated)

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'capybara'
include Capybara::DSL # Adding this line solved the error
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  config.infer_spec_type_from_file_location!
  config.include Capybara::DSL
end

Here is env.rb

require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
#require 'capybara/rails'
#require 'capybara/session'
ActionController::Base.allow_rescue = false
begin
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation

Upvotes: 0

Views: 2832

Answers (1)

user2290820
user2290820

Reputation: 2749

May this answer help some lost soul.

Finally got the answer here: Cucumber headless xvfb ubuntu

For anyone wanting to do headless browsing, this rescued me:

Included following in env.rb:

require 'capybara'
require 'capybara/cucumber'
require 'cucumber/rails'
require 'capybara/rails'
require 'capybara/dsl'

require 'selenium/webdriver'



$port = <port_number>

#Capybara.app_host = '<localhost>:<port>'
Capybara.configure do |config|
  config.run_server = true
  #Capybara.default_host = "<localhost>:<port>"
  config.default_driver = :selenium
  #config.app = "make sure this isn't nil"
  config.app_host = "<hostname>:#{$port.to_s}"
  config.server_port = $port
end

#To add chrome webdriver do the following in your machine
#chmod +x chromedriver
#sudo mv chromedriver /usr/local/share/
#sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
#sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
#Register chrome as default Capybara webdriver
Capybara.register_driver :firefox do |app|
  # optional
  client = Selenium::WebDriver::Remote::Http::Default.new
  # optional
  #client.timeout = 120
  Capybara::Selenium::Driver.new(app, :browser => :firefox, :http_client => client)
end
#set default js driver
Capybara.javascript_driver = :firefox

#Include headless
require_relative 'headless'

headless is a relative rb file headless.rb:

if Capybara.current_driver == :selenium || Capybara.default_driver == :selenium
  require 'headless'

  headless = Headless.new
  headless.start

  at_exit do
    headless.destroy
  end
end

Both env.rb and headless.rb are in features/support folder

I am able to to do bdd and web testing.

Upvotes: 1

Related Questions