我通过 Capybara 使用 Selenium 来自动化使用 Cucumber 运行的测试。我正在加载一些引用 CDN 上内容的页面。我没有兴趣创建不必要的请求并无缘无故地访问 CDN。我想配置 Selenium 以某种方式忽略对该域的请求。
Celerity 有一个这样的方法:
Browser.ignore_pattern("regex pattern")
这将忽略任何创建的匹配请求。我想以某种方式复制此功能。有没有办法覆盖 DNS 以转到 0.0.0.0 或以其他方式配置内部 Selenium 代理?
最佳答案
您应该能够使用在 https://github.com/jarib/browsermob-proxy-rb 找到的 browsermob-proxy-rb gem将您的 CDN 列入黑名单。
以下内容主要是从 github 列表的自述文件中窃取的:
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"
关于ruby - 阻止或重定向请求到 Selenium 中的特定路径或域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247341/