如何使用RemoteWebDriver / Sauce Labs / Sauce Connect正确设置BrowserMobProxy / Selenium代理?

时间:2017-09-08 22:08:51

标签: java saucelabs browsermob-proxy

我一直在寻找各处,并且没有找到任何关于如何正确设置代理以在Sauce实验室上运行脚本并提取HAR文件的良好文档。我在嵌入模式https://github.com/lightbody/browsermob-proxy#using-with-selenium中使用BMP以及https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy。我找到了关于通过BMP手动设置和运行脚本的Sauce文档https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy+with+an+Additional+Proxy+Setup,但是他们的文档没有说明如何仅通过独立模式在嵌入模式下设置脚本。这是我的设置:

我的PAC文件

function FindProxyForURL(url, host) {
    if (shExpMatch(host, "*.miso.saucelabs.com") ||
        shExpMatch(host, "saucelabs.com")) {
        // KGP and REST connections. Another proxy can also be specified.
        return "DIRECT";
    }

    // Test HTTP traffic, route it through the local BrowserMob proxy.
    return "PROXY localhost:9091";
}

BMP设置

package com.grainger.Framework;

import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.grainger.Automation.Utilities;
import com.grainger.Build.BuildVariables;

import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;

public class BrowserMobProxyImpl {
     public static Logger log = Logger.getLogger(BrowserMobProxyImpl.class.getName());
    private static BrowserMobProxy MOB_PROXY_SERVER;
    private static Proxy SELENIUM_PROXY;

    /**
     * @author xsxg091
     * @return
     */
    public static void startBrowserMobProxyServer(){
         // start the proxy
        MOB_PROXY_SERVER = getProxyServer();
        // get the Selenium proxy object
        SELENIUM_PROXY = getSeleniumProxy(MOB_PROXY_SERVER);
    }

    /**
     * @author xsxg091
     * @return
     */
    public static BrowserMobProxy getProxyServer() {
        BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.setTrustAllServers(true); 
        proxy.start(9090);
        return proxy;
   }

    /**
     * @author xsxg091
     * @param proxyServer
     * @return
     */
    public static Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);;
        try {
            String hostIp = Inet4Address.getLocalHost().getHostAddress();
            seleniumProxy.setHttpProxy(hostIp + ":" + Integer.toString(9091));
            seleniumProxy.setSslProxy(hostIp + ":" + Integer.toString(9091));
            seleniumProxy.setAutodetect(false);
        } catch (UnknownHostException e) {
            log.error("Error initializing Selenium Proxy");
        }
        return seleniumProxy;
    }

    /**
     * @author xsxg091
     * @param tcName
     * @param capabilities
     */
    public static void setSeleniumProxy(DesiredCapabilities capabilities){
        if(BuildVariables.amICapturingNetworkTraffic()){
            capabilities.setCapability(CapabilityType.PROXY, SELENIUM_PROXY);
        }
    }

    /**
     * @author xsxg091
     * @param tcName
     * @param capabilities
     */
    public static void stopBrowserMobProxyServer(){
        MOB_PROXY_SERVER.stop();
    }

    /**
     * @author xsxg091
     * @return
     */
    public static void getHarFile(String fileName) {
        // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
        MOB_PROXY_SERVER.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
        MOB_PROXY_SERVER.newHar(fileName);
        try {
            // get the HAR data
            Har pageHarFile = MOB_PROXY_SERVER.getHar();
            File harFile = new File(Utilities.getWorkSpace()+"//"+fileName+".har");
            pageHarFile.writeTo(harFile);
        } catch (IOException e) {
            log.error("Unable to store Har File");
        }
    }
}

这是我用来启动我的Sauce Tunnel的命令

bin/sc -u ****** -k *********** -i Tunnel_Testing -v --pac file:///<path-to-pac-file>/BrowserMobProxy/browserMob.js

当我运行lsof时,我可以端口9090正在主动收听,但我没有看到9091处于嵌入式模式。但是,当我在独立模式下运行时,我可以看到两个端口,一切都在Sauce实验室中完美运行。我在嵌入模式下运行时看到了这个:

enter image description here

我做错了什么?任何帮助将不胜感激。如果有任何不清楚的地方,请告诉我!

提前致谢。

1 个答案:

答案 0 :(得分:0)

我明白了。原来这是版本2.1.4中的错误。当我升级到2.1.5版本时,一切都按预期工作。