问题是:
可以告诉由selenium webdriver控制的浏览器不要从外部源加载任何内容,或者不从给定的域列表()加载资源。
回溯:
我有一个网页,我用selenium编写了一个基于java的测试脚本--我不能更改页面,我只需要编写测试。站点从另一个域加载的某些外部内容存在问题。外部内容是一些javascript代码,实际上我的测试并不需要这些代码,但是有问题的页面包括。现在问题来了。有时外部源是超慢的,阻止webdriver在给定的页面加载超时(20秒)内加载页面。我的测试实际上运行良好,因为页面实际上是加载的-所有html都在那里,所有内部脚本都加载并工作。
对此的随机思考:
对于不同的浏览器,有一些扩展可以满足我的要求,但是我需要使用几种浏览器来运行我的测试,比如chrome、firefox和幻影to。而且也没有像幻影一样的延伸。如果可能的话,我需要一个纯粹基于webdriver技术的解决方案。不过,我愿意为每个浏览器编写一个单独的解决方案。
我很感激任何关于如何解决这个问题的想法。
发布于 2013-09-16 11:49:02
解决方案是使用代理。Webdriver与浏览器服务器代理( 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状态代码,您可以很容易地检索他们使用代理。代码可以是这样的:
// 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");
}
}
}发布于 2014-12-12 22:00:10
您可以将代理链接起来,这样做的文档不多:
http://www.nerdnuts.com/2014/10/browsermob-behind-a-corporate-proxy/
我们能够使用以下代码在企业代理后面使用浏览器:
// start the proxy
server = new ProxyServer(9090);
server.start();
server.setCaptureContent(true);
server.setCaptureHeaders(true);
server.addHeader(“accept-encoding”, “”);//turn off gzip
// Configure proxy server to use our network proxy
server.setLocalHost(InetAddress.getByName(“127.0.0.1″));
/**
* THIS IS THE MAJICK!
**/
HashMap<String, String> options = new HashMap<String, String>();
options.put(“httpProxy”, “172.20.4.115:8080″);
server.setOptions(options);
server.autoBasicAuthorization(“172.20.4.115″, “username”, “password”);
// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(CapabilityType.PROXY, proxy);https://stackoverflow.com/questions/18815542
复制相似问题