使用Ruby访问Google Chrome开发者工具上的网络面板数据

时间:2018-02-13 11:20:43

标签: ruby google-chrome-devtools

我已经提供了以下代码,以便我可以访问Google Chrome开发者工具上的网络面板数据,以便在运行我的Selenium脚本时能够获取此信息。

driver.get("http://www.google.com");
System.out.println(driver.getTitle());
String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);

然而,我的自动化框架是用Ruby编写的,我如何将上述内容转换为在我的Ruby环境中工作?我需要安装其他宝石吗?

1 个答案:

答案 0 :(得分:0)

selenium API在它的实现中非常相似,所以,虽然我没有测试过,但我认为以下方法可行:

driver.get("http://www.google.com")
puts(driver.title)
script = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;"
data = driver.execute_script(script)
puts data

您还必须安装Selenium gem。你可以看到他们的official instructions