TransWikia.com

How to get browser name using Selenium WebDriver with Java?

Stack Overflow Asked by Swathi Chowdary on January 6, 2021

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.

5 Answers

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

Correct answer by Shubham Jain on January 6, 2021

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

Answered by Rushby on January 6, 2021

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

Answered by DebanjanB on January 6, 2021

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

driver.capabilities['browserName']

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

Answered by user7610 on January 6, 2021

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.

Answered by Andrew Regan on January 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP