黄瓜因错误未定义方法“访问"而失败 [英] Cucumber fails with error undefined method `visit'

查看:19
本文介绍了黄瓜因错误未定义方法“访问"而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行

rake cucumber 

甚至可以通过未经测试的功能.运行时

passes even untested features. while running

cucumber features/something.feature

抛出

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

我已经用谷歌搜索了一些 github 问题,他们谈论它但没有缓解.这在使用url参数时在没有机架的情况下运行Capybara会产生错误 很有帮助,但没有解决我的问题

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

更新我确实从水豚 readme

水豚与黄瓜一起使用

cucumber-rails gem 内置了对 Capybara 的支持.如果你没有使用 Rails,手动加载 capybara/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

但是在哪个文件中包含上述内容?我尝试将上述内容添加到 env.rb 并收到此错误:

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

未初始化的常量 ActionController (NameError)

uninitialized constant ActionController (NameError)

现在评论后,我仍然得到同样的错误.

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

这是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

这里是 spec/spec_helper.rb(截断)

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

这里是 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

推荐答案

愿这个答案能帮助一些迷失的灵魂.

May this answer help some lost soul.

终于在这里得到了答案:黄瓜无头xvfb ubuntu

Finally got the answer here: Cucumber headless xvfb ubuntu

对于任何想要进行无头浏览的人来说,这救了我:

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

在 env.rb 中包含以下内容:

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是一个相对的rb文件headless.rb:

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

env.rb 和 headless.rb 都在 features/support 文件夹中

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

我能够进行 bdd 和网络测试.

I am able to to do bdd and web testing.

这篇关于黄瓜因错误未定义方法“访问"而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆