How to add custom cookies in browsermob proxy on java -
i writing automation test , capture network call made in background, using browsermob-proxy.
in browsermob-proxy, want set cookie before making requests. how can it?
below code:-
string strfilepath = "data.har"; // start proxy proxyserver server = new proxyserver(4444); server.start(); server.setcaptureheaders(true); server.setcapturecontent(true); // selenium proxy object proxy proxy = server.seleniumproxy(); firefoxprofile profile = new firefoxprofile(); string useragent = "mozilla/5.0 (iphone; u; cpu iphone os 4_3_2 mac os x; en-us) applewebkit/533.17.9 (khtml, gecko) version/5.0.2 mobileweb/8h7 safari/6533.18.5"; profile.setpreference("general.useragent.override", useragent); // configure desired capability desiredcapabilities capabilities = desiredcapabilities.firefox(); capabilities.setcapability(capabilitytype.proxy, proxy); capabilities.setcapability(firefoxdriver.profile, profile); // start browser 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 new har 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); // 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();
using request filter right approach. however, highly recommend building latest version of browsermob proxy littleproxy integration. filters more reliable , easier use. see github page information on building , using latest version. littleproxy interceptors section particularly relevant.
here's simple example of adding cookie every request using 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; } });
Comments
Post a Comment