捕获Sauce实验室中运行的浏览器网络日志



捕获在Saucelabs中运行的浏览器网络日志。

我使用以下代码来截取Saucelabs中运行的浏览器网络日志。它在本地机器中工作,能够在本地捕获日志,但在炖锅实验室中运行时无法捕获。

这是代码。有人能帮我照一下吗?

++++++++++本地工作精细++++++++

package automation.networklogs;
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;
import org.openqa.selenium.By;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
public class NetWorkLogsWithMobProxyLocal {
public static String sFileName = "C:/Users/DELL/Desktop/New folder/harfile2.har";
public static WebDriver driver;
public static BrowserMobProxy proxy;

public static void main(String[] args) throws InterruptedException, MalformedURLException {
try {
proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
try {
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
} catch (UnknownHostException e) {
e.printStackTrace();
}
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/DELL/IdeaProjects/prodigy-client-repo/zenq/src/test/resources/browser_drivers/chromedriver.exe" );
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);
driver = new ChromeDriver(options);
driver.manage().window().maximize();
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.newHar("yahoo.com" );
driver.get("https://yahoo.com" );
Thread.sleep(10);
Har har = proxy.getHar();
File harFile = new File(sFileName);
try {
har.writeTo(harFile);
} catch (IOException ex) {
System.out.println(ex.toString());
System.out.println("Could not find file " + sFileName);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("ENTER IN EXCEPTION firstName" );
} finally {
if (driver != null) {
proxy.stop();
driver.quit();
}
}
}
}

+++++++++++++++++++茶托实验室不工作

package automation.networklogs;
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;
import org.openqa.selenium.By;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
public class NetWorkLogsWithMobProxySL {
public static String sFileName = "C:/Users/DELL/Desktop/New folder/harfile3.har";
public static WebDriver driver;
public static BrowserMobProxy proxy;

public static void main(String[] args) throws InterruptedException, MalformedURLException {
try {
proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
MutableCapabilities sauceOpts = new MutableCapabilities();
sauceOpts.setCapability("username", "username" );
sauceOpts.setCapability("accessKey", "acceskey" );
sauceOpts.setCapability("seleniumVersion", "3.141.59" );
sauceOpts.setCapability("name", "test" );
sauceOpts.setCapability("proxy", hostIp+":"+proxy.getPort() );
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("browser.visible", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.merge(capabilities);
MutableCapabilities cap = new MutableCapabilities();
cap.setCapability("sauce:options", sauceOpts);
cap.setCapability("goog:chromeOptions", options);
cap.setCapability("platformName", "Windows 10" );
cap.setCapability("browserName", "chrome" );
cap.setCapability("browserVersion", "latest" );
String sauceURL = "https://ondemand.saucelabs.com:443/wd/hub";
driver = new RemoteWebDriver(new URL(sauceURL), cap);
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.newHar("yahoo.com" );
driver.get("https://yahoo.com" );
Thread.sleep(10);
Har har = proxy.getHar();
File harFile = new File(sFileName);
try {
har.writeTo(harFile);
} catch (IOException ex) {
System.out.println(ex.toString());
System.out.println("Could not find file " + sFileName);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("ENTER IN EXCEPTION firstName" );
} finally {
if (driver != null) {
proxy.stop();
driver.quit();
}
}
}
}

+++++++++在Saucelabs 中运行时出现以下错误

log4j:WARN找不到记录器(io.nety.util.internal.logging.InternalLoggerFactory(的附加程序。log4j:WARN请正确初始化log4j系统。log4j:警告请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。2020年9月17日下午12:20:09 org.openqa.selenium.remote.ProtocolHandshake createSession信息:检测到的方言:W3Corg.openqa.selenium.WebDriverException:未知错误:net::ERR_PROXY_CONNECTION_FAILED(会话信息:chrome=85.0.4183.83(构建信息:版本:"3.6.0",修订:"6fbf3ec767",时间:"2017-09-27T15:28:36.4Z"系统信息:主机:"RAVILAPTOP",ip:"192.168.147.1",os.name:"Windows 10",os.arch:"amd64",os.version:"10.0",java.version:"1.8.0_77"驱动程序信息:org.openqa.selenium.remote.RemoteWebDriver功能[{networkConnectionEnabled=false,chrome={chromediverVersion=85.0.4183.38(9047dbc2c693f044042bbec5c91401c708c7c26a refs/branch-heads/4183@{#779}(,userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir2300_1366152112},超时={implicit=0,pageLoad=300000,script=30000},pageLoadStrategy=normal,unhandledPromptBehavior=disse and notify,strictFileInteractibility=false,platform=WINDOWS,proxy=proxy(manual,http=192.168.147.1:59693,ssl=192.168.147.17:59693(,goog:chromeOptions={debuggerAddress=localhost:49770},browserVersion=85.0.4183.83,acceptInsecureCerts=false,browserName=chrome,javascriptEnabled=true,platformName=WINDOWS、setWindowRect=true,webauthn:virtualAuthenticators=true}]会话ID:ebf16fbb5f7a4f5d8a91755b8a0966f9在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法(在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessor Impl.java:62(在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessor Impl.java:45(位于java.lang.reflect.Constructure.newInstance(Constructor.java:423(网址:org.openqa.selene.remote.http.W3CttpResponseCodec.createException(W3CHttpResponceCodec.java:185(网址:org.openqa.selene.remote.http.W3CttpResponseCodec.decode(W3CHttpResponceCodec.java:120(网址:org.openqa.selene.remote.http.W3CttpResponseCodec.decode(W3CHttpResponceCodec.java:49(网址:org.openqa.selene.remote.HttpCommandExecution.execute(HttpCommandExecutior.java:164(网址:org.openqa.selene.remote.RemoteWebDriver.exexecute(RemoteWebDriver.java:586(网址:org.openqa.selene.remote.RemoteWebDriver.get(RemoteWebDriver.java:310(在automation.networklogs.NetWorkLogsWithMobProxySL.main(networklogs WithMobProxy SL.java:77(

您得到org.openqa.selenium.WebDriverException: unknown error: net::ERR_PROXY_CONNECTION_FAILED是因为BrowserMob Proxy正在您的本地机器上运行,并且从Sauce Labs浏览器完全无法访问。

通常,要在Sauce Labs测试期间访问仅限本地的资源,您可以使用Sauce Connect。这在这里不起作用,因为Sauce Connect本身被配置为测试浏览器的系统代理。如果使用Selenium的代理设置配置不同的代理,Sauce Connect将不再工作。

你可以做两件事中的一件。复杂的解决方案是设置Sauce Connect,并对其进行配置,使您在机器上运行的客户端使用BrowserMob Proxy。这里有一些文档。

第二种更简单的解决方案是使用扩展调试。然后,Sauce Labs将自动为您捕获HAR文件,以及JS控制台日志记录。只要您正在测试的平台支持它,您只需要添加所需的功能extendedDebugging并将其设置为true。测试完成后,您可以从RESTneneneba API或WebUI下载HAR文件。

(如果你使用BrowserMob代理在请求通过时更改请求,那么你可能会发现扩展调试的流量编辑很有用……尽管我个人建议尽可能避免在测试中进行这种篡改。(

最新更新