Joe
Joe

Reputation: 71

How to determine browser type (IE, FF, Chrome, etc.)

I'm in the process of switching my Watir / FireWatir scripts over to use watir-webdriver and need a means in watir-webdriver to determine which type of browser the test is currently being executed against, (IE, FF, Chrome).

With Watir / FireWatir looking at the class of the browser would return either "Watir::IE" or "FireWatir:Firefox". Using that the code could be branched to execute browser specific code.

In watir-webdriver, the class of the browser is always "Watir::Browser", it doesn't vary when running IE, Firefox, or Chrome.

Does anyone know of a way in Ruby with watir-web-driver to identify the browser's type (i.e. IE, Firefox, Chrome)?

For example: With Watir / Firewatir define methods:

def is_ie?()

return self.class.to_s == "Watir::IE"
end
def is_firefox?()
return self.class.to_s == "FireWatir::Firefox"
end

Then invoke them like this...

if(browser.is_ie?)

# run the IE specific code
end
if(browser.is_firefox?)
# run the firefox specific code
end



Thanks in advance,
Joe

Upvotes: 7

Views: 2256

Answers (2)

Joe
Joe

Reputation: 1

Thanks that is just what I needed!

As I'm in a transition with some scripts ported over to Watir-WebDriver and some still needing to run under Watir / Firewatir I've updated mt method as follows, posting them in case someone else is in the same situation.

def is_chrome?()

if(is_webdriver? == true)
  return (self.driver.browser.to_s.downcase == "chrome")
else
    return (self.class.to_s == "ChromeWatir::Browser")
end

end

def is_firefox?()

if(is_webdriver? == true)
  return (self.driver.browser.to_s.downcase == "firefox")
else
    return (self.class.to_s == "FireWatir::Firefox")
end

end

def is_ie?()

if(is_webdriver? == true)
  return (self.driver.browser.to_s.downcase == "internet_explorer")
else
    return (self.class.to_s == "Watir::IE")
end

end

def is_webdriver?()

  if($LOADED_FEATURES.to_s =~/watir-webdriver/)
    return true
  else
    return false
  end

end

Upvotes: 0

jarib
jarib

Reputation: 6058

Try

browser.driver.browser #=> :firefox

Upvotes: 10

Related Questions