项目作者: mike10004

项目描述 :
Browse with Selenium in Java and capture your traffic in a HAR file.
高级语言: Java
项目地址: git://github.com/mike10004/selenium-capture.git
创建时间: 2016-11-18T19:49:13Z
项目社区:https://github.com/mike10004/selenium-capture

开源协议:MIT License

下载


Travis build Status
AppVeyor build status
Maven Central

selenium-capture

This is a library that facilitates capturing HTTP traffic from a Selenium
web-browsing session. An intercepting proxy is used to capture the traffic.

Example

  1. // be sure to define system property with geckodriver location if not contained in $PATH
  2. // System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
  3. FirefoxWebDriverFactory factory = FirefoxWebDriverFactory.builder()
  4. .configure(firefoxOptions -> {
  5. firefoxOptions.setAcceptInsecureCerts(true);
  6. })
  7. .build();
  8. Path scratchDir = java.nio.file.Files.createTempDirectory("selenium-capture-example");
  9. try {
  10. TrafficCollector collector = TrafficCollector.builder(factory)
  11. .collectHttps(new AutoCertificateAndKeySource(scratchDir))
  12. .build();
  13. HarPlus<String> harPlus = collector.collect(new TrafficGenerator<String>() {
  14. @Override
  15. public String generate(WebDriver driver) {
  16. driver.get("https://www.example.com/");
  17. return driver.getTitle();
  18. }
  19. });
  20. System.out.println("collected page with title " + harPlus.result);
  21. File harFile = File.createTempFile("selenium-capture-example", ".har");
  22. BrowserUpHars.writeHar(harPlus.har, harFile, StandardCharsets.UTF_8);
  23. System.out.format("%s contains captured traffic%n", harFile);
  24. } finally {
  25. FileUtils.forceDelete(scratchDir.toFile());
  26. }

Capturing HTTPS traffic

To capture HTTPS traffic, the proxy must MITM the TLS traffic and the browser
must be configured to trust the proxy’s certificate or to accept insecure
certificates.

The library will generate a self-signed certificate on demand to capture HTTPS
traffic, or you can pre-generate one (take a look at the
[GenerateNewCertificate][generate-new] class in the test sources).

Generating a new certificate takes up to 1000ms, so if you’re frequently
generating new certificates with the auto-generator, then you might save time
by pre-generating a certificate and reusing it. For some examples of reusing
a pre-generated certificate, take a look at the unit tests where HTTPS traffic
is captured.

Providing cookies to your browser

The WebDriver APIs for cookie management are a tad simplistic. They have
annoying snafus like

  • you can’t add cookies associated with a site whose page is not open in the
    browser window,
  • you can’t add cookies with all of the attributes the browser is capable of
    storing
  • you can’t export cookies with all of the original attributes like expiration
    date

This library helps resolve the first two of these snafus for Firefox by
providing a mechanism to generate the SQLite database where the browser stores
cookies in the user profile directory.

To resolve the issue of cookie export, you can use the traffic capture to
gain access to all cookies that were sent during a browsing session.

Required Driver Versions

Make sure to keep your Chromedriver and
Geckodriver installations up to date. When
Selenium gets updated, the minimum required versions
of the driver executables are often raised.