How to disable loading external urls on seleniumlibrary / robotframework

I started playing with the Seleniumlibrary tests (launched using robotframework), and since our sites have ads and indicators, etc., every time I run the test, these URLs load.

Is there a way to tell selenium / robotframework not to load certain types of URLs or to load external resources (i.e. everything that is not related to localhost).

+5
source share
1 answer

You can do this with browsermob-proxy . Once you have installed this, you simply install the proxy server and set up the blacklist.

ProxyServer server = new ProxyServer(9000)
server.start();
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
//Send a 200 for all requests to the facebook cdn
server.blacklistRequests("http://.*\\.fbcdn.net/.*", 200); 
//Finish setting up your driver
WebDriver driver = new SomeDriverImpl(capabilities);

, python ( ):

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
proxy.blacklist('http://.*\\.fbcdn.net/.*', 200)
from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
...
proxy.stop()
driver.quit()
+8

All Articles