所以,我有一个可以工作的nightmare.js应用程序,它可以100%地工作。我现在处于重构阶段,希望将我创建的自定义函数(使用nightmare.js函数)放到另一个文件中,然后将它们导出/导入到我的主文件中。
函数会被调用,但噩梦函数并不实际执行或抛出错误。
为什么噩梦函数在导入时不起作用?
我的主要应用:
const Nightmare = require('nightmare')
const nightmare = Nightmare({
show: true,
typeInterval: 1000,
waitTimeout: 60 * 1000
})
const bot = require('./utils')
nightmare
.goto(url)
.then(_ => bot.selectByVal('#myDiv', 'myVal'))
.then( 'yada yada yada ...')...
module.exports = nightmare;实用工具:
const Nightmare = require('nightmare');
const nightmare = Nightmare();
module.exports = {
selectByVal: function(el, val) {
console.log('select' + el + val)
try {
return nightmare.select(el, val)
} catch (e) {
return e
}
}
}我相信这与我的噩梦实例没有被导出/导入有关,但不确定如何做到这一点。
发布于 2019-04-05 18:30:02
bot或utils无权访问在主应用程序上创建的nightmare。您需要传递引用。
返回一个函数,该函数返回一个对象。
module.exports = function(nightmare) { // <-- now the same nightmare is in both file
return {
selectByVal: function(el, val) {
console.log('select' + el + val)
try {
return nightmare.select(el, val)
} catch (e) {
return e
}
}
}
}然后在你的主应用上,
const bot = require('./utils')(nightmare) // <-- pass the reference
nightmare
.goto(url)
.then(_ => bot.selectByVal('#myDiv', 'myVal'))https://stackoverflow.com/questions/55301874
复制相似问题