如何禁用在SeleniumLibrary/robotframework上加载外部URL [英] How to disable loading external urls on seleniumlibrary/robotframework

查看:178
本文介绍了如何禁用在SeleniumLibrary/robotframework上加载外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩Seleniumlibrary测试(通过robotframework运行),并且由于我们的网站上有广告和指标等,因此每次运行测试时,这些URL都会被加载.

I started playing with Seleniumlibrary tests (run with robotframework) and as our websites have ads and metrics and so on, every time I run a test those URLs get loaded.

有没有一种方法可以告诉selenium/robotframework不要加载某些类型的URL或阻止它加载外部资源(即不是来自本地主机的所有资源).

Is there a way to tell selenium/robotframework to not load certain types of URLs or prevent it to load external resources (i.e. everything that is not from localhost).

推荐答案

您可以使用 browsermob-代理.安装完成后,您只需设置代理并设置黑名单即可.

You can do that with browsermob-proxy. Once you have that installed, you simply setup the proxy and set the blacklist.

ProxyServer server = new ProxyServer(9000)
server.start();
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
//Send a 200 for all requests to the facebook cdn
server.blacklistRequests("http://.*\\.fbcdn.net/.*", 200); 
//Finish setting up your driver
WebDriver driver = new SomeDriverImpl(capabilities);

我相信以下内容可用于此python包装器(正则表达式可能会略有不同):

I believe the following will work with this python wrapper (the regex might be slightly different):

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
proxy.blacklist('http://.*\\.fbcdn.net/.*', 200)
from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
...
proxy.stop()
driver.quit()

这篇关于如何禁用在SeleniumLibrary/robotframework上加载外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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