0

我正在使用 Watir-webdriver 并且在我的测试机器上同时拥有 IE 32 位和 IE 64 位。当我使用创建浏览器实例时

browser = Watir::Browser.new :ie

它默认打开 64 位 IE(这本身很有趣,因为它不是默认浏览器)。我希望能够以编程方式指定我所针对的 IE 版本(32 位或 64 位)。这可能吗,如果可以,怎么做。

4

1 回答 1

2

Background

Selenium-webdriver, which is what Watir-webdriver is using, determines the version to use by:

"The driver supports running 32-bit and 64-bit versions of the browser. The choice of how to determine which "bit-ness" to use in launching the browser depends on which version of the IEDriverServer.exe is launched. If the 32-bit version of IEDriverServer.exe is launched, the 32-bit version of IE will be launched. Similarly, if the 64-bit version of IEDriverServer.exe is launched, the 64-bit version of IE will be launched." - http://code.google.com/p/selenium/wiki/InternetExplorerDriver

Presumably you have the 64bit version installed.

Solution

Selenium-webdriver is hardcoded to the file IEDriverServer.exe. The quickest solution might be to override this. Try the following:

  • Download/extract both of the IEDriverServer for 32bit and 64bit versions
  • Rename them to IEDriverServer32.exe and IEDriverServer64.exe respectively
  • Ensure that both are in your path somewhere
  • Use the following code to specify which version to use

This is a pretty quick hack. I am sure is a more elegant solution.

require 'rubygems'
require 'watir-webdriver'

#Specify your version - 32bit or 64bit
version = '32bit'

#Determine the binary to use
case version
    when '32bit'
        $ie_binary = 'IEDriverServer32'
    when '64bit'
        $ie_binary = 'IEDriverServer64'
    else
        raise( "Invalid version - #{version}" )
end

#Override the method that determines the binary to get
module Selenium
    module WebDriver
        module IE
            class Server
                def self.get
                    binary = Platform.find_binary($ie_binary)
                    if binary
                        new(binary)
                    else
                        raise Error::WebDriverError,
                        "Unable to find standalone executable. Please download the IEDriverServer from http://code.google.com/p/selenium/downloads/list and place the executable on your PATH."
                    end
                end
            end
        end
    end
end

browser = Watir::Browser.new :ie
#=> Browser of your desired IE version should launch

Update - Add a bit option

So that you can more nicely determine which browser to use, you can add a 'bit option' when creating an IE window. This is nicer than above, though still hacky (ie global variable).

require 'rubygems'
require 'watir-webdriver'

module Selenium
    module WebDriver
        module IE
            class Bridge
                alias_method :old_initialize, :initialize
                def initialize(opts = {})
                    $ie_binary = ''
                    case opts.delete(:bit)
                        when '32'
                            $ie_binary = 'IEDriverServer32'                     
                        when '64'
                            $ie_binary = 'IEDriverServer64'
                        else
                            $ie_binary = 'IEDriverServer'
                    end

                    old_initialize(opts)
                end
            end
        end
    end
end

module Selenium
    module WebDriver
        module IE   
            class Server
                def self.get                
                    binary = Platform.find_binary($ie_binary)
                    if binary
                        new(binary)
                    else
                        raise Error::WebDriverError,
                        "Unable to find standalone executable. Please download the IEDriverServer from http://code.google.com/p/selenium/downloads/list and place the executable on your PATH."
                    end
                end
            end
        end
    end
end

#Open and close a IE 32bit browser
browser = Watir::Browser.new :ie, {:bit => '32'}
browser.close

#Open and close a IE 64bit browser
browser = Watir::Browser.new :ie, {:bit => '64'}
browser.close
于 2012-08-01T14:56:30.143 回答