SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Get Browser And OS Details On Run-Time In Selenium WebDriver

Earlier we already learnt how to execute selenium WebDriver test In different browsers like Mozilla Firefox (VIEW THIS POST), Google Chrome (VIEW THIS POST) and IE (VIEW THIS POST) browsers. Sometimes you also need to know browser and OS name and version where your selenium WebDriver tests are running. Supposing you are running your tests on cloud then you
must aware In which OS and browsers your tests are running. Is It possible to get browser and OS name and version on run-time?

Something like this where you don't know browser and OS version and you wants to know It to reproduce some Issues manually which are generated during automation test execution. You need to add some extra code In your selenium WebDriver test to get browser and OS detail where your test Is running. Bellow given code will get these details for you.

//Get Browser name and version.
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();
  
//Get OS name.
String os = System.getProperty("os.name").toLowerCase();

Full example to get browser and OS detail for different browser Is as bellow. Execute It In eclipse IDE and verify console result.


package STTA.MavenProject1;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;

public class GetBrowserDetail {

 WebDriver driver;

 @BeforeTest
 public void setup(){

  driver = new FirefoxDriver();
  //Check and print Firefox browser and OS detail.
  CheckBrowserOS();
  driver.close();
  
  System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
  driver = new ChromeDriver();
  //Check and print Chrome browser and OS detail.
  CheckBrowserOS();
  driver.close();
  
  System.setProperty("webdriver.ie.driver", "D://IEDriverServer.exe");
  driver = new InternetExplorerDriver();
  //Check and print IE browser and OS detail.
  CheckBrowserOS();
  driver.close();
 }

 
 public void CheckBrowserOS() {
  //Get Browser name and version.
  Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
  String browserName = caps.getBrowserName();
  String browserVersion = caps.getVersion();
  
  //Get OS name.
  String os = System.getProperty("os.name").toLowerCase();
  System.out.println("OS = " + os + ", Browser = " + browserName + " "+ browserVersion);
 } 
}

At the end of test execution, Test execution result will looks like bellow. It will show you Mozilla Firefox, Google Chrome, IE browser and Its OS version.

This Is way to get browser and OS detail In selenium WebDriver.

No comments:

Post a Comment