-1

I need to write 3 methods using java for determining what the browser using as a default in 3 operation systems: Windows, Linux and Mac. I guess, I need to use registry for windows as this example But how to do it in Linux? I need something like

System.out.println(getBrowserForLinux())
//Chrome
System.out.println(getBrowserVersionForLinux())
//79
Siegmund
  • 1
  • 1
  • Try this. https://stackoverflow.com/questions/5916900/how-can-you-detect-the-version-of-a-browser – GoharSahi Feb 03 '20 at 19:03
  • There's a selenium tag on your question but you've not stated anything of the sort in your question. Please make it clear if you are using selenium or not. – Kevin Raoofi Feb 03 '20 at 19:36

1 Answers1

0

You can modify this code (using selenium) to get what you want

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”;
}
}
}
  • Is it possible to determine the browser version before initiating the driver. ? – sam Dec 04 '20 at 21:26
  • i guess in the init method https://userpages.umbc.edu/~emurian/learnJava/swing/tutor/v2/explanations/Explain21.html#:~:text=The%20term%20init()%20is,to%20use%20in%20its%20statements. – Andres Navarro Dec 06 '20 at 22:46