Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver tests with JavaScript disabled

One of our internal applications (written in angularjs) has a special error box appearing if javascript is disabled in the browser (using noscript), similar to the one on stackoverflow:

enter image description here 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.

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), 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, but not anymore.

  • 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.

like image 812
alecxe Avatar asked Nov 24 '14 06:11

alecxe


People also ask

Is JavaScript needed for automation testing?

JavaScript is one of the most widely used programming languages by developers, including test automation engineers.

Does Selenium WebDriver rely on JavaScript for automation?

Selenium WebDriver has made automation testing easier and more efficient than ever. By using JavaScript to create test scripts, it is easy to perform automated UI Testing for applications.

How does Selenium verify disabled elements?

How To Verify Element is Enabled or Disabled in Selenium Webdriver? To verify that the target element (Button, Check box, Dropdown, text box, Radio Button, Icons, etc ) are enabled or disabled use isEnabled() Method to check element is enabled or disabled.

What Cannot be tested using Selenium?

Selenium cannot be used to automate testing on Customised/Embedded Hardware.. Also, we always need to remember that if manual testing takes less time and effort, then automating those flows would be a bad idea, as automation test scenarios are usually considered for saving time, effort, and increasing reliability.


1 Answers

Here is what actually happened.

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

There is a setting called ignoreSynchronization which by default is 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 was not setting it to true which made protractor to try synchronizing with the page and execute client side scripts, which evaluate.js is responsible for.

The solution was so simple 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 would help somebody in the future.

like image 158
alecxe Avatar answered Nov 08 '22 12:11

alecxe