Search code examples
javacookiesselenium-webdriverproxybrowsermob

How to add custom cookies in browsermob proxy on java


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();

Solution

  • 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;
            }
        });