Selenium WebDriver tests with JavaScript disabled

In one of our internal applications (written in angularjs), a special error window appears if javascript is disabled in the browser (using noscript ), similar to that in stackoverflow:

enter image description here I am trying to write an automatic test for it, but with difficulties.

We use a protractor , but I'm sure this is not about that. 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 :

 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 can see, we set the javascript.enabled preference for firefox to false , which has proven to work if you manually open about:config in firefox, change it to false - you will see the contents of noscript .

But when I run the tests, I get the following error:

An exception was thrown by org.openqa.selenium.WebDriverException: waiting for error load.js

Here is the full trace .

It uses FYI, selenium 2.44.0 and firefox 33.1.1 .

As far as I understand (with the help of a few points raised here ), disabling javascript kills javascript webdriver itself . It's true? If so, what are my options or workarounds?


Notes:

  • in the case of chrome, in the past you could disable javascript via the --disable-javascript command line argument , but no more .

  • this leads to bypass number 0 - switching to chrome to the old version that supports the command line flag - this will not be a proven plan B

  • setting javascript.enabled=false firefox preferences work with selenium python 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 am open to any suggestions and can provide you with any additional information.

+7
javascript firefox testing selenium-webdriver protractor
source share
4 answers

This is what really happened.

As it turns out, after studying the protractor and selenium js webdriver source code, the key problem is not js webdriver or protractor , it was the way my test was written.

There is a parameter called ignoreSynchronization , which defaults to false :

  /** * If true, Protractor will not attempt to synchronize with the page before * performing actions. This can be harmful because Protractor will not wait * until $timeouts and $http calls have been processed, which can cause * tests to become flaky. This should be used only when necessary, such as * when a page continuously polls an API using $timeout. * * @type {boolean} */ this.ignoreSynchronization = false; 

And I did not set it to true , which made protractor try to synchronize with the page and execute the client-side scripts that evaluate.js is responsible for.

The solution was so simple that I could not imagine - just setting ignoreSynchronization to true , solved the problem:

 'use strict'; require('jasmine-expect'); describe('Disabled Javascript', function () { beforeEach(function () { browser.ignoreSynchronization = true; browser.get('index.html'); }); it('should show disabled js', function () { var element = browser.findElement(by.tagName('noscript')); expect(element.getText()).toEqual('Please enable Javascript and try again.'); }); }); 

Hope this helps someone in the future.

+2
source share

As far as I understand (with a few points raised here) disabling javascript kills javascript webdriver itself. It's true?

Yes. Please note that WebDriver itself works as a Firefox extension, so its code does not depend on disabling JavaScript. However, the error message indicates that Selenium is trying to run some code in the context of the web page. To do this, he enters the <script> on the web page and puts the contents of evaluate.js into it. He then waits for this script to set a flag indicating that it is ready - something that never happens because the scripts are not working on this web page.

If so, what are my options or workarounds?

An important issue is what Selenium is trying to run there. Your trace simply indicates that some code is executeScript() or executeAsyncScript() . I could not find any implied calls for any method in the Selenium codebase (at least not in the files associated with the Firefox web driver). Given that Selenium is open source, you should be able to debug it and add a breakpoint to org.openqa.selenium.remote.RemoteWebDriver.executeScript() and org.openqa.selenium.remote.RemoteWebDriver.executeAsyncScript() - this way you will actually see the script running there. In addition, you can also recompile RemoteWebDriver.java and draw debug output.

Taking a look at how these methods are implemented, there may be another way: tell Selenium that JavaScript is disabled. This should work through features:

 'javascriptEnabled': false, 

According to the documentation, this feature only works on HTMLUnitDriver, but Selenium will always consider it and from the source code indicating it for Firefox, it seems possible. With this feature, any calls that execute scripts on web pages will not be executed earlier.

+1
source share

according to your glass, it seems you are using ff 33.1 with selenium 2.43.1, so it does not support. Upgrade your selenium version to 2.44 to enable tests on ff-33.

0
source share

An alternative solution / workaround was found - "NoScript Security Suite" firefox extension .

It disables javascript for each domain and, by default, it does this for all sites except those listed in the white list. This makes the webdriver work without problems, open a web page, then the extension disables javascript for that particular site, and I see the contents of the noscript tag.

Here's the protractor configurator:

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

where disabledJavascript.helper.js contains:

 'use strict'; var q = require('q'); var FirefoxProfile = require('firefox-profile'); exports.getFirefoxProfile = function() { var deferred = q.defer(); var firefoxProfile = new FirefoxProfile(); firefoxProfile.addExtension('./test/e2e/disabledJavascript/noscript.xpi', function () { firefoxProfile.encoded(function(encodedProfile) { var capabilities = { 'firefox_profile' : encodedProfile, 'browserName': 'firefox' }; deferred.resolve(capabilities); }); }); return deferred.promise; }; 

where noscript.xpi is the downloaded Noscript extension.

0
source share

All Articles