我应该如何使用水豚测试硒应用程序?

发布于 2025-01-04 14:14:29 字数 1225 浏览 3 评论 0原文

这是我的问题:

我正在编写一个应用程序,使用 selenium-webdriver 连接到网站点击/填充一堆东西。

显然,我想测试我的代码...而这就是它变得困难的地方!我该怎么做?

这是我的测试:

require 'spec_helper'
require 'capybara/rspec'

module MyModule
  Capybara.run_server = false

  describe "the method", :type => :request do
    it "should open a browser and go to the site" do
      MyClass.open_site("http://google.com")
      page.has_content? "Google"
    end
  end
end

这是代码:

require 'selenium-webdriver'

module MyModule
  class MyClass
    def self.open_site(url)
      @driver = Selenium::WebDriver.for :firefox
      @driver.navigate.to url 
    end
  end
end

这是我收到的错误:

Failures:

  1) the method should open a browser and go to the site
     Failure/Error: page.has_content? "Google"
     ArgumentError:
       rack-test requires a rack application, but none was given
     # (eval):2:in `has_content?'
     # ./spec/integration/myclass_spec.rb:10

我可以理解测试很混乱,因为通常 Capybara 运行 Selenium 来访问网站并检查一切看起来都很好。但这里 Selenium 作为代码的一部分独立运行...

我如何告诉rack-test 使用正在运行的 Selenium 作为其应用程序?

Capybara 是测试这段代码的正确解决方案吗?

感谢您的帮助!

Here is my problem:

I am writing an application that uses selenium-webdriver to connect to a site a click/fill a bunch of things.

Obviously, I want to test my code... And this is where it gets difficult! How do I do this?

Here is my test:

require 'spec_helper'
require 'capybara/rspec'

module MyModule
  Capybara.run_server = false

  describe "the method", :type => :request do
    it "should open a browser and go to the site" do
      MyClass.open_site("http://google.com")
      page.has_content? "Google"
    end
  end
end

Here is the code:

require 'selenium-webdriver'

module MyModule
  class MyClass
    def self.open_site(url)
      @driver = Selenium::WebDriver.for :firefox
      @driver.navigate.to url 
    end
  end
end

Here is the error I am getting:

Failures:

  1) the method should open a browser and go to the site
     Failure/Error: page.has_content? "Google"
     ArgumentError:
       rack-test requires a rack application, but none was given
     # (eval):2:in `has_content?'
     # ./spec/integration/myclass_spec.rb:10

I can understand the test is confused because usually Capybara runs Selenium to vist a site and check that everything looks good. But here Selenium is running on its own as part of the code...

How could I tell rack-test to use the running Selenium as its application?

Is Capybara even the right solution to test this code?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2025-01-11 14:14:29

您正在使用的功能之一必须使用机架应用程序。

问题不应该出在这一行:

page.has_content? "google"

因为这对我来说用硒驱动程序工作得很好。我怀疑这是你设置驱动程序的方式。

我在运行水豚测试时遇到了类似的问题,直到我发现这些帖子:google groups

他们给了我一些指导来让我的测试运行。
最后我最终用这些行来配置我的测试。
我使用的是 Chrome,但 Internet Explorer 和 Firefox 也可以这样工作。

require 'selenium-webdriver'

Capybara.register_driver :selenium_ie do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.default_driver = :selenium_chrome

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

One of the functions that you are using must be using a rack application.

The problem should not be with the line:

page.has_content? "google"

because that works fine for me with selenium driver. I suspect it is the way you have set up the driver.

I had a similar problem with running my capybara tests until I discovered these posts:google groups

They gave me a few pointers to get my test running.
In the end I ended up having these lines to configure my tests.
I am using chrome but Internet explorer and firefox would work this way too.

require 'selenium-webdriver'

Capybara.register_driver :selenium_ie do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.default_driver = :selenium_chrome

Capybara.app_host = 'http://www.google.com'
只是在用心讲痛 2025-01-11 14:14:29

以下是我对运行 MongoDB 的 Sinatra 应用程序所做的操作。注释掉selenium代码以尝试不使用水豚的纯selenium。安装 test-unitcapybaracapybara-webkitselenium-webdriver gem。对于 capybara-webkit,请执行 brew install qt4 (mac) 或以其他方式安装 qt4。

require './app'
require 'test-unit'
require 'capybara'
require 'capybara-webkit'
require 'selenium-webdriver'

class IntegrationTest < Test::Unit::TestCase  
  include Capybara::DSL

  def setup
    # Clear database first
    MongoMapper.database.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)

    # For pure selenium: @b = Selenium::WebDriver.for :firefox
    # For pure selenium: @w = Selenium::WebDriver::Wait.new(:timeout => 15)

    Capybara.run_server = false
    Capybara.default_selector = :css
    Capybara.default_wait_time = 5
    Capybara.ignore_hidden_elements = false
    Capybara.javascript_driver = :webkit  # Comment out to use :selenium
    Capybara.default_driver = Capybara.javascript_driver
    Capybara.app = Sinatra::Application.new
    Capybara.app_host = "http://crowdfundhq.dev:3001"
    Capybara.server_port = 3001
  end

  def teardown
    # For pure selenium: @b.quit
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end

  def test_root
    # For pure selenium: @b.get "http://crowdfundhq.dev:3001"
    # For pure selenium: assert @b.page_source =~ /#pricing/

    # Change driver during test: Capybara.current_driver = Capybara.javascript_driver    

    visit("/")
    assert(page.body =~ /highlight/)
  end  
end

Here's what I did with my Sinatra application running MongoDB. Comment out the selenium code to try pure selenium without capybara. Install the test-unit, capybara, capybara-webkit, selenium-webdriver gems. For capybara-webkit do brew install qt4 (mac) or install qt4 another way.

require './app'
require 'test-unit'
require 'capybara'
require 'capybara-webkit'
require 'selenium-webdriver'

class IntegrationTest < Test::Unit::TestCase  
  include Capybara::DSL

  def setup
    # Clear database first
    MongoMapper.database.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)

    # For pure selenium: @b = Selenium::WebDriver.for :firefox
    # For pure selenium: @w = Selenium::WebDriver::Wait.new(:timeout => 15)

    Capybara.run_server = false
    Capybara.default_selector = :css
    Capybara.default_wait_time = 5
    Capybara.ignore_hidden_elements = false
    Capybara.javascript_driver = :webkit  # Comment out to use :selenium
    Capybara.default_driver = Capybara.javascript_driver
    Capybara.app = Sinatra::Application.new
    Capybara.app_host = "http://crowdfundhq.dev:3001"
    Capybara.server_port = 3001
  end

  def teardown
    # For pure selenium: @b.quit
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end

  def test_root
    # For pure selenium: @b.get "http://crowdfundhq.dev:3001"
    # For pure selenium: assert @b.page_source =~ /#pricing/

    # Change driver during test: Capybara.current_driver = Capybara.javascript_driver    

    visit("/")
    assert(page.body =~ /highlight/)
  end  
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文