0

I have multiple version of Firefox in multiple PCs where some has newest which is 47 and some has older version.

I have following this and setup RemoteWebDriver with Marionette, the next generation of FirefoxDriver to support Firefox version 47 for automation as below :-

URL server = new URL("http://localhost:4444/wd/hub")
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver webDriver = new RemoteWebDriver(server, capabilities);
webDriver.get("https://www.google.co.in");

It's working fine with Firefox version 47 but when I'm running same on other PCs where installed Firefox older version, it's giving exception as below :-

Caused by: org.openqa.selenium.remote.UnreachableBrowserException: Could not sta

rt a new session. Possible causes are invalid address of the remote server or br

owser start-up failure.

Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'

System info: host: 'com-PC', ip: '192.168.3.3', os.name: 'Windows 7', os.arch: '

x86', os.version: '6.1', java.version: '1.8.0_92'

Driver info: driver.version: MarionetteDriver

Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHost

ConnectException: Connect to localhost:3125 [localhost/127.0.0.1] failed: Connec

tion refused: connect

Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'

System info: host: 'com-PC', ip: '192.168.3.3', os.name: 'Windows 7', os.arch: '

x86', os.version: '6.1', java.version: '1.8.0_92'

Driver info: driver.version: MarionetteDriver

Caused by: java.net.ConnectException: Connection refused: connect

WARN - Exception: Connection refused: connect

When I removes line capabilities.setCapability("marionette", true); means removes MarionetteDriver support, it's working well with Firefox older version but raising exception with Firefox version 47 ie. UnreachableBrowserExcetion.

So my question is :-

Is there any way to know Firefox version or any othere solution by which I could run simultaneously with both Old and New version of Firefox??.

I want a generic solution where my code intelligently could be able to know when it will start with MarionetteDriver and when with FireFoxDriver on the basis of FireFox Version.

Thanks in advance...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73

2 Answers2

2

Set a boolean flag at the beginning of your test depending on if you want to run it on 43 or 47. Check the flag in the code where you're creating your driver, for example like this:

boolean useMarionette = true //false

URL server = new URL("http://localhost:4444/wd/hub")
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", useMarionette);
WebDriver webDriver = new RemoteWebDriver(server, capabilities);
webDriver.get("https://www.google.co.in");

Now if you have useMarionette set to true, it will run with marionette, if set to false, it will not.

If you really want to parse the windows registry for the available firefox version, here is a partial example for you:

How to check if a program is installed on Windows system

Use as follows:

public class Test {

    public static void main(String... args) throws Exception {

        RegistryKey firefoxKey;
        RegistryKey.initialize(Test.class.getResource("jRegistryKey.dll").getFile());
        RegistryKey key = new RegistryKey(RootKey.HKCU, "SOFTWARE\\Mozilla\\Mozilla Firefox");
        for (Iterator<RegistryKey> subkeys = key.subkeys(); subkeys.hasNext();) {
            firefoxKey = subkeys.next();

            if(firefoxKey.getName().contains("47") {
                 //marionette
            }
            //start browser with or without     marionette
        }
    } 
}
Community
  • 1
  • 1
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
  • I couldn't do this on the basis of flag.. I want more generic solution where my code intelligently could be able to know when it will run with `MarionetteDriver` and when with `FireFoxDriver`.. – Saurabh Gaur Jun 14 '16 at 12:29
  • The only way I see to do this in a generic way is to try and request a webbrowser and catch the exception, and on exception request the other. Which is extremely bad design. You should be in control of where you tests are running, it is your environment after all, so you should decide how the tests are going to run. – Mobrockers Jun 14 '16 at 12:38
  • Are you running the tests in a distributed selenium grid or starting them manually? – Mobrockers Jun 14 '16 at 12:38
  • I want to run same code with multiple machine where I don't know which version of firefox running in all machine – Saurabh Gaur Jun 14 '16 at 12:40
  • What you're asking is not possible then. You can only ask the grid to give you the browser you're looking for, you cannot ask it which browsers it has. – Mobrockers Jun 14 '16 at 12:45
  • Why is it not possible???.. the code will be run on the local machine but I could not modify the code on the basis on machine... but I can know which version of firefox is installed on that local machine because I'm using java.. – Saurabh Gaur Jun 14 '16 at 12:59
  • I have added a partial solution to find out the installed firefox version number... But I would really really not recommend doing this. – Mobrockers Jun 14 '16 at 13:16
-1

Now I have achieved this by following approach :-

public static String executeCommand(List<String> commands)
            throws IOException {
        ProcessBuilder builder = new ProcessBuilder(commands);
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        return r.readLine();
    }

//this command gives the current installed path of Firefox in c drive 
String[] getInstalledFirefoxDirectoryCmd = { "cmd.exe", "/c", "dir /s/b \"C:/firefox.exe\""};

String installedFirefoxLocation = executeCommand(Arrays.asList(getInstalledFirefoxDirectoryCmd));

//this command will give version of the installed Firefox
String[] getFirefoxVersionCmd = { "cmd.exe", "/c", "\"" + installedFirefoxLocation + "\" -v | more" };

String version = executeCommand(Arrays.asList(getFirefoxVersionCmd));

int version_int = Integer.parseInt(version.replace("Mozilla Firefox ", "").split("\\.")[0]);

URL server = new URL("http://localhost:4444/wd/hub")
DesiredCapabilities capabilities = DesiredCapabilities.firefox();

//use marionette if ff version equal or greater than 47
if(version_int >= 47) {
     capabilities.setCapability("marionette", true);
}

WebDriver webDriver = new RemoteWebDriver(server, capabilities);
webDriver.get("https://www.google.co.in");

By this generic way we can automate our test in multiple PCs with same code. No matter which version of Firefox installed on those Pcs.

Edited..

For Mac Pcs we can change the command to get version of installed Firefox as below :-

String installedFirefoxLocation = "/Applications/Firefox.app/Contents/MacOS/firefox"
// it's default location

String[] getFirefoxVersionCmd = { installedFirefoxLocation + " -v | more" };

If firefox is not installed in default location then we can provide Firefox binary location either by path or by System property because selenium does not work without providing Firefox binary location if it is installed in other location.

For checking OS we can use as below :-

String OS = System.getProperty("os.name").toLowerCase();

if(OS.indexOf("win") >= 0) {
//Means it's windows
}

if(OS.indexOf("mac") >= 0) {
//Means it's mac
}

...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    This seems unnecessary long and complex compared to the answer Mobrockers provided you earlier. The answer which you called too long. – RemcoW Jun 15 '16 at 15:43
  • 1
    This also does not work on any different OS, nor does it work on machines that have firefox installed in a different location. – RemcoW Jun 15 '16 at 15:45
  • @RemcoW here I'm not comparing which answer is better..for diffrent OS just need to change command line which I have provided in my edited answer...Have you tried ever to launch ff from different location using selenium??....90% `Pcs` has default location...and FYI if location is not default selenium ask for binary location of firefox...so this case does not need to execute my first command which gives the ff binaty location... – Saurabh Gaur Jun 16 '16 at 05:45
  • 1
    I might be a student, however I know what I'm talking about. All I'm doing is providing feedback on this answer so other users know what is wrong with this method as you failed to mention this in your answer. This way when they encounter the same issue they will know the pro's and cons of the answer and can make a decision on an implementation accordingly. There is no need to make rude comments and try to make smart remarks. All I'm doing is increasing the quality of your answer to help out others in the future. – RemcoW Jun 16 '16 at 07:21