我正在编写自动化测试并捕获在后台进行的网络调用,我正在使用browsermob-proxy。

在browsermob-proxy中,我想在发出请求之前设置cookie。我该怎么做?

下面是我的代码:

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

最佳答案

使用请求过滤器是正确的方法。但是,我强烈建议使用LittleProxy集成构建最新版本的BrowserMob代理。它的过滤器更可靠,更易于使用。有关构建和使用最新版本的信息,请参见github pageLittleProxy interceptors部分将特别相关。

这是一个使用新的请求过滤器向每个请求添加cookie的简单示例:

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

09-25 17:38