How to get browser name using Selenium WebDriver with Java?
Asked Answered
M

6

18

I have a test case and need to execute based on the browser name i.e. IE or Chrome. In this test case some part will depend on browser type.

How will I get the browser name in between the execution? Example if it is IE, I need to pass the data. If it is Chrome browser, I need to select the data.

Mamey answered 7/2, 2016 at 19:33 Comment(0)
L
36

You can use below code to know browser name, version and OS details:-

    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);

packages you need to import

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

OR

   Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();

    String browserName = cap.getBrowserName();
    String browserVersion = (String)cap.getCapability("browserVersion");
    String osName = Platform.fromString((String)cap.getCapability("platformName")).name().toLowerCase();

    return browserName + browserVersion + "-" + osName;

Hope it will help you :)

Loon answered 8/2, 2016 at 5:52 Comment(4)
If the OP had (one or more) driver instances, why would they need code to work out the name so that only then could they switch on it? Where is the instance going to come from in the first place?Koodoo
OP can use this code on the testcases where he need to know the version and browser. OP can change the driver instance name. I just answered whatever OP has asked for.Loon
That doesn't make sense to me - but it's OP's choice.Koodoo
How can i get the browser details using EventFiringWebDriver (c#) ? I'm getting "Invalid cast" exception while trying to cast EventFiringWebDriver to RemoteWebDriverAutopsy
R
8

In Python, you may access the driver.capabilities dict like this

driver.capabilities['browserName']

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

Risotto answered 25/7, 2016 at 16:10 Comment(1)
You've written the wrong name for the dict: it's called driver.capabilities["name_of_attribute"] If you plural it it works great.Olive
A
2

To retrieve the Browser Name , Browser Version and Platform Name you can use either of the following approaches:

  • Using the API directly:

    • Code Block:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.remote.RemoteWebDriver;
      
      public class browserCapabilitiesRetrieve {
      
          public static void main(String[] args) {
      
              // initial configuration
              System.out.println("Browser Name is : "+((RemoteWebDriver) driver).getCapabilities().getBrowserName().toLowerCase());
              System.out.println("Browser Version is : "+((RemoteWebDriver) driver).getCapabilities().getVersion().toString());
              System.out.println("Platform Name is : "+((RemoteWebDriver) driver).getCapabilities().getPlatform().toString());
              driver.quit();
          }
      }
      
  • Using the Capabilities object and getCapability() method:

    • Code Block:

      import org.openqa.selenium.Capabilities;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.remote.RemoteWebDriver;
      
      public class FirefoxBrowserCapabilitiesRetrieve_getCapability {
      
          public static void main(String[] args) {
      
              // initial configuration
              Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
              System.out.println("Browser Name is : "+cap.getBrowserName());
              System.out.println("Browser version is : "+cap.getVersion());           
              System.out.println("Platform is : "+cap.getPlatform().toString());
              driver.quit();
          }
      }
      
Ache answered 23/5, 2020 at 21:50 Comment(0)
F
1

For those using C# you can do the following to detect browser when using either the local browser driver or remotewebdriver:

        public static bool IsSafari(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "safari";
            // Using local browser driver
            return driver.GetType() == typeof(SafariDriver);
        }

        public static bool IsInternetExplorer(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "internet explorer";
            // Using local browser driver
            return driver.GetType() == typeof(InternetExplorerDriver);
        }

        private static string GetRemoteDriverBrowserName(IWebDriver driver)
        {
            return ((RemoteWebDriver)driver).Capabilities.GetCapability("browserName").ToString().ToLower();
        }
Fibroma answered 14/9, 2020 at 16:59 Comment(0)
K
0

You're the tester, so it's up to you to write code/scripts to explicitly test each of the various browser/version combinations and their various nuances and subtleties (whilst trying to reuse as much logic as you can, minimise duplication etc.)

The nature of WebDriver is that you, the tester, are doing the driving - not the browser. Don't try to detect things.

So given that you have different behaviour for IE and for Chrome, you should explicitly create a WebDriver instance for each (in different @Tests) and set up the required data (likewise properties, Capabilities etc.) as appropriate.

By all means share common lookup code between the tests, but until your tests are robust and working you shouldn't try to refactor them.

Koodoo answered 7/2, 2016 at 19:39 Comment(2)
While you have a point, IMHO there are cases where it is better if the tests automatically adapt to different browsers. For example: tests are often run (semi)manually against different browsers; or one browser requires a work around for a Selenium bug when interacting with certain elements. For these cases you don't want separate tests, you want one test that works across browsers.Accountable
Different browsers require slight adjustments to code, and detecting which browser is being used enables your code to be more intelligent. For instance currently Safari requires most clicks to be executed via javascript, so detecting whether it's safari and altering how a click is performed via a selenium extension is the sensible approach. Some browsers deal with window switching slightly differently as well as of today's date, logic based on browser type is then also required.Fibroma
B
0

To list all capabilities in Java you can use:

import org.openqa.selenium.remote.RemoteWebDriver;

for (String s : ((RemoteWebDriver) driver).getCapabilities().getCapabilityNames()) {
    System.out.println(s + " :: " + ((RemoteWebDriver) driver).getCapabilities().getCapability(s));
}
Baldridge answered 14/6, 2022 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.