Chet
Chet

Reputation: 22105

Block or redirect requests to specific path or domain in Selenium

I'm using Selenium through Capybara to automate tests run with Cucumber. I'm loading some pages that reference content on a CDN. I have no interest in creating more requests than necessary and hitting the CDN without reason. I'd like to configure Selenium to ignore requests to that domain somehow.

Celerity has a method like this:

Browser.ignore_pattern("regex pattern")

That will ignore any requests created that match. I'd like to replicate this feature in some manner. Is there a way to override DNS to go to 0.0.0.0 or some other way to configure the internal Selenium proxy?

Upvotes: 4

Views: 2190

Answers (1)

bbbco
bbbco

Reputation: 1540

You should be able to use the browsermob-proxy-rb gem found at https://github.com/jarib/browsermob-proxy-rb to blacklist your CDN.

The following was largely stolen from the github listing's 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"

Upvotes: 2

Related Questions