Chrome在seleniumgrid中进行远程调试 [英] Chrome remote debugging in a seleniumgrid

查看:355
本文介绍了Chrome在seleniumgrid中进行远程调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用几个chrome实例运行一个硒网格。硒网格是2台机器(窗口)有几个节点。测试从连接到电网的另一台机器执行。为了能够使用远程调试的功能,我需要从正在执行的计算机(可以读取会话主机和驱动程序debugUrl)连接到其他计算机以及最后的chrome实例。



但是chrome拒绝localhost以外的任何东西。

我只能找到解决方案,在那里只有一个实例的情况下,人们可以通过隧道或端口转发,这也许可以。在网格中,我没有静态端口或静态规则来提供静态转发。



在我的场景中,网格是自动构建的,而不是永远在运行的系统。 / p>

有没有人提示如何解决这个问题?

解决方案

自己找到了我想分享的解决方案。我只会发布部分代码给出提示,而不是完整的代码,因为它在这里工作很多,但对于一个有经验的开发人员来说,这应该已经足够了。



成为能够访问正确的浏览器并访问其远程调试websocket,我为我的节点实现了一个自定义servlet。

首先是servlet:

  public class DebugServlet扩展RegistryBasedServlet 

通过node.json注册,如

 servlets:[com ..... ui.util.DebugServlet] ,

要访问节点(在正确的机器上),我要求selenium会话: / p>

 http://+ hubHost +:+ hubPort +/ grid / api / testsession?session =+ sessionId 

其中sessionid可以从chromedriver中获取。



从返回的json中,我们可以提取会话的节点信息,这里我们需要url。

  url = JSONUtil.get(response.getBody(),proxyId)

不,我们可以调用正确主机的servlet并为浏览器提供websocket url,以及需要的任何数据。在我的例子中,为BasicAuth添加一个默认的网络头。

  url +/ extra / DebugServlet
code>

带有java头部(也可以是参数或其他http提供的可能性)

  new BasicHeader(BrowserUrl,webSocketDebuggerUrl),新的BasicHeader(Name,名称),
new BasicHeader(Value,value)

在servlet中,我们现在提取数据并使用给定的url打开websocket到浏览器并进行调用。 / p>

在servlet中:

  public static final String networkDebugging ={ \id \:1,\method \:\Network.enable \,\params \:{\maxTotalBufferSize \:10000000,\ maxResourceBufferSize \:5000000}}; 

public static final String addHeader ={\id \:2,\method \:\Network.setExtraHTTPHeaders \,\params \ :{\headers \:{\$ {key} \:\$ {value} \}}};


ws.connect();
ws.setAutoFlush(true);
ws.sendText(networkDebugging);

String payload = TemplateUtil.replace(addHeader,name,value);
ws.sendText(payload);


Im running a selenium-grid with several chrome instances. The selenium grid are 2 machines(windows) with several nodes. The tests are executed from another machine which connects to the grid. To be able to use the features of remote debugging, i need to connect from the executing machine(which can read the sessions host and the drivers debugUrl) to the other machines and finally the chrome instances.

But chrome rejects anything else than localhost.

I can only find solutions, where people tunnel or port forwarding, which is perhaps ok, when there is only a single instance. In a grid i don't have static ports or static rules to provide static forwarding.

In my scenario the grid is build up automated and not an ever running system.

Has anybody a hint how to solve this?

解决方案

Since i found a solution by myself i want to share. I will only post parts of code to give the hints and not the full code since its to much work here, but for an experienced developer this should be enough.

To be able to address the right browser and access its remote-debug websocket i implemented a custom servlet for my nodes.

First the servlet:

public class DebugServlet extends RegistryBasedServlet

being registered through the node.json like

"servlets" :["com.....ui.util.DebugServlet"],

To access the node(on the right machine) i ask the selenium session for it like:

"http://" + hubHost + ":" + hubPort + "/grid/api/testsession?session=" + sessionId

where the "sessionid" can be retrieved from chromedriver.

From the returned json we can extract the node info of the session, here we need the url.

url = JSONUtil.get(response.getBody(), "proxyId")

No we can call the servlet of the correct host and give in the websocket url for the browser and whatever data is needed. In my example to add a default network-header for BasicAuth.

url+ "/extra/DebugServlet"

with the header in java(can also be parameters or other http provided possibilities)

new BasicHeader("BrowserUrl", webSocketDebuggerUrl), new BasicHeader("Name", name),
                new BasicHeader("Value", value)

In the servlet we extract now the data and open a websocket to the browser with the given url and make our calls.

In the servlet:

public static final String networkDebugging = "{\"id\": 1,\"method\": \"Network.enable\",\"params\": {\"maxTotalBufferSize\": 10000000,\"maxResourceBufferSize\": 5000000 }}";

public static final String addHeader = "{\"id\": 2,\"method\": \"Network.setExtraHTTPHeaders\",\"params\": { \"headers\": {\"${key}\": \"${value}\"}}}";


ws.connect();
ws.setAutoFlush(true);
ws.sendText(networkDebugging);

String payload = TemplateUtil.replace(addHeader, name, value);
ws.sendText(payload);

这篇关于Chrome在seleniumgrid中进行远程调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆