10

I am a QA automation analyst responsible for testing a multi-platform online banking application. For our automation testing we use RubyMine suite with Gherkin/Cucumber, Ruby-scripted steps and Selenium-webdriver, Watir and page-object gems/libraries.

I have a number of scripts that I cannot automate completely without manual intervention, and these include blocking certain network calls through Telerik Fiddler to produce warning pages or error messages, etc. The future of our automation would be to do this through RubyMine instead of Fiddler for network blocking. I know there is a way to do this in Chrome using Inspect Element and the Network menu using enable request blocking. However, I cannot figure out a way to force Chrome through Ruby/Selenium to block a given request. The only way is do manually do it myself, and therefore I can't actually automate these as wanted.

So, my question -- is this a possibility to automate request-blocking with Selenium-webdriver? And, if so, where should I begin to look for help with this?

Thanks.

centuryfall
  • 400
  • 2
  • 5
  • 13
  • 1
    To block the URI, either launch the browser on a customized proxy or call the DevTool API through Selenium or use a web extension. – Florent B. Oct 23 '17 at 14:20
  • https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setBlockedURLs – Florent B. Oct 23 '17 at 14:41
  • Thanks! I will look at this now! – centuryfall Oct 23 '17 at 15:10
  • So looking into this, the DevTools API is the most likely solution we can use. However,I am not seeing examples of how this is being used for what we want to do. I am thinking of calling RequestPattern and using YAML variables as substitutes for the requests I wish to block, but I do not see a call to fail it or ability to return a 404 error, for example. Would you happen to know which calls from this API could accomplish this? – centuryfall Oct 23 '17 at 16:50
  • it doesn't seem possible to alter the response/status with Selenium/DevTool since it requires to directly listen to the socket to get the events. It looks rather simple with [puppeter](https://github.com/GoogleChrome/puppeteer/blob/master/test/test.js#L1182). – Florent B. Oct 23 '17 at 18:54

4 Answers4

13

It's not very well documented, but you can also implement request blocking by passing the host-resolver-rules option to chrome and mapping the domain to localhost or an invalid IP. Something like this should work for you:

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--host-resolver-rules=MAP www.google-analytics.com 127.0.0.1')
driver = Selenium::WebDriver.for :chrome, options: options
David Lumpkin
  • 174
  • 1
  • 6
  • I used host-rules but the concept was the same and can be done in protractor as well. Lifesaver since protractor onPageLoad was not firing before angular loaded. – William Neely Jan 28 '21 at 19:53
  • I'll add code for Java: `options.addArguments("--host-rules=MAP *.google.com 127.0.0.1, MAP *.amazon.com 127.0.0.1"); ` – liaombro May 19 '23 at 12:33
10

To block URLs from loading with Selenium with the DevTool API:

def send_cmd(driver, cmd, params={})
  bridge = driver.send(:bridge)
  resource = "session/#{bridge.session_id}/chromium/send_command_and_get_result"
  response = bridge.http.call(:post, resource, {'cmd':cmd, 'params': params})
  raise response[:value] if response[:status]
  return response[:value]
end

send_cmd(driver, "Network.setBlockedURLs", {'urls': ["*"]})
send_cmd(driver, "Network.enable")
Florent B.
  • 41,537
  • 7
  • 86
  • 101
7

For those wanting to know, python version is just:

driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["www.baidu.com"]})
driver.execute_cdp_cmd('Network.enable', {})
Retiarius
  • 334
  • 2
  • 12
  • This doesn't work for `remote` webdriver. To solve this I had to use [this workaround](https://github.com/SeleniumHQ/selenium/issues/8672#issuecomment-699676869) – Marco Antônio Dec 07 '21 at 17:12
1

Try https://github.com/lightbody/browsermob-proxy

I dont know if it can satisfy your requirement as I am no way a network expert, I do use browsermob-proxy extensively to capture network request along with selenium and there is a method to blacklist certain request

https://browsermob-proxy-py.readthedocs.io/en/stable/client.html#browsermobproxy.Client.blacklist

How to disable loading external urls on seleniumlibrary/robotframework

Satish
  • 1,976
  • 1
  • 15
  • 19