2013-01-09 65 views
4

我通过Capybara使用Selenium来自动执行使用Cucumber的测试。我正在加载一些在CDN上引用内容的页面。我没有兴趣创建超过必要的更多请求,并且无故击中CDN。我想配置Selenium以某种方式忽略对该域的请求。在Selenium中阻止或重定向请求到特定路径或域

迅捷具有这样的方法:

Browser.ignore_pattern("regex pattern") 

这将忽略创建匹配的任何请求。我想以某种方式复制此功能。有没有办法覆盖DNS转到0.0.0.0或其他方式来配置内部Selenium代理?

回答

2

您应该可以使用在https://github.com/jarib/browsermob-proxy-rb上找到的browsermob-proxy-rb gem将您的CDN列入黑名单。

require 'selenium/webdriver' 
require 'browsermob/proxy' 

server = BrowserMob::Proxy::Server.new("/path/to/download/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...> 
server.start 

proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...> 

profile = Selenium::WebDriver::Firefox::Profile.new #=> #<Selenium::WebDriver::Firefox::Profile:0x000001022bf748 ...> 

# This is the line I added 
proxy.blacklist(/path.to.CDN.com/) 

profile.proxy = proxy.selenium_proxy 

driver = Selenium::WebDriver.for :firefox, :profile => profile 

driver.get "http://www.yoursite.com" 

下面主要是从github上上市的README被盗

相关问题