0

I am writing automation test and to capture the network call made in background, I am using browsermob-proxy.

In browsermob-proxy, I want to set cookie before making requests. How can i do it?

Below is my code:-

String strFilePath = "data.har";

        // start the proxy
        ProxyServer server = new ProxyServer(4444);
        server.start();

        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        // get the Selenium proxy object
        Proxy proxy = server.seleniumProxy();

        FirefoxProfile profile = new FirefoxProfile();
        String userAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 MobileWeb/8H7 Safari/6533.18.5";
        profile.setPreference("general.useragent.override", userAgent);

        // configure it as a desired capability
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability(CapabilityType.PROXY, proxy);
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);

        final String[] remoteHost = {null};
        final String[] analytics = {null};
        final String[] fetchAdjs = {null};


        server.addRequestInterceptor(new RequestInterceptor()
        {
            int googleCount = 0;
            int adjs = 0;

            @Override
            public void process(BrowserMobHttpRequest browserMobHttpRequest, Har har)
            {
                remoteHost[0] = browserMobHttpRequest.getProxyRequest().getRemoteHost();

                String request = browserMobHttpRequest.getProxyRequest().getRequestURL().toString();

                if (request.matches(".*google.*"))
                    googleCount = googleCount + 1;

                if (request.matches(".*test.*"))
                    adjs = adjs + 1;

                analytics[0] = String.valueOf(googleCount);
                fetchAdjs[0] = String.valueOf(adjs);

                // System.out.println(browserMobHttpRequest.getMethod().getAllHeaders()[1]);  // user agent

                System.out.println(browserMobHttpRequest.getProxyRequest());
            }
        });

        // create a new HAR with the label "apple.com"
        server.newHar("assertselenium.com");

        // open yahoo.com
        driver.get("http://test.com");

        Thread.sleep(3000);
        Thread.sleep(3000);

        driver.get("http://test.com/316782/content/fDxL4zzv");
        Thread.sleep(3000);

        // get the HAR data
        Har har = server.getHar();

        FileOutputStream fos = new FileOutputStream(strFilePath);

        // view har file here --> http://pcapperf.appspot.com/
        har.writeTo(fos);
        server.stop();
        driver.quit();
Galet
  • 4,039
  • 12
  • 62
  • 119
  • Create a browser profile maybe - See this answer - http://stackoverflow.com/a/29396531/4720017. Adding the `user-dir` option will use the browser profile. – LittlePanda May 15 '15 at 08:13

1 Answers1

2

Using a request filter is the right approach. However, I would highly recommend building the latest version of BrowserMob Proxy with LittleProxy integration. Its filters are much more reliable and easier to use. See the github page for information on building and using the latest version. The LittleProxy interceptors section will be particularly relevant.

Here's a simple example of adding a cookie to every request using the new request filters:

    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.start();

    proxy.addRequestFilter(new RequestFilter() {
        @Override
        public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpRequest originalRequest) {
            request.headers().add("Cookie", "added-cookie=added-value");

            return null;
        }
    });
Jason Hoetger
  • 5,650
  • 2
  • 13
  • 14
  • Hi, I just want to ask. Why it is returning null ? Does it will affect the HttpResponse result ? – Aldo Suwandi Aug 26 '15 at 07:46
  • @AldoSuwandi: Returning null just means "Continue regular processing, I don't want to return a short-circuit response." It won't affect the response from the server. – Jason Hoetger Sep 06 '15 at 20:02