import com.paddypower.reporter.ReporterInterface;
import com.paddypower.reporter.SeleniumReporterFactory;
import org.browsermob.core.har.Har;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* User: gabell
* Date: 27/03/12
* Time: 10:18
*/
public class demo {
@Test(priority = 1, groups = {"demo"},
description = "demo test")
public void test1() throws IOException {
//System.setProperty("webdriver.chrome.driver", "library\\Chromedriver\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ArrayList<String> switches = new ArrayList<>();
switches.add("--start-maximized");
switches.add("--disable-application-cache");
switches.add("--disk-cache-size=1");
switches.add("--media-cache-size=1");
switches.add("--disk-cache-dir=c:\\cache");
switches.add("enable-file-cookies");
switches.add("--disable-popup-blocking");
capabilities.setCapability("chrome.switches", switches);
ProxyServer proxyServer = new ProxyServer(4443);
try {
proxyServer.start();
Map options = new HashMap<String, String>();
options.put("httpProxy", "ppwebaccess1:8080");
proxyServer.setOptions(options);
} catch (Exception e) {
e.printStackTrace();
}
capabilities.setCapability(CapabilityType.PROXY, proxyServer.seleniumProxy());
RemoteWebDriver driver = new RemoteWebDriver(new URL(startStandaloneServer()), capabilities);
proxyServer.newHar("homepage");
Har har = proxyServer.getHar();
har.writeTo(new File("c:\\test1.har"));
ReporterInterface reporter = SeleniumReporterFactory.getSeleniumReporter();
reporter.log("");
}
private static String startStandaloneServer() {
//Start the server
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setPort(4444);
rc.setTimeoutInSeconds(20);
try {
SeleniumServer seleniumServer = new SeleniumServer(true, rc);
seleniumServer.boot();
seleniumServer.start();
seleniumServer.getPort();
} catch (Exception e) {
e.printStackTrace();
}
return hubURL;
}
}
This is the output I see in the console:
[TestNG] Running:
C:\Documents and Settings\gabell\.IntelliJIdea11\system\temp-testng-customsuite.xml
log4j:WARN No appenders could be found for logger (org.browsermob.proxy.jetty.util.Container).
log4j:WARN Please initialize the log4j system properly.
INFO 03/27 09:27:43 o.o.s.r.s.DriverSer~ - Executing: [new session: {platform=ANY, browserName=chrome, proxy={httpProxy=LP-IT-HC2-1210.inh..., version=, chrome.switches=[--start-maximized, --disable...}] at URL: /session)
Started ChromeDriver
port=33095
version=18.0.1022.0
log=C:\Java\workspace\Automation\chromedriver.log
INFO 03/27 09:27:44 o.o.s.r.s.DriverSer~ - Done: /session
INFO 03/27 09:27:44 o.o.s.r.s.DriverSer~ - Executing: org.openqa.selenium.remote.server.handler.GetSessionCapabilities@19c98d3 at URL: /session/1332840462378)
INFO 03/27 09:27:44 o.o.s.r.s.DriverSer~ - Done: /session/1332840462378
INFO 03/27 09:31:17 o.o.s.r.s.DriverSer~ - Done: /session/1332840462378/url
216069 [main] INFO com.paddypower.reporter.Reporter -
===============================================
Custom suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
I've attached the resulting Har though I'm not sure you'll be able to access it.