Selenium RC:自动在多个浏览器中运行测试

8

因此,我开始创建一些使用Selenium RC的Ruby单元测试来直接在浏览器中测试我的Web应用程序。我正在使用Ruby的Selenum-Client。我已经为所有其他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

这种方法是可行的,但如果一个浏览器失败了,整个测试就会失败,并且无法知道是哪个浏览器失败了。


1
嗨,丹尼尔,我有一个类似的问题。我想知道你是否能帮忙。Selenium RC:如何使用多个浏览器启动交互式测试 - onesith
4个回答

4

你试过Selenium Grid吗?我认为它可以创建一个相当好的概要报告,显示你需要的细节。可能我错了,因为我已经有一段时间没有使用它了。


1

我最终修改了Selenium的protocol.rb文件,如果响应不以“OK”开头,则会引发一个AssertionFailedError,其中包括@browser_string和返回的消息。 我还修改了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   

0

你需要独立对待每一个测试。所以如果一个测试失败了,它会继续测试其他测试。查看phpunit和selenium rc


0

免责声明:本人不是Selenium专家。

你只是想知道哪个浏览器失败了,还是想在所有浏览器上运行测试,然后在完成后报告总的失败情况?

如果你在设置中使用哈希存储驱动程序,前者就非常简单。(我相信有一种花里胡哨的方法可以使用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

这应该能让你接近。


好主意,但我认为失败的测试不一定会抛出异常。 - Daniel Beardsley
实际上,它们应该始终抛出某种AssertionFailedException; 但下面那个Grid东西看起来超级酷。 - Dan Fitch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接