我应该如何使用水豚测试硒应用程序?
这是我的问题:
我正在编写一个应用程序,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用的功能之一必须使用机架应用程序。
问题不应该出在这一行:
因为这对我来说用硒驱动程序工作得很好。我怀疑这是你设置驱动程序的方式。
我在运行水豚测试时遇到了类似的问题,直到我发现这些帖子:google groups
他们给了我一些指导来让我的测试运行。
最后我最终用这些行来配置我的测试。
我使用的是 Chrome,但 Internet Explorer 和 Firefox 也可以这样工作。
One of the functions that you are using must be using a rack application.
The problem should not be with the line:
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.
以下是我对运行 MongoDB 的 Sinatra 应用程序所做的操作。注释掉selenium代码以尝试不使用水豚的纯selenium。安装
test-unit
、capybara
、capybara-webkit
、selenium-webdriver
gem。对于 capybara-webkit,请执行brew install qt4
(mac) 或以其他方式安装 qt4。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 dobrew install qt4
(mac) or install qt4 another way.