0
我被给了一个javascript函数,我需要注入一个页面以获取稍后将使用的值列表。我可以使用Chrome控制台直接在网页上调用此功能,但我想在当前加载的网页上的nightmareJS中复制我在Chrome控制台中执行的操作。在梦魇中注入javascript函数js
这是函数:
function getList() {
require(['Service/List'],
function (Service)
{
Service.getList
({
onComplete: function (listOfServices)
{
console.log('List:success:' + JSON.stringify(listOfServices));
},
onFailure: function (error)
{
console.log('List:error:' + error);
}
});
});
}
getList();
我试过注射文件,但我没有成功,我也尝试添加额外的代码到函数的输出写入到一个文件,但我根本不认为它被称为。
这里是nightmareJS
describe('Open login page',() => {
it('Login', done => {
nightmare
.goto('http://loginURL.com')
.wait('input[type="text"]')
.wait('input[type="password"]')
.insert('input[type="text"]', 'username')
.insert('input[type="password"]', 'password')
.click('input[type="submit"]')
.evaluate(function() {
nightmare.inject('js', 'getList.js')
})
//.inject('js', 'getList.js')
.then(function() {
console.log('Done');
})
})
}) })
这是JavaScript文件注入到页面后的样品输出:
List:success:"Test":"https://someURL.com/resource/test","Design":"https://someURL.com/resource/Design"},"NewSpace":"https://someURL.com/resource/NewSpace","Generator":"https://Generator.someURL.com/resource/test","SomethingElse":"https://someURL.com/SomethingElse/test","Connection":"https://someURL.com/Connection/test","WorldWide":"https://someURL.com/resource/WorldWide","Vibes":"https://Vibes.someURL.com/resource/test","GoogleSearch":"https://someURL.com/resource/GoogleSearch",
我希望能够通过调用页面上的JavaScript文件获取该输出并将其保存到文件中,以便稍后可以使用它来调用该列表中的其他服务。
我想注入具有比单独的范围脚本在网页上。作为一种解决方法,您可能需要手动注入脚本(到
元素中)。如果此方法适用于您,请使用示例代码 – devilpreet我认为这可能会起作用,但我愿意尝试此时尝试任何内容如果我能看到一些示例代码会很棒! – patrickisallen