How to get browser name using Selenium WebDriver with Java?

I have a test case and need to run it based on the browser name, i.e. IE or Chrome. In this case, some parts will depend on the type of browser.

How do I get the browser name between execution? For example, if it is IE, I need to pass data. If it's a Chrome browser, I need to select the data.

+7
java selenium-webdriver
source share
3 answers

You can use the code below to find out the browser name, version and OS information: -

Capabilities cap = ((RemoteWebDriver) driver).getCapabilities(); String browserName = cap.getBrowserName().toLowerCase(); System.out.println(browserName); String os = cap.getPlatform().toString(); System.out.println(os); String v = cap.getVersion().toString(); System.out.println(v); 

to be imported

 import org.openqa.selenium.Capabilities; import org.openqa.selenium.remote.RemoteWebDriver; 

Hope this helps you :)

+14
source share

In Python, you can access driver.capabilities dict like this

 driver.capabilities['browserName'] 

https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8

+3
source share

You are a tester, so you need to write code / scripts explicitly to check each of the various browser / version combinations and their various nuances and subtleties (when trying to reuse the logic, as you can, minimize duplication, etc.)

The nature of WebDriver is that you , the tester, are the manager, not the browser. Do not try to discover things.

Thus, given that you have different behavior for IE and Chrome, you must explicitly create a WebDriver instance for each (in different @Test s) and configure the necessary data (similar to properties, Capabilities , etc.). ) as needed.

In any case, use a common search code between tests, but until your tests are reliable and do not work, you should not try to reorganize them.

+2
source share

All Articles