0

I have a Selenium project using Java that uses different browsers. I'm trying to introduce MS Edge, but having trouble using the getVersion() method from Capabilities. Below is a code snippet from a method that initializes the browser. WebDriver "driver" is declared at the beginning of the class.

            if(strBrowser.equalsIgnoreCase("Edge"))
        {
            String FileName=m1.get("TOOLS_PATH").toString()+"//MicrosoftWebDriver.exe";
            System.out.println("Full path to Edge executable: " + FileName);

            File edgeDriver = new File(FileName);
            System.setProperty("webdriver.edge.driver", edgeDriver.getAbsolutePath());  

            DesiredCapabilities caps = DesiredCapabilities.edge();

            driver = new EdgeDriver(caps);  //launches Edge browser


        }


        Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();           
        String browserName = caps.getBrowserName();
        String browserVersion = caps.getVersion().toString();

During debugging in ecliipse, when I hover on the 'caps' object after initializing it, it shows me the following: Capabilities [{acceptSslCerts=true, browserVersion=25.10586.672.0, platformVersion=10, browserName=MicrosoftEdge, takesScreenshot=true, pageLoadStrategy=normal, takesElementScreenshot=true, platformName=windows, platform=ANY}]

The string browserName comes out to be "MicrosoftEdge", but browserVersion comes out empty. I'd expect that it'd come out to be "25.10586.672.0"

2 Answers 2

0

Try the below approach

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(, browser_version.indexOf("."));
    return browsername + " " + browserversion;
Sign up to request clarification or add additional context in comments.

Comments

0

In firefox and Edge you need to specify the field name that you require its value - so please do like this:

Capabilities capabilities = ((RemoteWebDriver) webDriver).getCapabilities();
String browserVersion = capabilities.getCapability("browserVersion").toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.