防止在selenium webdriver测试中加载外部内容 [英] prevent external content to be loaded in selenium webdriver test

查看:91
本文介绍了防止在selenium webdriver测试中加载外部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

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

Backround:

Backround:

我有一个网页反对我用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测试中加载外部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆