1

我一直在到处寻找,但没有找到任何关于如何正确设置代理以在 Sauce labs 上运行脚本并提取 HAR 文件的好的文档。我在嵌入式模式下使用 BMP https://github.com/lightbody/browsermob-proxy#using-with-selenium以及https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy . 我找到了 Sauce 关于通过 BMP 手动设置和运行脚本的文档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");
        }
    }
}

这是我用来启动我的酱汁隧道的命令

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

当我运行 lsof 时,我可以端口 9090 正在主动监听,但在嵌入式模式下我看不到 9091。但是,当我在独立模式下运行它时,我可以看到两个端口,并且一切都在 Sauce labs 上完美运行。当我在嵌入式模式下运行时,我看到了这一点:

在此处输入图像描述

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

提前致谢。

4

1 回答 1

0

我想到了。原来这是版本 2.1.4 中的一个错误。当我升级到 2.1.5 版时,一切正常。

于 2017-09-12T03:31:27.947 回答