Extension for Arquillian Drone to create HAR-File
nscheeren Jul 8, 2019 4:17 AMHi all,
i am currently working on an Extension for Drone. It is supposed to create a HAR-File for each test method. Those files then will be used for creating loadttest-szenarios in gattling for example (a lot of loadtest-tools have import interfaces for this type of file).
my status:
i wrote the extension and HAR-Files are created for each method, but somehow they do not contain http-requests.
my code:
Observer:
public class PerformanceHARHandling {
private BrowserMobProxyServer server = PerformanceDroneInstatiator.getServerIntance();
private GeckoDriverService geckoService = PerformanceDroneInstatiator.getGeckoServiceIntance();
private String harFileName;
public void prepareHAR(@Observes EventContext context) throws IOException {
harFileName = "arquilliantest" + System.currentTimeMillis() + context.getEvent().getTestMethod().getName();
server.newHar(harFileName);
if (server.getHar() != null) {
System.err.print(harFileName + " is prepared");
} else {
throw new RuntimeException("HAR was not created!");
}
context.proceed();
}
public void writeHAR(@Observes EventContext context) throws IOException {
Har har = server.getHar();
String pfad = System.getProperty("user.dir") + File.separator + harFileName + ".har";
if (!server.getHar().getLog().getEntries().isEmpty()) {
File harFile = new File(pfad);
har.writeTo(harFile);
System.err.print(pfad + " is saved");
List entries = har.getLog().getEntries();
for (HarEntry entry : entries) {
System.err.println("Request URL: " + entry.getRequest().getUrl());
System.err.println("Entry response status: " + entry.getResponse().getStatus());
System.err.println("Entry response text: " + entry.getResponse().getStatusText());
}
} else {
throw new RuntimeException("HAR is empty!");
}
context.proceed();
}
public void closeServer(@Observes EventContext context) throws IOException {
server.endHar();
server.stop();
geckoService.stop();
context.proceed();
}
}
Instantiator for Drone
public class PerformanceDroneInstatiator implements Instantiator<firefoxdriver, webdriverconfiguration=""> {
private static volatile BrowserMobProxyServer server = new BrowserMobProxyServer();
private Proxy seleniumProxy;
private static GeckoDriverService geckoService;
private FirefoxOptions options = new FirefoxOptions();
private String hostIp;
// Create and Start Server, Create Proxy in the Server and include this Proxy in
// FirefoxOptions, that are used in the new FirefoxDriver Instance.
@Override
public FirefoxDriver createInstance(WebDriverConfiguration arg0) {
server.setTrustAllServers(true);
server.setHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
server.start();
System.err.println("BrowserMob Proxy running on port: " + server.getPort());
seleniumProxy = ClientUtil.createSeleniumProxy(server);
int port = server.getPort();
try {
hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + port);
seleniumProxy.setSslProxy(hostIp + ":" + port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
System.err.println("invalid Host Address");
}
options.setCapability(CapabilityType.PROXY, seleniumProxy);
options.setAcceptInsecureCerts(true);
options.addPreference("network.proxy.type", 1);
options.addPreference("network.proxy.socks", "127.0.0.1");
options.addPreference("network.proxy.socks_port", port);
geckoService = new GeckoDriverService.Builder()
.usingDriverExecutable(new File("C:/Program Files/GeckoDriver/geckodriver.exe")).usingAnyFreePort()
.build();
try {
geckoService.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new FirefoxDriver(geckoService, options);
}
public static BrowserMobProxyServer getServerIntance() {
return server;
}
public static GeckoDriverService getGeckoServiceIntance() {
return geckoService;
}
@Override
public int getPrecedence() {
return 0;
}
}
I use the following versions:
- Browsermob Core: 2.1.5
- Arquillian : 1.4.1-Final
- Drone: 2.5.1
- Selenium: 3.14.0
- Firefox 67.0
- GeckoDriver: 0.24.0
The Stackoverflow-Ticket (java - HAR incomplete when using BrowserMob Proxy in an Extension for Arquillian Drone - Stack Overflow ) i created for this topic.
Any ideas would be much appreciated
edit: i think the problem is the tracking protection function of firefox. I tried creating a FirefoxProfile (that has this preference set to false) in my code, but somehow my configurations don´t work. (there is no change in the settings) Is there something i need to consider when changing settings in firefox?
Nachricht geändert durch Nicole Scheeren