问题描述
捕获在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.netty.util.internal.logging.InternalLoggerFactory)。 log4j:WARN请正确初始化log4j系统。 log4j:WARN有关更多信息,请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig。 2020年9月17日下午12:20:09 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:W3C org.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',操作系统名称:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_77 ' 驱动程序信息:org.openqa.selenium.remote.RemoteWebDriver 功能[{networkConnectionEnabled = false,chrome = {chromedriverVersion = 85.0.4183.38(9047dbc2c693f044042bbec5c91401c708c7c26a-refs / branch-heads / 4183 @ {#779}),userDataDir = C:\ Users \ ADMINI〜1 \ AppData \ Local \ Temp \ scoped_dir2 },超时= {implicit = 0,pageLoad = 300000,脚本= 30000},pageLoadStrategy = normal,unhandledPromptBehavior =关闭并通知,strictFileInteractability = false,platform = WINDOWS,proxy = Proxy(手动,http = 192.168.147.1:59693, ssl = 192.168.147.1: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(NativeConstructorAccessorImpl.java:62) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) 在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) 在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586) 在org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:310) 在automation.networklogs.NetWorkLogsWithMobProxySL.main(NetWorkLogsWithMobProxySL.java:77)
解决方法
您得到org.openqa.selenium.WebDriverException: unknown error: net::ERR_PROXY_CONNECTION_FAILED
的原因是BrowserMob代理正在本地计算机上运行,并且无法从Sauce Labs浏览器完全访问。
通常,要在Sauce Labs上进行测试期间访问仅本地资源,请使用Sauce Connect。这在这里不起作用,因为Sauce Connect本身已配置为被测浏览器的系统代理。如果您使用Selenium的代理设置配置其他代理,则Sauce Connect将不再起作用。
您可以执行以下两项操作之一。复杂的解决方案是设置Sauce Connect,并对其进行配置,以使您在计算机上运行的客户端使用BrowserMob代理。 here上有一些文档。
第二个也是更简单的解决方案是使用extended debugging。然后,Sauce Labs会自动为您捕获HAR文件以及JS控制台日志记录。只要您要测试的平台支持该功能,您只需添加所需的功能extendedDebugging
并将其设置为true
。测试完成后,您可以从REST API或Web UI下载HAR文件。
(如果您正在使用BrowserMob代理更改请求,那么您可能会发现extended debugging's traffic editing很有用...尽管我个人建议您避免在测试中出现这种麻烦尽可能)。