Home Code How to get System OS, Browser and Version using Java and Selenium...

How to get System OS, Browser and Version using Java and Selenium WebDriver

This is to get browser name and browser version with Selenium Web driver. This will return browser name and version name like Firefox 29, Internet Explorer 08, Chrome 35.

This is done with javascript and selenium web driver to display the version number. OSDetector returns Operating system name for Windows, MAC, Linux and Solaris.

Related: 10 Best Text Editors for Windows, Linux and Mac.

Detect OS, Browser and Version using Java and Selenium WebDriver


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

public class BrowserVersion {
private static WebDriver browserDriver;

public static String getBrowserAndVersion() {
String browser_version = null;
Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities();
String browsername = cap.getBrowserName();
// This block to find out IE Version number
if (“internet explorer”.equalsIgnoreCase(browsername)) {
String uAgent = (String) ((JavascriptExecutor) browserDriver).executeScript(“return navigator.userAgent;”);
System.out.println(uAgent);
//uAgent return as “MSIE 8.0 Windows” for IE8
if (uAgent.contains(“MSIE”) && uAgent.contains(“Windows”)) {
browser_version = uAgent.substring(uAgent.indexOf(“MSIE”)+5, uAgent.indexOf(“Windows”)-2);
} else if (uAgent.contains(“Trident/7.0”)) {
browser_version = “11.0”;
} else {
browser_version = “0.0”;
}
} else
{
//Browser version for Firefox and Chrome
browser_version = cap.getVersion();// .split(“.”)[0];
}
String browserversion = browser_version.substring(0, browser_version.indexOf(“.”));
return browsername + ” ” + browserversion;
}

public static String OSDetector () {
String os = System.getProperty(“os.name”).toLowerCase();
if (os.contains(“win”)) {
return “Windows”;
} else if (os.contains(“nux”) || os.contains(“nix”)) {
return “Linux”;
}else if (os.contains(“mac”)) {
return “Mac”;
}else if (os.contains(“sunos”)) {
return “Solaris”;
}else {
return “Other”;
}
}
}

Disclaimer: MashTips is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission at no extra cost to you.

4 COMMENTS

  1. Use simple code for getting browser version
    C# selenium -:

    ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
    strBrowserVersion = capabilities.Version.ToString();
    if (strBrowserVersion == "")
    {
    strBrowserVersion = capabilities.GetCapability("browserVersion").ToString();
    }

  2. When I declare “private static WebDriver browserDriver;”.It’s giving null pointer exception in “Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities()” this line.When I debug the code the browserDriver is showing as null.That’s why it is giving that exception.Can anyOne help me?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free Newsletter

Read More...

How to Combine Videos on iPhone and iPad

Video editing on your smartphone is not a big deal these days with plenty of apps in the market. Even the iOS 12 and...

How to Save Instagram Videos on Windows, iPhone or Android

Even though Instagram is primarily used for sharing photos, videos (especially IGTV videos) are also equally popular among users. At times, there might be...

12 Best Free Android Calculator Apps for Students & Professionals

Android OS for your phone and tablet coming with a simple and a scientific calculator for day to day uses. But if you are...

How to Connect Bluetooth Adapter to Car Audio System?

Bluetooth FM Transmitter car kit lets you make hands-free calls and play songs direct from smartphone to your Car Stereo. There are Bluetooth car kits...

8 Best Malware Removal Tools for Windows and Mac

The new digital era that we now in demands utmost security and reliability for the systems we use. Whether you have Windows or Mac...

How To Get A Free US Phone Number to Call US and Canada?

Are you going to move to a different city or state? Well, you may require a local phone number with the new area code for...
%d bloggers like this: