如何使用 selenium 同时支持新旧版本的 Firefox (Java)
How to support both older and newer version of Firefox simultaneously using selenium (Java)
我在多个 PC 中有多个 Firefox
版本,其中一些 最新 是 47
和有些有旧版本。
我有关注 this
并使用 Marionette
设置 RemoteWebDriver
,下一代 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 上运行良好,但是当我在其他 PC 上 运行 安装相同 Firefox 旧版本,它给出 exception
如下:-
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
当我删除行 capabilities.setCapability("marionette", true);
意味着删除 MarionetteDriver
支持时,它在 Firefox 旧版本 上运行良好,但提高 exception
与 Firefox 版本 47 即。 UnreachableBrowserExcetion
.
所以我的问题是:-
有没有办法知道 Firefox version
或我可以同时 运行 同时使用 Old 和 New 的任何其他解决方案 版本的 Firefox??.
我想要一个通用解决方案,我的代码可以智能地知道它什么时候以 MarionetteDriver
开始,什么时候以 FireFoxDriver
开始FireFox Version
.
提前致谢...:)
在测试开始时设置一个布尔标志,具体取决于您是否想 运行 它在 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
}
}
}
现在我通过以下方法实现了这一点:-
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
二进制位置,因为如果不提供,selenium 将无法工作Firefox
二进制 location
如果安装在其他位置。
为了检查 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
}
...:)
我在多个 PC 中有多个 Firefox
版本,其中一些 最新 是 47
和有些有旧版本。
我有关注 this
并使用 Marionette
设置 RemoteWebDriver
,下一代 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 上运行良好,但是当我在其他 PC 上 运行 安装相同 Firefox 旧版本,它给出 exception
如下:-
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
当我删除行 capabilities.setCapability("marionette", true);
意味着删除 MarionetteDriver
支持时,它在 Firefox 旧版本 上运行良好,但提高 exception
与 Firefox 版本 47 即。 UnreachableBrowserExcetion
.
所以我的问题是:-
有没有办法知道 Firefox version
或我可以同时 运行 同时使用 Old 和 New 的任何其他解决方案 版本的 Firefox??.
我想要一个通用解决方案,我的代码可以智能地知道它什么时候以 MarionetteDriver
开始,什么时候以 FireFoxDriver
开始FireFox Version
.
提前致谢...:)
在测试开始时设置一个布尔标志,具体取决于您是否想 运行 它在 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
}
}
}
现在我通过以下方法实现了这一点:-
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
二进制位置,因为如果不提供,selenium 将无法工作Firefox
二进制 location
如果安装在其他位置。
为了检查 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
}
...:)