Selenium RC:自动在多个浏览器中运行测试
-
03-07-2019 - |
题
所以,我已经开始创建一些使用 Selenium RC 进行测试的Ruby单元测试我的网络应用程序直接在浏览器中。我正在使用 Selenum-Client 获取ruby。我已经为我继承的所有其他selenium测试创建了一个基类。
这会创建大量SeleniumDriver实例,并在每个实例上调用所有缺少的方法。这基本上是并行运行测试。
其他人如何自动化?
这是我的实施:
class SeleniumTest < Test::Unit::TestCase
def setup
@seleniums = %w(*firefox *iexplore).map do |browser|
puts 'creating browser ' + browser
Selenium::SeleniumDriver.new("localhost", 4444, browser, "http://localhost:3003", 10000)
end
start
open start_address
end
def teardown
stop
end
#sub-classes should override this if they want to change it
def start_address
"http://localhost:3003/"
end
# Overrides standard "open" method
def open(addr)
method_missing 'open', addr
end
# Overrides standard "type" method
def type(inputLocator, value)
method_missing 'type', inputLocator, value
end
# Overrides standard "select" method
def select(inputLocator, optionLocator)
method_missing 'select', inputLocator, optionLocator
end
def method_missing(method_name, *args)
@seleniums.each do |selenium_driver|
if args.empty?
selenium_driver.send method_name
else
selenium_driver.send method_name, *args
end
end
end
end
这样可行,但是如果一个浏览器失败,整个测试失败,无法知道哪个浏览器失败了。
解决方案
您是否尝试过 Selenium Grid ?我认为它创建了非常好的摘要报告,显示了您需要的详细信息。我可能错了,因为我没有使用它很长一段时间。
其他提示
我最终修改了Selenium的protocol.rb以使用 @browser_string
和来自Selenium RC的消息返回来引发 AssertionFailedError
响应并非以“OK”开头。我还修改了 http_post
方法以返回整个响应体和 method_missing
,以返回一个返回值数组,用于向Selenium RC发出get_X命令。
将此代码添加到问题中的代码,您应该能够看到哪些断言在哪些浏览器上失败。
# Overrides a few Driver methods to make assertions return the
# browser string if they fail
module Selenium
module Client
class Driver
def remote_control_command(verb, args=[])
timeout(default_timeout_in_seconds) do
status, response = http_post(http_request_for(verb, args))
raise Test::Unit::AssertionFailedError.new("Browser:#{@browser_string} result:#{response}") if status != 'OK'
return response[3..-1]
end
end
def http_post(data)
http = Net::HTTP.new(@server_host, @server_port)
response = http.post('/selenium-server/driver/', data, HTTP_HEADERS)
#return the first 2 characters and the entire response body
[ response.body[0..1], response.body ]
end
end
end
end
#Modify your method_missing to use seleniums.map to return the
#results of all the function calls as an array
class SeleniumTest < Test::Unit::TestCase
def method_missing(method_name, *args)
self.class.seleniums.map do |selenium_driver|
selenium_driver.send(method_name, *args)
end
end
end
免责声明:不是硒专家。
您只是想知道哪个浏览器失败了,或者您想要在所有浏览器上运行测试,然后在完成后报告总失败?
如果您在设置中通过哈希存储驱动程序,前者非常简单。 (我确信有一种花哨的方法可以用Hash.inject做到这一点,但我很懒。)
@seleniums = {}
%w(*firefox *iexplore).each do |browser|
puts 'creating browser ' + browser
@seleniums[browser] = Selenium::SeleniumDriver.new("localhost", 4444, browser, "http://localhost:3003", 10000)
end
然后更改核心功能以修改异常以包含正在使用的驱动程序的名称,例如:
@seleniums.each do |name, driver|
begin
driver.send method_name, *args
rescue Exception => ex
raise ex.exception(ex.message + " (in #{name})")
end
end
应该让你亲近。
您需要独立对待每个测试。因此,如果一个测试失败,它将继续测试其他测试。查看 phpunit and selenium rc