使用Selenium和browermob代理的详细计时给出了空的har文件

使用Selenium和browermob代理的详细计时给出了空的har文件,selenium,browsermob,Selenium,Browsermob,也许有人能帮我,我已经搜索了几个小时,在任何网站/博客/常见问题解答/上都找不到解决方案 我试图通过Selenium和Browsermob代理获取页面的详细时间安排。但生成的HAR文件始终为空pageTitle、pageTimings和条目,如: {“log”:{“version”:“1.2”,“creator”:{“name”:“BrowserMob Proxy”,“version”:“2.0”},“pages”:[{“id”:“assertselenium.com”,“startedDate

也许有人能帮我,我已经搜索了几个小时,在任何网站/博客/常见问题解答/上都找不到解决方案

我试图通过Selenium和Browsermob代理获取页面的详细时间安排。但生成的HAR文件始终为空pageTitle、pageTimings和条目,如:

{“log”:{“version”:“1.2”,“creator”:{“name”:“BrowserMob Proxy”,“version”:“2.0”},“pages”:[{“id”:“assertselenium.com”,“startedDateTime”:“2014-08-26T15:45:49.134+0000”,“title”:“,“pagetimes”:{}],“entries”:[]}

当我看到教程时,它看起来非常简单!但对我来说不是。 我支持一个公司代理,也许这就是问题的原因

我的代码是(修改了一百次…):

导入java.io.File;
导入java.util.HashMap;
导入java.util.concurrent.TimeUnit;
导入net.lightbody.bmp.proxy.ProxyServer;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.Proxy;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.firefox.FirefoxProfile;
导入org.openqa.selenium.remote.CapabilityType;
导入org.openqa.selenium.remote.DesiredCapabilities;
公开课范例{
公共静态void main(字符串[]args)引发异常{
ProxyServer服务器=新的ProxyServer(4444);
HashMap选项=新建HashMap();
server.start();
setOptions(选项);
setCaptureHeaders(true);
server.setCaptureContent(true);
Proxy Proxy=新代理();
//将其配置为所需的功能
DesiredCapabilities=新的DesiredCapabilities();
能力.setCapability(CapabilityType.PROXY,PROXY);
FirefoxProfile profile=新的FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateAissuer(true);
setPreference(“network.proxy.http”,“[MY\u proxy\u HERE]”;
setPreference(“network.proxy.http_port”,8080);
setPreference(“network.proxy.ssl”,“[MY\u proxy\u HERE]”;
setPreference(“network.proxy.ssl_port”,8080);
profile.setPreference(“network.proxy.type”,1);
setPreference(“network.proxy.no_proxies_on”,”);
能力。设置能力(FirefoxDriver.PROFILE,PROFILE);
//启动浏览器
WebDriver=新的FirefoxDriver(功能);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//创建一个标签为“apple.com”的新HAR
server.newHar(“assertselenium.com”);
//打开yahoo.com
驱动程序。获取(“http://assertselenium.com");
//驱动程序。获取(“http://assertselenium.com/2012/10/30/transformation-from-manual-tester-to-a-selenium-webdriver-automation-specialist/");
driver.findElement(By.id(“searchform”))
.findElement(按.className(“字段”))
.sendKeys(新字符串[]{“test selenium!”});
driver.findElement(By.id(“searchform”))
.findElement(按.className(“提交”))。单击();
//新的PerformanceMing((JavascriptExecutor)驱动程序,server.getHar());
((JavascriptExecutor)驱动程序)
.executeScript(“var performance=window.performance | |{};”
+“变量计时=性能。计时| |{};”
+“返回时间;”;
server.getHar().writeTo(
新文件(“C:/prj/SeleniumTest/harfiles/har.txt”);
server.stop();
driver.quit();
}
}
(当然[MY_PROXY_HERE]只是不提供真实姓名:))


谢谢大家!

仍然没有50个代表,所以回答而不是评论

只是想知道你想要的是什么?您是否可以直接从浏览器实例从中检索数据?这将为您提供页面加载的详细分类。如果您的开发团队愿意插入标记,那么您还可以使用它来捕获嵌入式AJAX调用等的计时。(这就是我正在做的)

获取数据只是一个简单的JavaScript注入。在我的例子中(Python——其中“driver”是我的webdriver实例)——driver.execute_脚本('return window.performance.timing')。它返回一个带有完整页面加载时间细分的dictionary对象


提问似乎比摆弄代理要简单得多…

首先,感谢您的回答!我想要的是使用一个工具来轻松创建一个漂亮的HAR文件,该文件包含所有计时,然后可以显示在自定义界面中或使用harviewer之类的工具。这似乎是最简单的方法,因为它被大量记录在案,作为一种神奇的解决方案随处可见,但显然不是。我想我们将尝试导航计时API!我使用python而不是Java,但我也得到了空的har文件。我应该如何解决这个问题?
import java.io.File;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import net.lightbody.bmp.proxy.ProxyServer;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Example {

    public static void main(String[] args) throws Exception {

        ProxyServer server = new ProxyServer(4444);

        HashMap<String, String> options = new HashMap<String, String>();

        server.start();
        server.setOptions(options);
        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        Proxy proxy = new Proxy();

        // configure it as a desired capability
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(true);
        profile.setPreference("network.proxy.http", "[MY_PROXY_HERE]");
        profile.setPreference("network.proxy.http_port", 8080);
        profile.setPreference("network.proxy.ssl", "[MY_PROXY_HERE]");
        profile.setPreference("network.proxy.ssl_port", 8080);
        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("network.proxy.no_proxies_on", "");
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // create a new HAR with the label "apple.com"
        server.newHar("assertselenium.com");

        // open yahoo.com
        driver.get("http://assertselenium.com");
        // driver.get("http://assertselenium.com/2012/10/30/transformation-from-manual-tester-to-a-selenium-webdriver-automation-specialist/");

        driver.findElement(By.id("searchform"))
                .findElement(By.className("field"))
                .sendKeys(new String[] { "test selenium!" });

        driver.findElement(By.id("searchform"))
                .findElement(By.className("submit")).click();

        // new PerformanceTiming((JavascriptExecutor) driver, server.getHar());

        ((JavascriptExecutor) driver)
                .executeScript("var performance = window.performance || {};"
                        + "var timings = performance.timing || {};"
                        + "return timings;");

        server.getHar().writeTo(
                new File("C:/prj/SeleniumTest/harfiles/har.txt"));

        server.stop();
        driver.quit();

    }

}