Selenium WebDriver tests with JavaScript disabled
Question: One of our internal applications (written in _angularjs_) has a special error
box appearing if javascript is disabled in the browser (using
[`noscript`](https://developer.mozilla.org/en-
US/docs/Web/HTML/Element/noscript?redirectlocale=en-
US&redirectslug=HTML%2FElement%2Fnoscript)), similar to the one on
stackoverflow:

![enter image description here](http://i.stack.imgur.com/QZaVk.png) I'm trying
to write an automated test for it, but having difficulties.

We are using `protractor`, but I'm pretty sure this is not about it. Here is
the protractor configuration file:

    
    
    'use strict';
    
    var helper = require('./helper.js');
    
    exports.config = {
        seleniumAddress: 'http://localhost:4444/wd/hub',
        baseUrl: 'http://localhost:9001',
    
        capabilities: helper.getFirefoxProfile(),
    
        framework: 'jasmine',
        allScriptsTimeout: 20000,
    
        jasmineNodeOpts: {
            showColors: true,
            isVerbose: true,
            includeStackTrace: true
        }
    };
    

where `helper.js` is:

    
    
    var q = require('q');
    var FirefoxProfile = require('firefox-profile');
    
    exports.getFirefoxProfile = function() {
        var deferred = q.defer();
    
        var firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("javascript.enabled", false);
        firefoxProfile.encoded(function(encodedProfile) {
            var capabilities = {
                'browserName': 'firefox',
                'firefox_profile' : encodedProfile,
                'specs': [
                    '*.spec.js'
                ]
            };
            deferred.resolve(capabilities);
        });
    
        return deferred.promise;
    };
    

As you see, we are setting `javascript.enabled` firefox preference to `false`
which has been proven to work if you manually open up `about:config` in
firefox, change it to `false` \- you would see the contents of `noscript`
section.

But, when I run the tests, I am getting the following error:

> Exception thrown org.openqa.selenium.WebDriverException: waiting for
> evaluate.js load failed

Here is the [complete
traceback](https://gist.github.com/alecxe/8247dd77d55ea34c8916).

FYI, selenium `2.44.0` and firefox `33.1.1` are used.

As far as I understand (with the help of several points raised
[here](http://yizeng.me/2014/01/08/disable-javascript-using-selenium-
webdriver/)), **disabling javascript is killing the javascript webdriver
itself**. Is it true? If yes, what are my options or workarounds?

* * *

Notes:

  * in case of chrome, in the past it was possible to disable javascript via [`--disable-javascript` command-line argument](http://peter.sh/experiments/chromium-command-line-switches/#disable-javascript), but [not](https://code.google.com/p/selenium/issues/detail?id=3175) [anymore.](https://code.google.com/p/selenium/issues/detail?id=6672)

  * this leads to a workaround number 0 - downgrade chrome to an old version which supported the command-line flag - this would be a not-tested plan B

  * setting `javascript.enabled=false` firefox preference works with _python selenium bindings_ :
    
        from selenium import webdriver
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference('javascript.enabled', False)
    driver = webdriver.Firefox(firefox_profile=profile)
    
    driver.get('https://my_internal_url.com')
    # no errors and I can assert the error is present
    

I'm open to any suggestions and can provide you with any additional
information.


Answer: > As far as I understand (with the help of several points raised here),
> disabling javascript is killing the javascript webdriver itself. Is it true?

Yes. Note that WebDriver itself runs as a Firefox extension, so its code isn't
affected by you disabling JavaScript. However, the error message indicates
that Selenium is attempting to run some code in the context of the webpage.
For that it injects a `<script>` tag into the webpage and puts the contents of
`evaluate.js` into it. Then it waits for that script to set a flag indicating
that it is ready - something that never happens of course because scripts
aren't running in that webpage.

> If yes, what are my options or workarounds?

The important question is what Selenium is trying to run there. Your traceback
is merely indicating that some code is calling `executeScript()` or
`executeAsyncScript()`. I couldn't find any implied calls to either method in
the Selenium codebase (at least not in any files related to Firefox web
driver). Given that Selenium is open source, you should be able to debug it
and add a breakpoint on
`org.openqa.selenium.remote.RemoteWebDriver.executeScript()` and
`org.openqa.selenium.remote.RemoteWebDriver.executeAsyncScript()` \- that way
you will actually see the script being executed there. Alternatively, you can
also recompile `RemoteWebDriver.java` and make it produce debugging output.

Looking into how these methods are implemented, there might also be another
way: telling Selenium that JavaScript is disabled. This should work via
capabilities:

    
    
    'javascriptEnabled': false,
    

According to documentation this capability only works on HTMLUnitDriver, yet
Selenium will always consider it and from the source code specifying it for
Firefox seems possible. With this capability any calls executing scripts on
web pages will fail early.

