Chrome remote debugging in a seleniumgrid

倖福魔咒の 提交于 2019-11-29 18:01:54

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!