<tfoot id='mXh0y'></tfoot>
  • <i id='mXh0y'><tr id='mXh0y'><dt id='mXh0y'><q id='mXh0y'><span id='mXh0y'><b id='mXh0y'><form id='mXh0y'><ins id='mXh0y'></ins><ul id='mXh0y'></ul><sub id='mXh0y'></sub></form><legend id='mXh0y'></legend><bdo id='mXh0y'><pre id='mXh0y'><center id='mXh0y'></center></pre></bdo></b><th id='mXh0y'></th></span></q></dt></tr></i><div id='mXh0y'><tfoot id='mXh0y'></tfoot><dl id='mXh0y'><fieldset id='mXh0y'></fieldset></dl></div>

          <bdo id='mXh0y'></bdo><ul id='mXh0y'></ul>
      1. <legend id='mXh0y'><style id='mXh0y'><dir id='mXh0y'><q id='mXh0y'></q></dir></style></legend>
      2. <small id='mXh0y'></small><noframes id='mXh0y'>

        防止在 selenium webdriver 测试中加载外部内容

        prevent external content to be loaded in selenium webdriver test(防止在 selenium webdriver 测试中加载外部内容)
          <tbody id='XGglK'></tbody>
        • <bdo id='XGglK'></bdo><ul id='XGglK'></ul>

            <i id='XGglK'><tr id='XGglK'><dt id='XGglK'><q id='XGglK'><span id='XGglK'><b id='XGglK'><form id='XGglK'><ins id='XGglK'></ins><ul id='XGglK'></ul><sub id='XGglK'></sub></form><legend id='XGglK'></legend><bdo id='XGglK'><pre id='XGglK'><center id='XGglK'></center></pre></bdo></b><th id='XGglK'></th></span></q></dt></tr></i><div id='XGglK'><tfoot id='XGglK'></tfoot><dl id='XGglK'><fieldset id='XGglK'></fieldset></dl></div>

            <small id='XGglK'></small><noframes id='XGglK'>

            <tfoot id='XGglK'></tfoot>

                  <legend id='XGglK'><style id='XGglK'><dir id='XGglK'><q id='XGglK'></q></dir></style></legend>
                • 本文介绍了防止在 selenium webdriver 测试中加载外部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  问题:

                  是否可以告诉由 selenium webdriver 控制的浏览器不从外部源加载任何内容,或者不从给定的域列表中加载资源?

                  背景:

                  我有一个网页,我用 selenium webdriver 编写了一个基于 java 的测试脚本 - 我无法更改页面,我只需要编写测试.网站从另一个域加载的一些外部内容存在问题.外部的东西是我的测试实际上不需要的一些javascript代码,但有问题的页面包括.现在的问题.有时外部资源非常慢,阻止 webdriver 在给定的页面加载超时(20 秒)内加载页面.我的测试实际上会运行良好,因为页面实际上已加载 - 所有 html 都在那里,所有内部脚本都已加载并且可以工作.

                  I have a webpage against which I write a java based test script with selenium webdriver - I can't change the page, I just have to write the tests. There are issues with some external content that the site loads from another domain. The external stuff is some javascript code that is actually not needed for my tests, but that the page in question includes. Now the problem. Sometimes the external sources are super slow, preventing the the webdriver to load the page within the given page load timeout (20 sec). My tests actually would run fine, because the page is in fact loaded - all html is there, all internal scripts are loaded and would work.

                  关于这个的随机想法:

                  有针对不同浏览器的扩展可以满足我的要求,但我需要使用几种浏览器运行我的测试,即 chrome、firefox 和 phantomjs.并且没有像 phantomjs 扩展这样的东西.如果可能的话,我需要一个纯粹基于 webdriver 技术的解决方案.不过,我愿意为每个浏览器编写单独的解决方案.

                  There are extensions for different browsers that would do what I ask, but I need to run my tests with several browsers, namely chrome, firefox and phantomjs. And there is no such thing like phantomjs extensions. I need a solution that is purely based on the webdriver technology if possible. I am willing to program a separate solution for each browser, though.

                  我很感激有关如何解决此问题的任何想法.

                  I appreciate any idea about how to address this.

                  推荐答案

                  解决方案是使用代理.Webdriver 与 browsermob 代理集成得非常好:http://bmp.lightbody.net/

                  Solution is to use proxy. Webdriver integrates very well with browsermob proxy: http://bmp.lightbody.net/

                  private WebDriver initializeDriver() throws Exception {
                      // Start the server and get the selenium proxy object
                      ProxyServer server = new ProxyServer(proxy_port);  // package net.lightbody.bmp.proxy
                  
                      server.start();
                      server.setCaptureHeaders(true);
                      // Blacklist google analytics
                      server.blacklistRequests("https?://.*\.google-analytics\.com/.*", 410);
                      // Or whitelist what you need
                      server.whitelistRequests("https?://*.*.yoursite.com/.*. https://*.*.someOtherYourSite.*".split(","), 200);
                  
                      Proxy proxy = server.seleniumProxy(); // Proxy is package org.openqa.selenium.Proxy
                  
                      // configure it as a desired capability
                      DesiredCapabilities capabilities = new DesiredCapabilities();
                      capabilities.setCapability(CapabilityType.PROXY, proxy);
                  
                      // start the driver   ;
                      Webdriver driver = new FirefoxDriver(capabilities);
                  
                      return driver;
                  }
                  

                  人们经常询问 http 状态代码,您可以使用代理轻松检索它们.代码可以是这样的:

                  people are often asking for http status codes, you can easily retrive them using the proxy. Code can be something like this:

                  // create a new har with given label
                  public void setHar(String label) {
                      server.newHar(label);
                  }
                  
                  public void getHar() throws IOException {
                      // FIXME : What should be done with the this data?
                      Har har = server.getHar();
                      if (har == null) return;
                      File harFile = new File("C:\localdev\bla.har");
                      har.writeTo(harFile);
                      for (HarEntry entry : har.getLog().getEntries()) {
                          // Check for any 4XX and 5XX HTTP status codes
                          if ((String.valueOf(entry.getResponse().getStatus()).startsWith("4"))
                                  || (String.valueOf(entry.getResponse().getStatus()).startsWith("5"))) {
                              log.warn(String.format("%s %d %s", entry.getRequest().getUrl(), entry.getResponse().getStatus(),
                                      entry.getResponse().getStatusText()));
                              //throw new UnsupportedOperationException("Not implemented");
                          }
                      }
                  }
                  

                  这篇关于防止在 selenium webdriver 测试中加载外部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                  Java System Environment Variable(Java 系统环境变量)
                  Java -classpath option(Java -classpath 选项)
                  Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)

                    <tbody id='3iOVL'></tbody>

                  <legend id='3iOVL'><style id='3iOVL'><dir id='3iOVL'><q id='3iOVL'></q></dir></style></legend>
                    • <tfoot id='3iOVL'></tfoot>
                      <i id='3iOVL'><tr id='3iOVL'><dt id='3iOVL'><q id='3iOVL'><span id='3iOVL'><b id='3iOVL'><form id='3iOVL'><ins id='3iOVL'></ins><ul id='3iOVL'></ul><sub id='3iOVL'></sub></form><legend id='3iOVL'></legend><bdo id='3iOVL'><pre id='3iOVL'><center id='3iOVL'></center></pre></bdo></b><th id='3iOVL'></th></span></q></dt></tr></i><div id='3iOVL'><tfoot id='3iOVL'></tfoot><dl id='3iOVL'><fieldset id='3iOVL'></fieldset></dl></div>
                      • <small id='3iOVL'></small><noframes id='3iOVL'>

                            <bdo id='3iOVL'></bdo><ul id='3iOVL'></ul>