net.lightbody.bmp.core.har.Har Java Examples

The following examples show how to use net.lightbody.bmp.core.har.Har. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyBasedIT.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 6 votes vote down vote up
@Test
public void usingAProxyToTrackNetworkTrafficStep2() {
    BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
    browserMobProxy.start();
    Proxy seleniumProxyConfiguration = ClientUtil.createSeleniumProxy(browserMobProxy);

    FirefoxOptions firefoxOptions = new FirefoxOptions();
    firefoxOptions.setCapability(CapabilityType.PROXY, seleniumProxyConfiguration);
    driver = new FirefoxDriver(firefoxOptions);
    browserMobProxy.newHar();
    driver.get("https://www.google.co.uk");

    Har httpArchive = browserMobProxy.getHar();

    assertThat(getHTTPStatusCode("https://www.google.co.uk/", httpArchive))
            .isEqualTo(200);
}
 
Example #2
Source File: HttpConnectHarCaptureFilter.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
public HttpConnectHarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (!ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted HTTP CONNECT har capture on non-HTTP CONNECT request");
    }

    this.har = har;
    this.currentPageRef = currentPageRef;

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    // create and cache an HTTP CONNECT timing object to capture timing-related information
    this.httpConnectTiming = new HttpConnectTiming();
    httpConnectTimes.put(clientAddress, httpConnectTiming);
}
 
Example #3
Source File: HttpConnectHarCaptureFilter.java    From Dream-Catcher with MIT License 6 votes vote down vote up
public HttpConnectHarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (!ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted HTTP CONNECT har capture on non-HTTP CONNECT request");
    }

    this.har = har;
    this.currentPageRef = currentPageRef;

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    // create and cache an HTTP CONNECT timing object to capture timing-related information
    this.httpConnectTiming = new HttpConnectTiming();
    httpConnectTimes.put(clientAddress, httpConnectTiming);
}
 
Example #4
Source File: HttpConnectHarCaptureFilter.java    From CapturePacket with MIT License 6 votes vote down vote up
public HttpConnectHarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (!ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted HTTP CONNECT har capture on non-HTTP CONNECT request");
    }

    this.har = har;
    this.currentPageRef = currentPageRef;

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    // create and cache an HTTP CONNECT timing object to capture timing-related information
    this.httpConnectTiming = new HttpConnectTiming();
    httpConnectTimes.put(clientAddress, httpConnectTiming);
}
 
Example #5
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public Har endHar() {
    Har oldHar = getHar();

    // end the page and populate timings
    endPage();

    this.har = null;

    return oldHar;
}
 
Example #6
Source File: ProxyBasedIT.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 5 votes vote down vote up
private int getHTTPStatusCode(String expectedURL, Har httpArchive) {
    for (HarEntry entry : httpArchive.getLog().getEntries()) {
        if (entry.getRequest().getUrl().equals(expectedURL)) {
            return entry.getResponse().getStatus();
        }
    }

    return 0;
}
 
Example #7
Source File: BrowserMobProxyIT.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 5 votes vote down vote up
@Test
public void usingAProxyToTrackNetworkTrafficStep2() {
    getBrowserMobProxy().newHar();
    driver.get("https://www.google.co.uk");

    Har httpArchive = getBrowserMobProxy().getHar();

    assertThat(getHTTPStatusCode("https://www.google.co.uk/", httpArchive))
            .isEqualTo(200);
}
 
Example #8
Source File: BrowserMobProxyIT.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 5 votes vote down vote up
private int getHTTPStatusCode(String expectedURL, Har httpArchive) {
    for (HarEntry entry : httpArchive.getLog().getEntries()) {
        if (entry.getRequest().getUrl().equals(expectedURL)) {
            return entry.getResponse().getStatus();
        }
    }

    return 0;
}
 
Example #9
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public Har endHar() {
    Har oldHar = getHar();

    // end the page and populate timings
    endPage();

    this.har = null;

    return oldHar;
}
 
Example #10
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the HarCaptureFilter that will capture request and response information. If no har is specified in the
 * constructor, this filter will do nothing.
 * <p/>
 * Regardless of the CaptureTypes specified in <code>dataToCapture</code>, the HarCaptureFilter will always capture:
 * <ul>
 *     <li>Request and response sizes</li>
 *     <li>HTTP request and status lines</li>
 *     <li>Page timing information</li>
 * </ul>
 *
 * @param originalRequest the original HttpRequest from the HttpFiltersSource factory
 * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required)
 * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client
 * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be
 *                      captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType)
 */
public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef, Set<CaptureType> dataToCapture) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted har capture for HTTP CONNECT request");
    }

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    if (dataToCapture != null && !dataToCapture.isEmpty()) {
        this.dataToCapture = EnumSet.copyOf(dataToCapture);
    } else {
        this.dataToCapture = EnumSet.noneOf(CaptureType.class);
    }

    // we may need to capture both the request and the response, so set up the request/response filters and delegate to them when
    // the corresponding filter methods are invoked. to save time and memory, only set up the capturing filters when
    // we actually need to capture the data.
    if (this.dataToCapture.contains(CaptureType.REQUEST_CONTENT) || this.dataToCapture.contains(CaptureType.REQUEST_BINARY_CONTENT)) {
        requestCaptureFilter = new ClientRequestCaptureFilter(originalRequest);
    } else {
        requestCaptureFilter = null;
    }

    if (this.dataToCapture.contains(CaptureType.RESPONSE_CONTENT) || this.dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        responseCaptureFilter = new ServerResponseCaptureFilter(originalRequest, true);
    } else {
        responseCaptureFilter = null;
    }

    this.har = har;

    this.harEntry = new HarEntry(currentPageRef);
}
 
Example #11
Source File: ExecutionReportTest.java    From bromium with MIT License 5 votes vote down vote up
@Test
public void canAccessFilesNeededForReport() {
    LoadingTimes loadingTimes = mock(LoadingTimes.class);
    Har har = mock(Har.class);
    AutomationResult automationResult = AutomationResult.SUCCESS;

    ExecutionReport executionReport = new ExecutionReport(loadingTimes, har, automationResult);

    assertEquals(loadingTimes, executionReport.getLoadingTimes());
    assertEquals(har, executionReport.getHar());
    assertEquals(automationResult, executionReport.getAutomationResult());
}
 
Example #12
Source File: CaptureBinder.java    From CapturePacket with MIT License 5 votes vote down vote up
public void clearHarEntries() {
    if (mProxyServer != null) {
        Har har = mProxyServer.newHar();
        if (har != null) {
            har.getLog().clearAllEntries();
        }
    }
}
 
Example #13
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public Har endHar() {
    Har oldHar = getHar();

    // end the page and populate timings
    endPage();

    this.har = null;

    return oldHar;
}
 
Example #14
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the HarCaptureFilter that will capture request and response information. If no har is specified in the
 * constructor, this filter will do nothing.
 * <p/>
 * Regardless of the CaptureTypes specified in <code>dataToCapture</code>, the HarCaptureFilter will always capture:
 * <ul>
 *     <li>Request and response sizes</li>
 *     <li>HTTP request and status lines</li>
 *     <li>Page timing information</li>
 * </ul>
 *
 * @param originalRequest the original HttpRequest from the HttpFiltersSource factory
 * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required)
 * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client
 * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be
 *                      captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType)
 */
public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef, Set<CaptureType> dataToCapture) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted har capture for HTTP CONNECT request");
    }

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    if (dataToCapture != null && !dataToCapture.isEmpty()) {
        this.dataToCapture = EnumSet.copyOf(dataToCapture);
    } else {
        this.dataToCapture = EnumSet.noneOf(CaptureType.class);
    }

    // we may need to capture both the request and the response, so set up the request/response filters and delegate to them when
    // the corresponding filter methods are invoked. to save time and memory, only set up the capturing filters when
    // we actually need to capture the data.
    if (this.dataToCapture.contains(CaptureType.REQUEST_CONTENT) || this.dataToCapture.contains(CaptureType.REQUEST_BINARY_CONTENT)) {
        requestCaptureFilter = new ClientRequestCaptureFilter(originalRequest);
    } else {
        requestCaptureFilter = null;
    }

    if (this.dataToCapture.contains(CaptureType.RESPONSE_CONTENT) || this.dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        responseCaptureFilter = new ServerResponseCaptureFilter(originalRequest, true);
    } else {
        responseCaptureFilter = null;
    }

    this.har = har;

    this.harEntry = new HarEntry(currentPageRef);
}
 
Example #15
Source File: ProxyStepDefinitions.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
/**
 * Saves a HAR file with the details of the transactions that have passed through BrowserMob.
 * This step is only required if you wish to save har files at particular points during the test.
 * The HAR file is always saved when browsermob is shut down, meaning that once you begin the
 * capture of the HAR file it will be saved regardless of the success or failure of the test.
 * @param alias If this word is found in the step, it means the filename is found from the
 *              data set.
 * @param filename The optional filename to use for the HAR file
 */
@When("^I dump the HAR file(?: to( alias)? \"(.*?)\")?$")
public void saveHarFile(final String alias, final String filename) {

	final String fixedFilename = autoAliasUtils.getValue(
		StringUtils.defaultString(filename, Constants.HAR_FILE_NAME),
		StringUtils.isNotBlank(alias),
		State.getFeatureStateForThread());

	final Optional<ProxyDetails<?>> proxy =
		State.getFeatureStateForThread().getProxyInterface(BrowsermobProxyUtilsImpl.PROXY_NAME);

	proxy
		.flatMap(ProxyDetails::getInterface)
		.map(BrowserMobProxy.class::cast)
		.map(x -> Try.run(() -> {
			final Har har = x.getHar();

			checkState(
				har != null,
				"You need to add the step \"I enable HAR logging\" before saving the HAR file");

			final File file = new File(
				State.getFeatureStateForThread().getReportDirectory()
					+ "/"
					+ fixedFilename);
			har.writeTo(file);
		}));
}
 
Example #16
Source File: CaptureService.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void onLowMemory() {
    super.onLowMemory();
    HLog.w(TAG, "onLowMemory()");
    if (mProxyServer != null) {
        Har har = mProxyServer.newHar();
        if (har != null) {
            HarLog log = har.getLog();
            log.clearAllEntries();
            log.server = null;
            mProxyServer.mHarCallback.onClearEntries();
        }
    }
}
 
Example #17
Source File: BaseTest.java    From at.info-knowledge-base with MIT License 4 votes vote down vote up
@AfterTest
public void stopProxy() throws Exception {
    Har har = server.getHar();
    server.stop();
    System.out.println("Size of har file is: " + har.getLog().getEntries().size());
}
 
Example #18
Source File: BrowserMobProxy.java    From at.info-knowledge-base with MIT License 4 votes vote down vote up
public Har getHar() throws IOException {
    return mapper.readValue(getHarAsString(), Har.class);
}
 
Example #19
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public Har getHar() {
    return har;
}
 
Example #20
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public Har newPage(String pageRef) {
    return newPage(pageRef, null);
}
 
Example #21
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public Har newPage() {
    return newPage(null);
}
 
Example #22
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public Har newHar(String initialPageRef) {
    return newHar(initialPageRef, null);
}
 
Example #23
Source File: TrafficLogProvider.java    From bobcat with Apache License 2.0 4 votes vote down vote up
@Override
public void listeningStopped(Har har) {
  this.hars.add(har);
}
 
Example #24
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public Har newHar() {
    return newHar(null);
}
 
Example #25
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 4 votes vote down vote up
@Override
public Har newPage(String pageRef) {
    return newPage(pageRef, null);
}
 
Example #26
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 4 votes vote down vote up
@Override
public Har newPage() {
    return newPage(null);
}
 
Example #27
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 4 votes vote down vote up
@Override
public Har getHar() {
    return har;
}
 
Example #28
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 4 votes vote down vote up
@Override
public Har newHar() {
    return newHar(null);
}
 
Example #29
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 4 votes vote down vote up
@Override
public Har getHar() {
    return har;
}
 
Example #30
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 4 votes vote down vote up
@Override
public Har newHar(String initialPageRef) {
    return newHar(initialPageRef, null);
}