首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获Sauce实验室中运行的浏览器网络日志

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

Stack Overflow用户
提问于 2020-09-17 14:31:50
回答 1查看 225关注 0票数 0

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

我使用以下代码来捕获在Saucelabs中运行的浏览器网络日志。它在本地机器上工作,能够在本地捕获日志,但在saucelabs中运行时不能捕获。

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

++++++++++本地工作良好的+++++++++

代码语言:javascript
复制
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();

            }

        }

    }

}

+++++++++ ++++++++++ saucelabs不起作用+++++++++

代码语言:javascript
复制
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:警告找不到记录器(io.netty.util.internal.logging.InternalLoggerFactory).的附加程序log4j :警告请正确初始化log4j系统。log4j:WARN更多信息请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig。Sep 17,2020 12:20:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession信息:检测到的方言: 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',os.name:'Windows 10',os.arch:‘(9047dbc2c693f044042bbec5c91401c708c7c26a-refs/branch-heads/4183@{#779}),userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir2300_1366152112},64’,os.version:'10.0',java.version:'1.8.0_77‘驱动程序信息: org.openqa.selenium.remote.RemoteWebDriver功能{networkConnectionEnabled=false,chrome={chromedriverVersion=85.0.4183.38 amd64 timeouts={implicit=0,pageLoad=300000,script=30000},pageLoadStrategy=normal,unhandledPromptBehavior=dismiss和notify,strictFileInteractability=false,platform=WINDOWS,proxy=Proxy(manual,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=truewebauthn:virtualAuthenticators=true}会话ID: sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) at org.openqa.selenium.remote.http的sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法的ebf16fbb5f7a4f5d8a91755b8a0966f9 )。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)

EN

回答 1

Stack Overflow用户

发布于 2020-11-23 16:38:39

你得到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代理。这里有一些关于here的文档。

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

(如果您正在使用BrowserMob代理来更改传入的请求,那么您可能会发现extended debugging's traffic editing很有用……(尽管我个人建议在测试中尽可能避免这种摆弄。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63932467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档