Har entries is not captured while running Selenium test in IE and Safari

77 views
Skip to first unread message

VChetan

unread,
May 7, 2014, 9:13:37 AM5/7/14
to seleniu...@googlegroups.com
We are not able to capture HAR enteries while running Selenium test for internet explorer and also Safari but functionally it is working. There is no problem with connection to the browser. I am not trying to create a separate HAR file but instead trying to capture values provided in Har entries (like cookies, header values etc.).

Please find the code of base template where I am connecting to internet explorer and capturing http requests using HAR (provided by BrowserMob Proxy) -

package test;

import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.core.har.HarEntry;
import net.lightbody.bmp.core.har.HarNameValuePair;
import net.lightbody.bmp.core.har.HarRequest;
import net.lightbody.bmp.proxy.ProxyServer;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.protocol.HttpContext;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;


public abstract class BaseTemplate {
protected static WebDriver browser;
protected static ProxyServer proxyServer;
protected static WebAnalyticsRequest webAnalyticsRequest;
    @BeforeClass
    public static void oneTimeSetUp()
    throws Exception
    {
startProxyServer();
startBrowser();
    }
 
    @AfterClass
    public static void oneTimeTearDown()
    throws Exception
    {
    clearSession();
proxyServer.stop();
closeBrowsers();
    }
    
    private static void closeBrowsers()
    throws Exception
    {
    browser.quit();
    }
    
    private static void clearSession()
    {
    webAnalyticsRequest = null;
    }
@Before
public void setUp()
throws Exception
{
this.getWebAnalyticsRequest();
}

private void getWebAnalyticsRequest()
throws Exception
{
if (!requestCached())
{
this.fetchPageInformation();
}
}
private static boolean requestCached()
{
return webAnalyticsRequest != null;
}
private static void startProxyServer()
throws Exception
{
proxyServer = new ProxyServer(9100);
proxyServer.start();
proxyServer.setCaptureHeaders(true);
proxyServer.setCaptureContent(true);
}
private static void startBrowser()
throws Exception
{
try {
File file = new File("C:\\Users\\Chetan\\Desktop\\SVN_WORKSPACE\\WebAnalytics\\lib\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
Proxy proxy = proxyServer.seleniumProxy();
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(CapabilityType.PROXY, proxy);
browser = new InternetExplorerDriver(capabilities);
} catch (Exception e) {
throw e;
}
}
protected abstract void fetchPageInformation() throws Exception;
protected static void captureNetworkTraffic()
throws Exception
{
waitForPageLoad(2);
                proxyServer.newHar("Testing");
}
protected static void getWebAnalyticsValues(int eventIndex)
throws Exception
{
List<WebAnalyticsRequest> webAnalyticsRequests = getWebAnalyticsRequests();
setChosenRequest(webAnalyticsRequests, eventIndex);
}
protected static List<WebAnalyticsRequest> getWebAnalyticsRequests()
throws Exception
{
Thread.sleep(5000);
List<WebAnalyticsRequest> webAnalyticsRequests = new ArrayList<WebAnalyticsRequest>();
Har har = proxyServer.getHar();
List<HarEntry> entries = har.getLog().getEntries();
for (HarEntry entry : entries)
{
HarRequest request = entry.getRequest();
List<HarNameValuePair> allHeaders = request.getHeaders();
if (allHeaders != null)
{
for (HarNameValuePair nvp : allHeaders)
{
if (nvp.getName().equals("Host") && nvp.getValue().contains("metrics.target"))
{
List<HarNameValuePair> queryValues = request.getQueryString();
HashMap<String,String> propHashMap = convertPropsListToHashMap(queryValues);
WebAnalyticsProperties props = new WebAnalyticsProperties(propHashMap);
List<HarNameValuePair> requestHeaders = request.getHeaders();
HashMap<String,String> headersHashMap = convertPropsListToHashMap(requestHeaders);
WebAnalyticsHeaderValues headerValues = new WebAnalyticsHeaderValues(headersHashMap);
WebAnalyticsCookieValues cookieValues = getCookieValues(headersHashMap.get("Cookie"));
WebAnalyticsRequest webAnalyticsRequest =
new WebAnalyticsRequest(cookieValues, headerValues, props);
webAnalyticsRequests.add(webAnalyticsRequest);
}
}
}
}
return webAnalyticsRequests;
}
}

Reply all
Reply to author
Forward
0 new messages