我为Arquillian-Drone写了一个扩展,该扩展应该记录测试的HTTP请求并为每种方法创建一个HAR-File。
已创建HAR文件(大部分时间……这似乎有点不稳定),但是它们不包含HTTP请求的URL
我在stackoverflow上找到了另一个问题,该问题描述了相同的结果,但是我的问题没有解决方案。
(门票:BrowserMob Proxy + Selenium: Not receiving any HTTP responses)
实例化器:
@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);
try {
hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + server.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + server.getPort());
} catch (UnknownHostException e1) {
e1.printStackTrace();
System.err.println("invalid Host Address");
}
options.setCapability(CapabilityType.PROXY, seleniumProxy);
options.setAcceptInsecureCerts(true);
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 void prepareHAR(@Observes EventContext<Test> 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<After> 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<HarEntry> 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!");
}
// server.newHar(harFileName);
context.proceed();
}
public void closeServer(@Observes EventContext<AfterClass> context) throws IOException {
server.endHar();
server.stop();
geckoService.stop();
context.proceed();
}
摘录我的HAR:
{“ log”:{“ version”:“ 1.2”,“ creator”:{“ name”:“ BrowserMob
代理”,“版本”:“ 2.1.5”,“评论”:“”},
“页面”:
[{“ id”:“ arquilliantest1561033479096minimumtestAnlegenKrankheit”,
“ startedDateTime”:“ 2019-06-20T12:24:39.316Z”,
“ title”:“ arquilliantest1561033479096minimumtestAnlegenKrankheit”,
“ pageTimings”:{“ comment”:“”},“ comment”:“”}]],
“条目”:
[{“ pageref”:“ arquilliantest1561033479096minimaltestAnlegenKrankheit”,
“ startedDateTime”:“ 2019-06-20T12:24:40.032Z”,
“请求”:
{“ method”:“ POST”,
“ url”:“ https://shavar.services.mozilla.com/downloads?client=navclient-auto-
ffox&appver = 67.0&pver = 2.2“,
“ httpVersion”:“ HTTP / 1.1”,
“饼干”:[],
“标题”:[],
因此似乎网址已以某种方式替换为HAR名称(如另一张票证中所示)
我已经使用过BrowserMob,就像其他票证中所建议的那样:
编译组:“ net.lightbody.bmp”,名称:“ browsermob-core”,版本:“ 2.1.5”
任何想法将不胜感激!
编辑:
我使用以下版本:
Browsermob核心:2.1.5
Arquillian:1.4.1-决赛
无人机:2.5.1
硒:3.14.0
Firefox 67.0
壁虎驱动程序:0.24.0
编辑
我想我知道问题的根源:Firefox的跟踪保护。我试图实现一个Firefox配置文件,该配置文件的“跟踪首选项”保护设置为false,但是以某种方式这些更改将不会显示在浏览器中。更改首选项时是否需要考虑什么?
最佳答案
我现在找到了解决我的问题的解决方案:1.在Drone Initializer创建驱动程序(我使用ManagerStarted)之前,需要初始化BrowserMob(或BrowserUp)ProxyServer并在某个事件中启动;以及2. Firefox浏览器关闭代理用于对本地主机的请求(至少在自动化模式下)。解决此问题的方法是添加此首选项:
options.addPreference(“ network.proxy.allow_hijacking_localhost”,true);
希望这对其他人有帮助:)