当前位置: 首页 > 知识库问答 >
问题:

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

邵星河
2023-03-14

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

共有1个答案

俞俊逸
2023-03-14

我想通了。原来这是2.1.4版本中的一个bug。当我升级到2.1.5版本时,一切都正常工作。

 类似资料:
  • 问题内容: 我用and 事件编写了一些Javascript代码。我想用selenium测试。我刚刚发现了TouchActions类,该类似乎正是我想要的方法。 我的测试是通过RemoteWebDriver运行的: 驱动程序将是a 或最终将是a ,而不是a 。 当我尝试使用以下方法初始化动作时: 我收到强制转换错误: java.lang.ClassCastException:org.openqa.s

  • 问题内容: 如何从公司代理后面通过RemoteWebDriver连接到硒网格,例如Br​​owserStack? 被测应用程序在代理之外,可以从BrowserStack免费访问。 问题答案: 我设法根据链接的问题中的可接受答案得到了一些解决方案,如果有人遇到相同问题,这是我的实现: 例 MyHttpClientFactory

  • 如何从公司代理后面通过RemoteWebDriver连接到selenium网格(如BrowserStack)? 被测应用程序位于代理之外,可以从BrowserStack自由访问。 这个使用Selenium RemoteWebDriver隐藏公司代理(Java)stackoverflow的问题问了同样的问题,但我无法按照公认的答案回答。

  • 我想在Azure函数v4(.net 6)中使用Serilog(日志应发送到Datadog)。为此,我安装了以下nuget软件包: 以下是启动中的配置。cs类: 基本上日志记录工作正常,但所有日志语句都写入两次(与Datadog和控制台相差几毫秒)。 显然,我在配置上做了一些根本的错误。我不使用appsettings.json,Serilog的配置只在代码中进行。我搜索了整个互联网,几乎阅读了关于S

  • 问题内容: 我试图在一些远程自动化服务(Sauce Labs,Browserstack等)上运行Selenium测试,并遇到通过我的公司防火墙攻击其API的问题。 请注意,我要测试的应用程序 不在 此防火墙后面,可以公开访问。 问题似乎是Selenium的管道将url中的user:key解释为代理凭据,因此它永远不会离开我们的网络。是否有任何特定的技巧来配置它?似乎在后台使用了Apache Htt

  • 我正在尝试在一些远程自动化服务(Sauce Labs、Browserstack等)上运行Selenium测试,并通过我的公司防火墙遇到了影响其API的问题。 请注意,我尝试测试的应用程序不在此防火墙后面,它是可公开访问的。 问题似乎是Selenium的管道将用户:输入url作为代理凭据,所以它永远不会离开我们的网络。是否有任何特定的技巧来配置此功能?它似乎在幕后使用Apache HttpClien