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

I run the Watir / FireWatir scripts to use watir-webdriver and need the tools in watir-webdriver to determine which browser type the current test is running in (IE, FF, Chrome).

When Watir / FireWatir, looking at the browser class, returns either "Watir :: IE" or "FireWatir: Firefox". Using this code can be forked to execute specific browser code.

In watir-webdriver, the browser class is always "Watir :: Browser", it does not change when starting IE, Firefox or Chrome.

Does anyone know of a way to work in Ruby using the watir-web driver to identify the type of browser (e.g. IE, Firefox, Chrome)?

For example: using the Watir / Firewatir methods:

  def is_ie? ()

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


Then call 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

+6
source share
2 answers

Try

browser.driver.browser # =>: firefox

+9
source

Thank you, this is what I need!

Since I have a transition with some scripts ported to Watir-WebDriver, and some of them still need to be run under Watir / Firewatir, I updated the mt method as follows, placing them in case someone else ends up 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

0
source

Source: https://habr.com/ru/post/895504/


All Articles