如何确定测试是 运行 本地还是在远程服务器上
How to determine if tests are running local or on the remote server
根据此 tutorial,我可以在网站上上传文件,同时 运行 我在本地和远程服务器上进行测试。
如教程中所示:
For those of you doing this locally, all you need to do is use the
sendKeys command to type the local path of the file in any file field.
This works like a charm in all drivers. When moving this test to a
remote server (such as, for example, our Selenium 2 Cloud), all you
need to do is use the setFileDetector method to let WebDriver know
that you’re uploading files from your local computer to a remote
server instead of just typing a path.
在我必须使用的远程服务器上:
driver.setFileDetector(new LocalFileDetector());
...
upload.sendKeys("/Path/to/image.jpg");
和本地只是:
upload.sendKeys("/Path/to/image.jpg");
这一切都很好。唯一的问题是,没有关于如何确定我的测试是 运行 本地还是远程服务器的信息。
我已尝试确定 webDriver 的实例:
WebDriver proxiedWebDriver = ((WebDriverFacade) getDriver()).getProxiedDriver();
if (proxiedWebDriver instanceof RemoteWebDriver) {
((RemoteWebDriver)proxiedWebDriver).setFileDetector(new LocalFileDetector());
}
但似乎(本地和远程)两种情况都使用 RemoteWebDriver
而 运行,因为在每种情况下我都通过了 if
条件。
如何确定我的测试是 运行 本地测试还是远程测试?
要获取远程服务器的地址,您可以像这样使用 HttpCommandExecutor
:
HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
String remoteAddress = ce.getAddressOfRemoteServer().toString();
String localAddress = null;
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("google.com", 80));
localAddress = socket.getLocalAddress().getHostAddress();
} catch (IOException e) {
e.printStackTrace();
}
if (remoteAddress.contains("localhost") || remoteAddress.contains(localAddress)) System.out.println("Local machine");
else System.out.println("Remote machine");
以上代码获取远程服务器地址 (HUB) 并将其与您的 public IP 地址进行比较。如果您是 运行 本地或远程服务器
,它应该会为您提供信息
根据此 tutorial,我可以在网站上上传文件,同时 运行 我在本地和远程服务器上进行测试。
如教程中所示:
For those of you doing this locally, all you need to do is use the sendKeys command to type the local path of the file in any file field. This works like a charm in all drivers. When moving this test to a remote server (such as, for example, our Selenium 2 Cloud), all you need to do is use the setFileDetector method to let WebDriver know that you’re uploading files from your local computer to a remote server instead of just typing a path.
在我必须使用的远程服务器上:
driver.setFileDetector(new LocalFileDetector());
...
upload.sendKeys("/Path/to/image.jpg");
和本地只是:
upload.sendKeys("/Path/to/image.jpg");
这一切都很好。唯一的问题是,没有关于如何确定我的测试是 运行 本地还是远程服务器的信息。
我已尝试确定 webDriver 的实例:
WebDriver proxiedWebDriver = ((WebDriverFacade) getDriver()).getProxiedDriver();
if (proxiedWebDriver instanceof RemoteWebDriver) {
((RemoteWebDriver)proxiedWebDriver).setFileDetector(new LocalFileDetector());
}
但似乎(本地和远程)两种情况都使用 RemoteWebDriver
而 运行,因为在每种情况下我都通过了 if
条件。
如何确定我的测试是 运行 本地测试还是远程测试?
要获取远程服务器的地址,您可以像这样使用 HttpCommandExecutor
:
HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
String remoteAddress = ce.getAddressOfRemoteServer().toString();
String localAddress = null;
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("google.com", 80));
localAddress = socket.getLocalAddress().getHostAddress();
} catch (IOException e) {
e.printStackTrace();
}
if (remoteAddress.contains("localhost") || remoteAddress.contains(localAddress)) System.out.println("Local machine");
else System.out.println("Remote machine");
以上代码获取远程服务器地址 (HUB) 并将其与您的 public IP 地址进行比较。如果您是 运行 本地或远程服务器
,它应该会为您提供信息