我正在尝试让 Capybara 在一个简单的 Ruby 脚本中运行——即没有/在 Rails 之外。这是脚本:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
include Capybara
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
visit('/')
问题是,当我运行这个时,我收到此错误:
NameError: uninitialized constant Capybara::Session
at top level in dsl.rb at line 52
method gem_original_require in custom_require.rb at line 36
method require in custom_require.rb at line 36
at top level in capybara_test.rb at line 3
method gem_original_require in custom_require.rb at line 31
method require in custom_require.rb at line 31
at top level in capybara_test.rb at line
我做错了什么?
更多信息:
谢谢!
尼尔
注意:根据 jnicklas 的评论,我尝试了这个,它更符合新的自述文件:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
include Capybara
def test_google
visit('/')
end
end
不幸的是,我仍然看到同样的错误:
NameError: uninitialized constant Capybara::Session
想法?
谢谢!
这似乎对我有用:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def test_google
visit('/')
end
end
end
t = MyCapybaraTest::Test.new
t.test_google
这表明,即使是不正确的文档也将永远存在。 Capybara README 曾经建议将 Capybara 包含在全局命名空间中,这是一个“真的”坏主意,并且会弄乱许多随机的事情。您应该将 Capybara 包含在您自己的模块或类中并使用它。 查看自述文件以了解当前的最佳实践。
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'capybara'
gem 'selenium-webdriver'
end
require 'capybara'
session = Capybara::Session.new(:selenium_headless)
session.visit('https://google.com')
session.click_on('test')
https://www.rubydoc.info/gems/capybara/Capybara/Session