【问题标题】:How to support both older and newer version of Firefox simultaneously using selenium (Java)如何使用 selenium (Java) 同时支持新旧版本的 Firefox
【发布时间】:2016-06-14 11:34:41
【问题描述】:

我在多台 PC 中有多个版本的 Firefox,其中一些具有 最新的,即 47,而一些具有旧版本

我有关注this 并设置 RemoteWebDriverMarionette,下一代 FirefoxDriver 以支持 Firefox 版本 47 进行自动化,如下所示:-

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");

它在 Firefox 47 版 上运行良好,但是当我在安装了 Firefox 旧版本 的其他 PC 上运行相同时,它给出了@ 987654328@ 如下:-

引起:org.openqa.selenium.remote.UnreachableBrowserException: 无法安装

rt 一个新的会话。可能的原因是远程地址无效 服务器或br

owser 启动失败。

构建信息:版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 17:00:58'

系统信息:主机:'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.version: MarionetteDriver

原因:org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHost

ConnectException: 连接到 localhost:3125 [localhost/127.0.0.1] 失败:连接

拒绝:连接

构建信息:版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 17:00:58'

系统信息:主机:'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.version: MarionetteDriver

原因:java.net.ConnectException:连接被拒绝:连接

警告 - 例外:连接被拒绝:连接

当我删除行 capabilities.setCapability("marionette", true); 意味着删除 MarionetteDriver 支持时,它在 Firefox 旧版本 上运行良好,但在 Firefox 版本 47 上提升 exception 即。 UnreachableBrowserExcetion.

所以我的问题是:-

有什么方法可以知道Firefox version 或任何其他解决方案,我可以通过这些解决方案同时使用 版本的 Firefox??。

我想要一个通用解决方案,我的代码可以智能地知道它何时以MarionetteDriver 开头,何时以FireFoxDriver 开头,基于FireFox Version

提前谢谢...:)

【问题讨论】:

    标签: java selenium firefox selenium-webdriver remotewebdriver


    【解决方案1】:

    根据您要在 43 还是 47 上运行它,在测试开始时设置一个布尔标志。检查您正在创建驱动程序的代码中的标志,例如:

    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");
    

    现在如果你将 useMarionette 设置为 true,它将与 marionette 一起运行,如果设置为 false,则不会。

    如果你真的想为可用的 firefox 版本解析 windows 注册表,这里有一个部分示例:

    How to check if a program is installed on Windows system

    如下使用:

    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
            }
        } 
    }
    

    【讨论】:

    • 我无法根据标志执行此操作。我想要更通用的解决方案,让我的代码能够智能地知道何时使用MarionetteDriver 以及何时使用FireFoxDriver 运行。 .
    • 我看到以通用方式执行此操作的唯一方法是尝试请求网络浏览器并捕获异常,并在异常请求时捕获另一个。这是非常糟糕的设计。您应该控制测试的运行位置,毕竟这是您的环境,因此您应该决定测试的运行方式。
    • 您是在分布式 selenium 网格中运行测试还是手动启动它们?
    • 我想在多台机器上运行相同的代码,但我不知道在所有机器上运行哪个版本的 firefox
    • 那么你的要求是不可能的。你只能让网格给你你要找的浏览器,你不能问它有哪些浏览器。
    【解决方案2】:

    现在我通过以下方法实现了这一点:-

    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");
    

    通过这种通用方式,我们可以使用相同的代码在多台 PC 中自动执行测试。无论那些 Pcs 上安装了哪个版本的 Firefox

    已编辑..

    对于 Mac 电脑,我们可以更改 command 以获取已安装的 Firefox 版本,如下所示:-

    String installedFirefoxLocation = "/Applications/Firefox.app/Contents/MacOS/firefox"
    // it's default location
    
    String[] getFirefoxVersionCmd = { installedFirefoxLocation + " -v | more" };
    

    如果firefox 没有安装在默认位置,那么我们可以通过路径或System 属性提供Firefox 二进制位置,因为如果没有提供Firefox 二进制location,selenium 将无法工作。其他位置。

    为了检查OS,我们可以使用如下:-

    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
    }
    

    ...:)

    【讨论】:

    • 与 Mobrockers 之前为您提供的答案相比,这似乎没有必要冗长而复杂。你叫的答案太长了。
    • 这也不适用于任何不同的操作系统,也不适用于在不同位置安装了 firefox 的机器。
    • @RemcoW 在这里我没有比较哪个答案更好..对于不同的操作系统只需要更改我在编辑的答案中提供的命令行...您是否尝试过从使用 selenium 的不同位置??....90% Pcs 具有默认位置...如果位置不是默认硒,则仅供参考给出 ff 二进制位置...
    • 我可能是一名学生,但我知道我在说什么。我所做的只是提供有关此答案的反馈,以便其他用户知道此方法有什么问题,因为您没有在答案中提及这一点。这样,当他们遇到相同的问题时,他们就会知道答案的优缺点,并可以相应地做出实施决定。没有必要发表粗鲁的言论并尝试发表聪明的言论。我所做的只是提高您回答的质量,以便将来帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 2012-07-07
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多