如何在纯Ruby(没有Rails)中使用Capybara?

时间:2023-01-23 20:44:53

I'm trying to get Capybara running in a simple Ruby script -- i.e. without/outside of Rails. Here's the script:

我试图让Capybara运行在一个简单的Ruby脚本中 - 即没有/不在Rails之外。这是脚本:

require 'rubygems'
require 'capybara'
require 'capybara/dsl'

include Capybara

Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'

visit('/')

The problem is that when I run this I get this error:

问题是,当我运行这个时,我收到此错误:

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 

What am I doing wrong?

我究竟做错了什么?

Some more info:

更多信息:

  • Mac OS X 10.5
  • Mac OS X 10.5
  • ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
  • ruby 1.8.6(2009-06-08 patchlevel 369)[universal-darwin9.0]
  • capybara (0.3.9)
  • 水豚(0.3.9)

Thanks!

谢谢!

Neal

尼尔

Note: Per the comment from jnicklas I tried this, which matches the new README more closely:

注意:根据jnicklas的评论我试过这个,它更接近地匹配新的README:

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

Unfortunately, I'm still seeing the same error:

不幸的是,我仍然看到同样的错误:

NameError: uninitialized constant Capybara::Session

Thoughts?

思考?

Thanks!

谢谢!

3 个解决方案

#1


28  

Here's something that seems to work for me:

这似乎对我有用:

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

#2


5  

It goes to show that even incorrect documentation lives forever. The Capybara README used to recommend to include Capybara in the global namespace, this is a really bad idea, and messes up any number of random things. You should include Capybara in your own module or class and use that instead.

它表明,即使不正确的文档永远存在。 Capybara README曾经建议将Capybara包含在全局命名空间中,这是一个非常糟糕的主意,并且会乱搞任意数量的随机事物。您应该在自己的模块或类中包含Capybara并使用它。

Check out the README for current best practices.

查看README以了解当前的最佳实践。

#3


2  

Please check this CapybaraRspec101 example and fork it.

请检查此CapybaraRspec101示例并进行分叉。

It's a small example for acceptance tests on http://www.hi5.com using from scratch:

这是在http://www.hi5.com上从头开始验收测试的一个小例子:

  • Capybara
  • 水豚
  • Rspec
  • Rspec的
  • Selenium-webdriver
  • 硒的webdriver

All instructions are in the repo

所有说明都在回购中

#1


28  

Here's something that seems to work for me:

这似乎对我有用:

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

#2


5  

It goes to show that even incorrect documentation lives forever. The Capybara README used to recommend to include Capybara in the global namespace, this is a really bad idea, and messes up any number of random things. You should include Capybara in your own module or class and use that instead.

它表明,即使不正确的文档永远存在。 Capybara README曾经建议将Capybara包含在全局命名空间中,这是一个非常糟糕的主意,并且会乱搞任意数量的随机事物。您应该在自己的模块或类中包含Capybara并使用它。

Check out the README for current best practices.

查看README以了解当前的最佳实践。

#3


2  

Please check this CapybaraRspec101 example and fork it.

请检查此CapybaraRspec101示例并进行分叉。

It's a small example for acceptance tests on http://www.hi5.com using from scratch:

这是在http://www.hi5.com上从头开始验收测试的一个小例子:

  • Capybara
  • 水豚
  • Rspec
  • Rspec的
  • Selenium-webdriver
  • 硒的webdriver

All instructions are in the repo

所有说明都在回购中