2

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.

Community
  • 1
  • 1
Jeremy
  • 3,418
  • 2
  • 32
  • 42

2 Answers2

2

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.

C Snover
  • 17,908
  • 5
  • 29
  • 39
  • I made that change, but I'm not getting in the registry what I would expect, ie. access to the widgets. If I loop though all widgets in `regisrty.toArray()` then I just get a lot of undefined items instead of the expected widgets. Should I expect to see all widgets that have been instantiated in the browser? or can I not use the registry in functional tests like that? – Jeremy Mar 17 '14 at 19:15
  • You can execute whatever code you want from an `execute` function and it will run in the browser. However, the only objects that you can pass back to the test runner (by returning them) are values that can be serialized to JSON, plus DOM element references which are a special case in the WebDriver API. – C Snover Mar 17 '14 at 19:24
  • Fair enough, so I cant really use dojo how I had hoped with functional tests, never mind. How can I get better feedback for the executed scripts then, any ideas, if I try to use the console of the web browser the test falls over? – Jeremy Mar 17 '14 at 19:39
0

There's also the dijit-intern-helper -> https://github.com/SitePen/dijit-intern-helper

so this

executeAsync(function (done) {
    require(['dijit/registry'], function (registry) {
        done(registry.byId('titlePane').titleBarNode);
    });
})
.then(function (node, setContext) {
    setContext(node);
})
.click()

becomes

.then(dijit.nodeById('titlePane', 'titleBarNode'))
.click()

A blog post about it -> https://www.sitepen.com/blog/simplified-dijit-functional-testing/

denov
  • 11,180
  • 2
  • 27
  • 43