gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:01 26 4
gpt4 key购买 nike

我到处寻找,但没有找到任何关于如何正确设置代理以在 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 实验室上完美运行。当我在嵌入模式下运行时,我看到了这个:

enter image description here

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

提前致谢。

最佳答案

我明白了。原来这是2.1.4版本的一个bug。当我升级到版本 2.1.5 时,一切都按预期运行。

关于java - 如何使用 RemoteWebDriver/Sauce Labs/Sauce Connect 正确设置 BrowserMobProxy/Selenium 代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com