代码之家  ›  专栏  ›  技术社区  ›  Daniel Beardsley

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

  •  8
  • Daniel Beardsley  · 技术社区  · 17 年前

    所以,我开始创建一些使用 Selenium RC 直接在浏览器中测试我的Web应用程序。我正在使用 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
    

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

    4 回复  |  直到 15 年前
        1
  •  4
  •   ya23 devoured elysium    17 年前

    你试过吗? Selenium Grid ?我认为它创建了很好的总结报告,显示了您需要的细节。我可能错了,因为我已经很久没用了。

        2
  •  1
  •   community wiki 2 revs Daniel Beardsley    17 年前

    我最后修改了Selenium的协议.rb来提高 AssertionFailedError 两者兼有 @browser_string 消息传回了 如果响应不是以“OK”开头,则来自Selenium RC。我也修改了 http_post 方法返回整个响应体和 method_missing 返回一个返回值数组,用于向SeleniumRC发出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   
    
        3
  •  0
  •   Dan Fitch    17 年前

    免责声明:不是硒专家。

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

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

    应该让你靠近。

        4
  •  0
  •   hkshambesh    16 年前

    你需要独立地对待每一个测试。因此,如果一个测试失败,它将进行其他测试。退房 phpunit and selenium rc

    推荐文章