Skip to content

Instantly share code, notes, and snippets.

@kodiyan
Last active August 29, 2015 14:03
Show Gist options
  • Save kodiyan/d23a175b51bbb7a21628 to your computer and use it in GitHub Desktop.
Save kodiyan/d23a175b51bbb7a21628 to your computer and use it in GitHub Desktop.
Get Browser and Version
package com.main;
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;
}
}
@kodiyan
Copy link
Author

kodiyan commented Aug 7, 2014

This is to get browser name and browser version with Selenium Web driver. This will return for browsers with browser name and version name like Firefox 29, Internet Explorer 08, Chrome 35.
This code is done with the help of java script and selenium web driver to display the version number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment