我正在尝试根据通过启动命令传递的自定义参数加载测试脚本以启动实习生测试。
为此,我尝试在测试中要求特定的测试脚本,但出现 Attempt to require unloaded module
错误。
这是我设置的代码。有人可以帮忙解决这个问题,或者建议一些替代方法来完成这项工作。
define(function (require) {
var intern = require('intern');
var AdvalentAutomationTestSuite = require('intern!object');
AdvalentAutomationTestSuite({
name: 'Advalent Automation Test',
'AdvalentTestSets': function () {
return this.remote
.then(function () {
var product = intern.args.product;
var script = 'Automation/TestScripts/FRG/' + product + '-Config';
require(script)
})
},
});
});
更新:
包括intern.js
文件:
define(function (require) {
var intern = require('intern');
console.log(intern)
return {
proxyPort: 9000,
proxyUrl: 'http://localhost:9000/',
defaultTimeout: 120000,
capabilities: {
'selenium_version': '2.48.2',
},
environments: [
{browserName: 'chrome', version: '48', platform: ['WINDOWS'], chromeOptions: {args: ['start-maximized']}},
],
maxConcurrency: 3,
tunnel: 'NullTunnel',
reporters: [
{id: 'JUnit', filename: 'test-reports/report.xml'},
{id: 'Runner'},
],
Loaders: {
'host-node': 'dojo/dojo',
'host-browser': 'node_modules/dojo/dojo.js'
},
loaderOptions: {
packages: [{name: 'intern-tutorial', location: '.'}]
},
functionalSuites: [
'Automation/TestScripts/FRG/FRG-Config',
],
defaultTimeout: 70000,
excludeInstrumentation: /^(?:tests|node_modules)\//
}
});
最佳答案
您应该可以使用默认加载器,尽管正如 @Troopers 指出的那样,它是 loaders
,而不是 Loaders
。问题是您正在使用计算名称执行动态要求:
var script = 'Automation/TestScripts/FRG/' + product + '-Config';
require(script)
AMD 加载器不完全支持 require(script)
语法,因为它们不同步加载模块。当模块以 CJS 兼容模式编写时,加载器会通过扫描模块代码中的 require
调用来伪造它,然后在执行模块代码之前预加载和缓存模块。当最终执行 require(script)
调用时,将返回预加载的模块。
当您使用计算出的模块名称时,加载器无法预加载所需的模块,因此同步 require
调用将失败。要加载具有计算名称的模块,您需要使用 require([ dependency ])
语法,例如:
var script = 'Automation/TestScripts/FRG/' + product + '-Config';
return new Promise(function (resolve) {
require([ script ], resolve);
});
不过,在更高的层面上,一开始在测试中这样做似乎很奇怪。这似乎应该在模块或配置级别处理。例如,假设 'Automation/TestScripts/FRG/' + Product + '-Config'
是一个功能测试套件,配置可以简单地将该套件添加到 functionSuites
列表中如果提供了所需的命令行参数。
关于javascript - 在 intern.js 中的测试中加载测试脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44565391/