How to capture all requests made by page in webdriver? Is there any alternative to Browsermob?

萝らか妹 提交于 2019-11-28 04:10:19
Mahesh Dani

There is an alternative with firefox ver 42+, there is addon called Firefox HarExport

File harExportApi = new File(System.getProperty("user.dir")
                     + "/src/main/resources/firebug/harexporttrigger-0.5.0-beta.7.xpi");

netExportProfile.addExtension(harExportApi);
netExportProfile.setPreference("extensions.netmonitor.har.enableAutomation", true);
    netExportProfile.setPreference("extensions.netmonitor.har.contentAPIToken", "test");
    netExportProfile.setPreference("extensions.netmonitor.har.autoConnect", true);

cap.setCapability(FirefoxDriver.PROFILE, netExportProfile);

and running following script will give us all the request responses

 String getHarLogScript = "var options = {\n" +
                "    token: \"test\",\n" +
                "    getData: true,\n" +
                "    title: \"my custom title\",\n" +
                "    jsonp: false,\n" +
                "  };\n" +
                "\n" +
                "  HAR.triggerExport(options).then(result => {\n" +
                "    var har = JSON.parse(result.data);\n" +
                "\n" +
                "    // Use performance.timing to provide onContentLoad\n" +
                "    +
                "     +
                "    var t = performance.timing;\n" +
                "    var pageTimings = har.log.pages[0].pageTimings;\n" +
                "    pageTimings.onContentLoad = t.domContentLoadedEventStart - t.navigationStart;\n" +
                "    pageTimings.onLoad = t.loadEventStart - t.navigationStart;\n" +
                "\n" +
                "    window.HarEntries=har.log.entries\n" +
                "\n" +
                "    console.log(\"HAR log (\" + result.data.length + \") \", har.log);\n" +
                "  }, err => {\n" +
                "    console.error(err);\n" +
                "  });"

LOG.info("Loading HAR log entries object into browser HarEntries object");
SeleniumUtils.executeScript(driver, getHarLogScript);

harEntries = ((List<Object>) SeleniumUtils.executeScript(driver, "return window.HarEntries"));

I've been working recently on this kind of proxy. Project is pretty fresh, I'm still working on documentation but it may be worth checking. Sources and examples are here

  1. Add dependency to your project
    <dependency>
       <groupId>com.moxproxy</groupId>
       <artifactId>moxproxy.core</artifactId>
       <version>1.0.2</version>
    </dependency>
  1. Start proxy
    MoxProxy proxy = LocalMoxProxy.builder()
                .withPort(89)
                .build();
    proxy.startServer();
  1. Setup selenium webdriver to use proxy on localhost with port 89 and run test

  2. Collect traffic

    List<MoxProxyProcessedTrafficEntry> requestTraffic = proxy.getAllRequestTraffic();
    List<MoxProxyProcessedTrafficEntry> responseTraffic = proxy.getAllResponseTraffic();

Beside collecting traffic proxy provides posibility to modify requests and responses - details on github

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