Ruby 我如何在Watir中为不同的测试用例使用相同的浏览器实例?

Ruby 我如何在Watir中为不同的测试用例使用相同的浏览器实例?,ruby,watir,watir-webdriver,Ruby,Watir,Watir Webdriver,我想对两个测试用例使用相同的浏览器。我不想为每个案例都打开一个新的浏览器 require File.expand_path("..",Dir.pwd)+"/TestHelper" class Validations < Test::Unit::TestCase def initializer browser = self.getBrowser if browser.nil? p "browser nil" helper =

我想对两个测试用例使用相同的浏览器。我不想为每个案例都打开一个新的浏览器

require File.expand_path("..",Dir.pwd)+"/TestHelper"

class Validations < Test::Unit::TestCase

    def initializer
      browser = self.getBrowser
      if browser.nil?
        p "browser nil"
        helper = TestHelper.new
        email = helper.getCurrentEmailAddress
        url = UtilConstants::HOME_PAGE+UtilConstants::SIGN_UP
        browser = helper.getExistingBrowser
        if browser.nil?
          browser = helper.getBrowser
          browser.goto url
        end
      end
      return browser
    end

    def test_email_field_validation
      #code 
    end

    def test_password_field_validation
     #code
    end
end

您应该使用实例变量,即@browser。在initializer中创建一次,并在内部使用如下方法:

def test_email
    email_elem = @browser.element(:id => "email")
    assertTrue(email_elem.exists?)
    ...
end

我建议看一下上的Test::Unit示例。它展示了如何在内置的设置方法中创建一个记忆化的浏览器对象,rspec也有一个类似的例子。