ruby - 阻止或重定向请求到 Selenium 中的特定路径或域

标签 ruby testing selenium proxy capybara

我通过 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/

相关文章:

ruby - 是否可以有多个 STDIN 流?

ruby-on-rails - 尝试加载 gem 'rmagick' 时出错。 ( bundler ::GemRequireError)

ScalatestRouteTest 测试一起运行时超时但单独运行时正常

django - 你如何模拟 Django 中的 RelatedManager 方法?

Firefox 3.6 更新破坏了 selenium

java - 让驱动程序等待条件为真并在条件为假时继续的最佳方法是什么?

Python Selenium - 获取 href 值

ruby - 从文件中的数据构建 SQLite 数据库的最有效方法是什么?

ruby - method_missing 在 Ruby 中的可见性

unit-testing - 如何使用 JUnit 测试我的 servlet