How to disable Javascript when using Selenium?
I am wondering how do I disable javascript when using selenium so I can test server side validation.
I found this article but I don't know what to really do. Like I make this javascript file then what?
http://thom.org.uk/2006/03/12/disabling-javascript-from-selenium/
This is a way to do it if you use WebDriver with FireFox:
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
This is the simple answer, for python at least.
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("javascript.enabled", False);
driver = webdriver.Firefox(profile)
Edit
In the meantime better alternatives did arise, please see the other answers e.g. https://stackoverflow.com/a/7492504/47573 .
Original answer
Other possibilities would be:
- Write your application to support disabling JavaScript (yes, the web application).
Sounds crazy? Isn't. In our development process we're doing exactly this, implementing features without JS until all are there, then spice up with JS. We usually provide a hook within all templates which can control from a single point to basically which JS off/on from the web application itself. And, yes, the application is hardly recognizable without JS enabled, but it's the best way to ensure things work properly. We even write Selenium tests for it, for both versions; NOJS and JS. The NOJS are so quickly implemented that they don't matter compared to what it takes to write sophisticated JS tests ... - Modify the appropriate browser profile to have JS disabled. I.e. for FF you can tell Selenium which profile to use; you can load this profile normally, disable JS in about:config and feed this profile as default profile to Selenium RC.