作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
根据苹果的documentation我可以使用 import 语句将一个 JS 文件导入另一个文件。是的,我能够使用 JS 函数并递归调用其他 JS 函数。
但是我可以将 Node 模块包含到我的自动化中吗? Node/npm模块似乎有很多工具可以让生活更轻松并避免代码重复。
实际上我能够使用一个名为 moment.js 的 Node 模块通过我的代码中的以下调用
#import "../node_modules/moment/moment.js"
但我没有像其他 npm 模块那样幸运。我试过一对Faker.js , Charlatan.js我在 Faker.js 中收到以下错误
Script threw an uncaught JavaScript error: Can't find variable: window on line 618 of Faker.js
查看 *.js 文件似乎与这些模块的打包方式有关。我的 JS 知识无济于事。
moment js文件最后几行
// CommonJS module is defined
if (hasModule) {
module.exports = moment;
}
/*global ender:false */
if (typeof ender === 'undefined') {
// here, `this` means `window` in the browser, or `global` on the server
// add `moment` as a global object via a string identifier,
// for Closure Compiler "advanced" mode
this['moment'] = moment;
}
/*global define:false */
if (typeof define === "function" && define.amd) {
define("moment", [], function () {
return moment;
});
}
Faker js文件的最后几行
if (typeof define == 'function'){
define(function(){
return Faker;
});
}
else if(typeof module !== 'undefined' && module.exports) {
module.exports = Faker;
}
else {
window.Faker = Faker;
}
我完全能够在 Node 控制台中使用这些模块,所以这些模块没有问题,只是如何在我的 JS 文件中包含/需要它们。
最佳答案
必须为 Faker 做两件事才能为我工作
添加这条语句
this['Faker'] = Faker;
关于javascript - 如何在 UIAutomation 中使用 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15100823/
我是一名优秀的程序员,十分优秀!