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

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

问题描述

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

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.

但 chrome 拒绝 localhost 以外的任何内容.

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.

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

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

首先是servlet:

First the servlet:

public class DebugServlet extends RegistryBasedServlet

通过 node.json 注册像

being registered through the node.json like

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

要访问节点(在正确的机器上),我向 selenium 会话询问它:

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

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

sessionid"可以从 chromedriver 中检索到.

where the "sessionid" can be retrieved from chromedriver.

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

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

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

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

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"

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

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)

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

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

在 servlet 中:

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

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

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