我在功能测试中使用任何 dojo 模块时遇到问题,我不断看到 window
未定义错误或 document
未定义。
我目前正在尝试像这样使用dijit/registry
(到目前为止仅导入其模块)..
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');
});
}
});
});
...并且从节点收到以下错误...
$ ./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)
我已阅读上一个问题 about using dojo/text!
in a similar way这似乎表明 Intern 的老家伙版本也许可以处理这个问题?
测试在没有注册表模块的情况下运行良好。
更新
好的,根据 C Snover 的响应,您不能在 webdriver execute()
方法之外利用诸如 dijit/registry
之类的内容,因为代码需要位于上下文中Web 浏览器的测试不是功能测试。
最佳答案
功能测试在 Node.js 中运行,而不是在浏览器环境中运行。如果您想访问正在测试的页面中加载的 dijit/registry
实例,则需要使用 execute
在远程环境中运行函数:
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);
});
您将无法从功能测试模块定义具有 DOM 要求的依赖项(例如 dijit/registry
)。只有基于浏览器的单元测试模块才能加载此类依赖项。
关于javascript - 如何在实习生功能测试中使用 dijit/registry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462251/