Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I automate Chrome request blocking using Selenium-webdriver for Ruby?

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.

like image 220
centuryfall Avatar asked Oct 23 '17 14:10

centuryfall


People also ask

What is Webdriver in selenium?

Where possible, WebDriver drives the browser using the browser’s built-in support for automation. Since all the driver implementations except for Internet Explorer are provided by the browser vendors themselves, they are not included in the standard Selenium distribution.

Can I use selenium in Python instead of Ruby?

NOTE: Be sure to check out Running Selenium with Headless Chrome if you’re interested in using Selenium in Python instead of Ruby. Since Google added support to run Chrome and Chromium in headless mode as of version 59, it has become a popular choice for both testing and web scraping.

How do I pass options from selenium to Chrome?

But you can also pass options directly to Chrome by using Chrome options. This gives you more possibilities to control the browser and in the case of maximizing the window, you remove the small delay that Selenium causes by resizing the window, because you can tell Chrome to start maximized.

Is selenium the best automation solution for Chrome?

There are a few Chrome-specific automation solutions out there, such as Puppeteer and Chrome Remote Interface, but Selenium remains a popular choice due to it’s uniform API across web browsers and it’s support for multiple programming languages.


2 Answers

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")
like image 171
Florent B. Avatar answered Oct 22 '22 04:10

Florent B.


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
like image 10
David Lumpkin Avatar answered Oct 22 '22 06:10

David Lumpkin