Tuesday Tip Day – Turn Off JavaScript

Sometimes when designing a website or web application, it’s important to try and plan for the different kind of visitors that will be visiting your site. One of the more troublesome visitors you might get is someone who has their JavaScript turned off within their browser, either due to their security software, or just being unaware that they might have accidentally switched it off.
Having JavaScript off can cause your website to behave very differently depending on how it was written, and will often lead people to hit broken pages or services not giving the correct results, if they are even working at all.
Because of this, it’s important that for this scenario, you have implemented some kind of fallout page for these users or way or pointing out that they have JavaScript turned off. Which means you’ll also need a test to make sure this behaviour is working.
The difficulty comes in making sure it’s turned off as part of an automated process rather than having to do it manually before and after every test run, which would obviously defeat the purpose of our automation test. Thankfully, turning JavaScript off as part of an automated test is as simple as setting a flag in the creation of our browser driver.
Let’s take a look at what that looks like in code for different browsers:
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference(“javascript.enabled”, false); driver = new FirefoxDriver(profile);
DesiredCapabilities capabilities = DesiredCapabilities.Chrome(); capabilities.SetCapability(“chrome.switches”, Arrays.AsList(“—disable-javascript”));
With the above code, your driver instance will have JavaScript turned off for the duration of the test. It’s important to remember this as any behaviour that requires it to be turned on will fail as long as the test is using that driver instance.