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

2

7

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

Ress answered 20/8, 2011 at 20:37 Comment(0)
E
10

Try

browser.driver.browser #=> :firefox

Exogenous answered 21/8, 2011 at 1:17 Comment(1)
^ that link is broken and links to malwareKallick
R
0

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

Ress answered 23/8, 2011 at 14:10 Comment(1)
You should mark Jarib's answer above as answering your question thenMephitis

© 2022 - 2024 — McMap. All rights reserved.