当前位置: 首页 > 面试题库 >

BrowserMob代理+ Selenium:未收到任何HTTP响应

闻人景澄
2023-03-14
问题内容

具有依赖项的Maven / Java项目:

      <dependencies>
<dependency>
  <groupId>net.lightbody.bmp</groupId>
  <artifactId>browsermob-core</artifactId>
  <version>2.1.4</version>
  <scope>test</scope>
</dependency>


<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.4</version>
</dependency>

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.7.1</version>
</dependency>

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>23.0</version>
</dependency>
</dependencies>

码:

公共类bmp_Selenium {

@Test
public void test() throws Exception {
    String chromedriverPath = System.getProperty("user.dir") + "/chromedriver";
    System.setProperty("webdriver.chrome.driver", chromedriverPath);


    BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy
    System.out.println("BrowserMob Proxy running on port: " + proxy.getPort());

    Proxy seleniumProxy = getSeleniumProxy(proxy);

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

    WebDriver driver = new ChromeDriver(capabilities);

    proxy.newHar("foo");
    driver.get("http://www.google.com");


    Har har = proxy.getHar();

    // Write HAR Data in a File
    String harFilePath = System.getProperty("user.dir") + "/hars/ww.har";
    File harFile = new File(harFilePath);
    try {
        har.writeTo(harFile);
    } catch (IOException ex) {
         System.out.println (ex.toString());
         System.out.println("Could not find file " + harFilePath);
    }

    List<HarEntry> entries = har.getLog().getEntries();
    for (HarEntry entry : entries) {
        System.out.println("Request URL: " + entry.getRequest().getUrl());
        System.out.println("Entry response status: " + entry.getResponse().getStatus());
        System.out.println("Entry response text: " + entry.getResponse().getStatusText());

    }


    proxy.stop();
    driver.quit();
}


public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
    try {
        String hostIp = Inet4Address.getLocalHost().getHostAddress();
        seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
        seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        Assert.fail("invalid Host Address");
    }
    return seleniumProxy;
}

public BrowserMobProxy getProxyServer() {
    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.setTrustAllServers(true);
    proxy.setHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    proxy.start();
    return proxy;
}

}

控制台输出:

在端口:57547上运行的BrowserMob代理在端口47157上启动ChromeDriver
2.33.506106(8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2)仅允许本地连接。2017年11月30日下午4:48:33
org.openqa.selenium.remote.ProtocolHandshake createSession
INFO:检测到方言:OSS请求URL:http :
//www.google.com/ 输入响应状态:0输入响应文本:请求URL
:http :
//www.google.com/
输入响应状态:0输入响应文本:请求URL:http :
//www.google.com/ 输入响应状态:0输入响应文本:请求URL:http://
www .google.com / 输入响应状态:0输入响应文本:

Har文件:

{
  "log": {
    "version": "1.2",
    "creator": {
      "name": "BrowserMob Proxy",
      "version": "2.1.4",
      "comment": ""
    },
    "pages": [{
        "id": "foo",
        "startedDateTime": "2017-11-30T21:48:34.064Z",
        "title": "foo",
        "pageTimings": {
          "comment": ""
        },
        "comment": ""
      }
    ],
    "entries": [{
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.210Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.224Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.225Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.327Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 443,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }
    ],
    "comment": ""
  }
}

问题答案:

根据GitHub用户mediga的建议,修改BMP依赖关系解决了该问题:

取代这个

<dependency>
  <groupId>net.lightbody.bmp</groupId>
  <artifactId>browsermob-core</artifactId>
  <version>2.1.4</version>
  <scope>test</scope>
</dependency>

>     <dependency>
>       <groupId>net.lightbody.bmp</groupId>
>       <artifactId>browsermob-core</artifactId>
>       <version>2.1.5</version>
>     </dependency>

https://github.com/lightbody/browsermob-
proxy/issues/689#issuecomment-348620752



 类似资料:
  • 具有依赖项的Maven/Java项目: 代码: 公共类bmp_Selenium{ } 控制台输出: BrowserMob代理运行在端口:57547上启动chrome driver 2 . 33 . 506106(8 a 06 c 39 c 4582 fbfbfbab 6966 db B1 c 38 a 9173 BF B1 a 2)在端口47157上只允许本地连接。2017年11月30日下午4:4

  • 我开始在Selenium上写一些测试,并认为如果我能在登录我们的网站时获得性能指标,那就太好了。我遇到了BrowserMob-Proxy。我开始从Lightbody的github页面复制前五行,我已经得到了错误消息:“ProxyServer无法解析为类型”和“代理无法解析为类型”。 谁能告诉我这里还缺什么吗? 这是pom。xml: 在这个类中,我试图让方法“开始”和“结束”(“开始”还没有完成,我

  • 下面是关于如何设置messenger机器人的Facebook教程-使用ngrok设置我的webhook。本地测试一切顺利,但在向bot发送消息时仍然没有收到任何响应。 韩国https://ngrok.com/ facebook教程https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start/

  • 我正在我的. Net Web应用程序上运行负载测试,使用Jeter。应用程序过程:启动-登录-开始测试-回答问题 直到500个用户或有时750个用户,测试才成功运行。但是当我增加负载时,我得到一个错误: 11.111.111.111是我的服务器IP地址。 我已经将jmeter.batch文件堆内存增加到HEAP=-Xms1g-Xmx4g-XX: MaxMetaspaceSize=256m Apac

  • 问题内容: 我已经使用Selenium2 / WebDriver编写了测试,并且想要测试HTTP请求是否返回HTTP 403 Forbidden。 是否可以使用Selenium WebDriver获取HTTP响应状态代码? 问题答案: 一言以蔽之。无法使用Selenium WebDriver API。此问题已在项目的问题跟踪器中进行了讨论,该功能不会添加到API中。

  • 我正在尝试将browsermob设置为在我的selenium项目中工作。我正在寻找一种使用ChromeOptions来设置代理的方法,但是所有的资源都告诉我要使用ChromeOptions来处理其他的事情,然后在实例化一个新的ChromeDriver实例之前将它转换为DesiredCapabilities。 这是我的代码: 我使用的是maven存储库中的Webdriver版本2.44。这是我得到的