RemoteWebDriver 和 Grid - 是否可以获取服务器 IP?

发布于 2024-12-01 09:23:32 字数 255 浏览 0 评论 0原文

我使用 Selenium 2 和 Grid 中的 RemoteWebDriver 将测试划分到多个虚拟机上。假设我有两台 Linux 机器,在测试中我指定了在 Linux 机器上运行的功能,但我无法确定正在使用这两台机器中的哪一台。 有什么办法可以弄清楚吗?像 driver.getServerIp() 或者其他什么? 原因是,在我的 Selenium 测试代码中,我想在运行测试的 Linux 机器的控制台上启动 bash 脚本。因此我必须知道测试在哪台机器上运行。

谢谢你们!

I am using RemoteWebDriver from Selenium 2 and Grid to divide my tests on several virtual machines. Let's say I have two Linux machines and in a test I specify the capabilities to run on a Linux machine, I can't figure out which of these two machines is being used.
Is there any way, to figure it out? Something like driver.getServerIp() or anything else?
The reason is, in my Selenium test code I want to start a bash script on the console of the linux machine where the test is being run. Therefore I have to know on which machine the test runs.

Thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤单情人 2024-12-08 09:23:32

我的java技能不是最好的,这段代码需要一些清理,但这对我有用。

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort + "/grid/api/testsession?session=" +  driver.getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = (JSONObject)new JSONParser().parse(EntityUtils.toString(response.getEntity()));     
String proxyID = object.get("proxyId"));

proxyID 包含节点的 IP。

My java skills aren't the greatest and this code could use some cleaning up, but this works for me.

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort + "/grid/api/testsession?session=" +  driver.getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = (JSONObject)new JSONParser().parse(EntityUtils.toString(response.getEntity()));     
String proxyID = object.get("proxyId"));

proxyID contains the IP of the node.

七月上 2024-12-08 09:23:32
HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
ce.getAddressOfRemoteServer();

尽管您可能知道该地址,因为无论如何您都将其传递到 RemoteWebDriver 的构造函数中。

HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
ce.getAddressOfRemoteServer();

Although presumably you'd know the address since you passed it into the constructor for RemoteWebDriver anyway.

々眼睛长脚气 2024-12-08 09:23:32

Bobble D 提供的答案仅有效,正如 Nilesh 提到的那样,JSONParser 也存在问题,这里是更新的代码,可以提供帮助

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort +         "/grid/api/testsession?session=" +  ((RemoteWebDriver) driver).getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity()));   
String proxyID = object.get("proxyId"));
System.out.println(proxyID.split("//")[1].split(":")[0]);

Answer provided by Bobble D is working only that there was problem with JSONParser as mentioned by Nilesh as well, here is updated code which can help

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort +         "/grid/api/testsession?session=" +  ((RemoteWebDriver) driver).getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity()));   
String proxyID = object.get("proxyId"));
System.out.println(proxyID.split("//")[1].split(":")[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文