Categories
Programming

Speed up Selenium web-driver tests by disabling images and CSS

Using Selenium Webdriver, we can manipulate and use a web browser programmatically. This is widely used in website testing as once a programmed script is written to run on a website, we can sit back and relax while the script is testing the desired outcomes of the website. Also there are many other use cases.

Now the thing about the driver is that it uses a real web browser to run the scripts. So once a web page is accessed, the browser will perform its normal functionality loading everything in the web page. This can slow down the script. However for your use case you might not need to wait till images are rendered or CSS files being loaded etc.

So I wanted to disable the images, CSS for my use case when I’m running the script on Firefox. So if you are ok with not having load the images, CSS and disable other stuff, you can manipulate Firefox profiler in the test window to achieve that. I think other browser can do similar like this to archive the same thing.  The code segment below works for Firefox.

## Get profile class
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable JavaScript
firefoxProfile.set_preference('javascript.enabled', False)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
## Use the driver
browser = webdriver.Firefox(firefoxProfile)

That’s it. Now images, CSS and other stuff will not be loaded.

Conclusion

If disabling images, and other stuff break your website you will not get any value from this approach however for certain cases this might come handy. And it’s a great option to have if you want to speed up the execution of the scripts.

2.3 3 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sumit Jiwane
Sumit Jiwane
3 years ago

Can the same thing be done with chrome?

Sinaru Gunawardena
Admin
3 years ago
Reply to  Sumit Jiwane

I believe so. But the configuration would be different. 🙂