【问题标题】:How Can I get network files in google chrome with selenium?如何使用硒在谷歌浏览器中获取网络文件?
【发布时间】:2019-04-06 23:56:17
【问题描述】:

我在项目中使用 selenium 进行测试,但我遇到了问题。 检查元素时,我需要从谷歌浏览器获取网络文件。

在本节中,我需要这些文件,它们是 JSON 文件,我需要它的信息。

             //String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
        String scriptToExecute = "var network = performance.getEntries() || {}; return network;";
        java.util.List<String> s= executeJavaScript(scriptToExecute)

String s 属性,给我返回一个奇怪的网络奇怪对象列表,对我来说不是很好的信息。

这是我的问题,我需要 JSON 文件,但我的代码返回了其他内容。

【问题讨论】:

    标签: java selenium automation automated-tests


    【解决方案1】:

    使用BrowserMobProxyServer 和 selenium 来获取每个网络 HAR 格式的网络详细信息。

    // Set up BrowserMobProxyServer while initiation driver
    proxy = new BrowserMobProxyServer();
    proxy.start(0);
    
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
    
    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    
    driver = new ChromeDriver(capabilities);
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    
     proxy.newHar("google.com");
    
    // Do all your navigation in selenium/ selenide code
    
    driver.get("http://google.com");
    
    // After navigation, you can find network details stored HAR
    Har har = proxy.getHar();
    

    如果需要,您可以在退出驱动程序之前将其存储到文件中,

    Har har = proxy.getHar();
    File harFile = new File(sFileName);
    har.writeTo(harFile);
    
    proxy.stop();
    driver.quit();
    

    【讨论】:

      【解决方案2】:

      1.创建一个具有监控网络调用功能的网络驱动程序。

          public static WebDriver getDriver() {
                  ChromeOptions options = new ChromeOptions();
                  System.setProperty("webdriver.chrome.driver", Driver local path);
      
                  DesiredCapabilities cap = DesiredCapabilities.chrome();
                  LoggingPreferences logPrefs = new LoggingPreferences();
                  logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
                  cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
      
                  Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
                  perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools"); // comma-separated trace categories
                  options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
                  cap.setCapability(ChromeOptions.CAPABILITY, options);
      
                  return new ChromeDriver(cap);
              }
      
      1. 现在使用以下代码检索您的日志条目。

        for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)){
                        System.out.println(entry.toString());
        }
        

      3.Result 将采用以下 JSON 格式。

      {
          "webview": <originating WebView ID>,  
          "message": { "method": "...", "params": { ... }} // DevTools message.
      }
      

      【讨论】:

        【解决方案3】:

        你可以用return JSON.stringify(network)替换return network。那么executeJavaScript(scriptToExecute)会返回json,但它仍然是String如果你不通过java改成json .你可以使用org.json.Just

        JSONArray netData = new JSONArray(driver.executeScript(scriptToExecute).toString());

        【讨论】:

          猜你喜欢
          • 2017-04-16
          • 2013-12-22
          • 1970-01-01
          • 2020-12-21
          • 2017-06-22
          • 2018-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多