【问题标题】:Block or redirect requests to specific path or domain in Selenium阻止或将请求重定向到 Selenium 中的特定路径或域
【发布时间】:2012-12-24 05:10:47
【问题描述】:

我通过 Capybara 使用 Selenium 来自动化使用 Cucumber 运行的测试。我正在加载一些引用 CDN 内容的页面。我对创建不必要的请求并无缘无故地访问 CDN 没有兴趣。我想将 Selenium 配置为以某种方式忽略对该域的请求。

Celerity 有这样的方法:

Browser.ignore_pattern("regex pattern")

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

【问题讨论】:

    标签: ruby testing selenium proxy capybara


    【解决方案1】:

    您应该能够使用位于 https://github.com/jarib/browsermob-proxy-rb 的 browsermob-proxy-rb gem 将您的 CDN 列入黑名单。

    以下内容大部分是从 github 列表的 README 中窃取的:

    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"
    

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 2014-09-06
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      相关资源
      最近更新 更多