Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable loading external urls on seleniumlibrary/robotframework

I started playing with Seleniumlibrary tests (run with robotframework) and as our websites have ads and metrics and so on, every time I run a test those URLs get loaded.

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

like image 784
gforcada Avatar asked Mar 08 '13 13:03

gforcada


People also ask

Is Robot framework better than Selenium?

There is a huge difference between both of them – Robot is a test framework that makes use of test libraries (standard & external) to execute tests, whereas Selenium is only a WebDriver/library that requires the support of test automation runners in order to perform test execution.

How do I know if a robot framework element is clickable?

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met. To verify, if the element can be clicked, we shall use the elementToBeClickable condition.

How do I launch a browser in Robot Framework?

The first keyword is called “open the browser”. It is configured to open a new browser window defined by the “BROWSER” variable and load the URL initialized in the “HOMEPAGE” variable. Here, “Open Browser” is an in-built keyword of Selenium2Library used to open a browser instance using Selenium Webdriver.


1 Answers

You can do that with browsermob-proxy. Once you have that installed, you simply setup the proxy and set 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);

I believe the following will work with this python wrapper (the regex might be slightly different):

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()
like image 154
Scott Avatar answered Dec 13 '22 02:12

Scott