1

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

我正在尝试使用 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":"","pageTimings":{}}],"entries":[]}}

当我看到教程时,它看起来很简单!但不适合我。我支持公司代理,也许这就是导致问题的原因......

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

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();

    }

}

(当然 [MY_PROXY_HERE] 只是不给出真实姓名:))

谢谢 !

4

1 回答 1

2

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

只是想知道你从表演时间中追求什么?您可以直接从浏览器实例中从Navigation Timing API检索数据吗?这为您提供了页面加载性能时间的详细细分。如果您的开发团队愿意插入标记,您还可以使用它来捕获嵌入式 AJAX 调用等的时间。(这就是我正在做的)

取回数据只是一个简单的 JavaScript 注入。在我的情况下(Python - 'driver' 是我的 webdriver 实例) - driver.execute_script('return window.performance.timing')。它返回一个包含完整页面加载时间细分的字典对象。

只是问,因为这似乎比搞乱代理要简单得多......

于 2014-09-16T13:25:45.593 回答