How to get browser name using Selenium WebDriver with Java?

前端 未结 5 926
情深已故
情深已故 2020-12-31 09:50

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 brows

相关标签:
5条回答
  • 2020-12-31 09:59

    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.

    0 讨论(0)
  • 2020-12-31 10:10

    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();
            }
        }
        
    0 讨论(0)
  • 2020-12-31 10:11

    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();
            }
    
    0 讨论(0)
  • 2020-12-31 10:17

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

    0 讨论(0)
  • 2020-12-31 10:21

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

    driver.capabilities['browserName']
    

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

    0 讨论(0)
提交回复
热议问题