Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Selenium RC:在多个浏览器中自动运行测试_Ruby On Rails_Ruby_Unit Testing_Selenium_Automated Tests - Fatal编程技术网

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

Ruby on rails Selenium RC:在多个浏览器中自动运行测试,ruby-on-rails,ruby,unit-testing,selenium,automated-tests,Ruby On Rails,Ruby,Unit Testing,Selenium,Automated Tests,因此,我开始创建一些Ruby单元测试,用于直接在浏览器中测试我的web应用程序。我用的是ruby。我已经为所有其他selenium测试创建了一个基类,以便从中继承 这将创建大量SeleniumDriver实例,并对每个实例调用缺少的所有方法。这实际上是并行运行测试 其他人是如何实现自动化的 这是我的实现: class SeleniumTest < Test::Unit::TestCase def setup @seleniums = %w(*firefox *iexplore)

因此,我开始创建一些Ruby单元测试,用于直接在浏览器中测试我的web应用程序。我用的是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专家

您只是想知道哪个浏览器失败了,还是想跨所有浏览器运行测试,然后在测试完成后报告全部失败

如果在设置中通过散列存储驱动程序,则前者非常简单。我相信用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

应该能让你接近。

你试过了吗?我认为它创建了非常好的总结报告,其中显示了您需要的详细信息。我可能错了,因为我有很长一段时间没有使用它。

我最终修改了Selenium的protocol.rb,如果响应不是以OK开头的,则使用@browser\u字符串和Selenium RC返回的消息来引发AssertionFailedError。我还修改了http_post方法以返回整个响应体,修改了方法_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   

您需要独立地处理每个测试。因此,如果一个测试失败,它将继续测试其他测试。检查一下好主意,虽然我认为失败的测试不一定会抛出异常。实际上,它们应该总是抛出某种断言FailedException;但是下面的网格看起来非常光滑。嗨,丹尼尔,我有一个类似的问题。我想知道你是否能帮忙。