How to get browser name using Selenium WebDriver with Java?

前端 未结 5 945
情深已故
情深已故 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 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();
            }
        }
        

提交回复
热议问题