0

首先是伟大的测试工具替代品,因为我大量使用硒。这可能是基本的,但对 javascript 和 node.js 来说尤其如此。

我希望能够在我的脚本中使用全局变量,但在这样做时遇到了麻烦

例如,我有一个名为 variables 的文件与我的 test.js 脚本处于同一级别,在此示例中,我将 google 分配给变量 url,但是在运行测试时它无法加载。

require('variables.js');

module.exports = {
'Log into Traceiq': function (test) {
 test
 .open(url)
 .done();
}
};

然后我尝试了这个,测试开始运行,但在开始阶段挂起

require('./variables');

module.exports = {
'open Google': function (test) {
 test
  .open(url)
  .done();
}
}; 

这是控制台中的输出

 Running tests
 Running Browser: Google Chrome
 OS: Linux 3.5.0-45-generic x86_64
 Browser Version: 31.0.1650.63
 >> WARNING: done not called!

 RUNNING TEST - "Open Google"
✔ 0 Assertions run
✔ TEST - "Open Google" SUCCEEDED

我在这里做错了什么?

任何帮助表示赞赏

谢谢

4

2 回答 2

0

Node.js 处理“导入”文件的要求与我们在浏览器领域所使用的不同。这篇 SO 帖子很好地概述了“如何”和“为什么”:

Node.js module.exports 的用途是什么,你如何使用它?

如果您正在寻找有关如何保持测试干燥和模块化的 DalekJS 特定资源,请查看我为研讨会制作的这个存储库https://github.com/asciidisco/jsdays-workshop/tree/8-dry 。更具体地说,这些文件尤其是:

https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/form2.js https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek /functions/configuration.js https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/functions/formHelper.js https://github.com/asciidisco/jsdays-workshop/blob /8-dry/test/dalek/functions/selectors.js

希望有帮助。

于 2014-03-30T21:58:13.410 回答
0

好吧,看起来这是我的部分用户错误,坚果会发布答案,以防它帮助其他人。在 Node.js 中设置变量与典型的客户端 Javascript 不同。

所以我在哪里设置

var url = "www.google.co.uk"

我需要设置

global.url = "www.google.co.uk"
于 2014-03-24T12:51:17.780 回答