Search code examples
javascriptdojofunctional-testingintern

How can I use dijit/regisrty in an Intern functional test


I am having problems using any dojo modules with my functional test, I keep seeing window not defined errors or document not defined.

I am currently trying to use the dijit/registry like so (only importing its module so far)..

define([
    'intern!object',
    'intern/chai!assert',
    'require',
    'dijit/registry'
], function (registerSuite, assert, require, registry) {
    registerSuite({

        name: 'login',

        'load and login': function () {
            return this.remote.get(require.toUrl('http://application.co.uk/'))
                .elementById('input1')
                    .click()
                    .type("username")
                    .end()
                .elementById('input2')
                    .click()
                    .type("password")
                    .end()
                .elementById('rememberMe')
                    .click()
                    .end()
                .elementByName('Login.Submit')
                    .click()
                    .end()
                .wait(5000)
                .title()
                .then(function (title) {
                    assert.strictEqual(title, 'Application title');
                });
        }
    });
});

...and am getting the following error from node...

$ ./libs/intern/bin/intern-runner.js config=test/intern
Defaulting to "runner" reporter
Listening on 0.0.0.0:9000

c:/.../libs/dojo/_base/unload.js:6
var win = window;
          ^
ReferenceError: window is not defined
    at c:/.../libs/dojo/_base/unload.js:6:11
    at execModule (c:\...\libs\intern\node_modules\dojo\dojo.js:512:54)
    at c:\...\libs\intern\node_modules\dojo\dojo.js:501:12
    at Array.map (native)
    at execModule (c:\...\libs\intern\node_modules\dojo\dojo.js:496:17)
    at c:\...\libs\intern\node_modules\dojo\dojo.js:501:12
    at Array.map (native)
    at execModule (c:\...\libs\intern\node_modules\dojo\dojo.js:496:17)
    at c:\...\libs\intern\node_modules\dojo\dojo.js:501:12
    at Array.map (native)

I have read a previous question about using dojo/text! in a similar way which seemed to indicate the geezer version of Intern could handle this maybe?

The test runs fine without the registry module.

UPDATE

Ok so based on C Snover's response, you cant leverage anything like dijit/registry outside of a webdriver execute() method as the code needs to be within the context of the web browser not the functional test.


Solution

  • Functional tests run from within Node.js, not the browser environment. If you want to access the dijit/registry instance that was loaded in the page you are testing, you need to use execute to run a function within the remote environment:

    return this.remote
      .get('http://application.co.uk')
      .execute(function () {
        // this function runs in the browser!
    
        var registry = require('dijit/registry');
        // ... do things with registry
    
        return something;
      })
      .then(function (something) {
        // this function runs inside the test runner!
    
        assert.isTrue(something);
      });
    

    You won’t be able to define dependencies that have DOM requirements (like dijit/registry) from functional test modules. Only browser-based unit test modules will be able to load such dependencies.