gpt4 book ai didi

node.js - 如何使用nightmare.js页面事件

转载 作者:行者123 更新时间:2023-12-03 13:23:38 25 4
gpt4 key购买 nike

我正在编写 Nightmare 脚本来测试网站。问题出在页面事件上。有一个剧本

    <button id="btn" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
confirm("Press a button!");
}
</script>


Nightmare 剧本是

var Nightmare = require('nightmare');


const path = require('path');

var nightmare = Nightmare({
show: true,
webPreferences: {
preload: path.resolve("pre.js")
//alternative: preload: "absolute/path/to/custom-script.js"
}
})

var confirm = 'confirm';
nightmare.on('page', function(confirm, message, response){
return true;
});

nightmare
.goto('http://localhost:3000/index.html')
.click('#btn')
.wait(1000)
.evaluate(function () {
return "just value";//document.querySelector('#main .searchCenterMiddle li a').href
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});

function testing(arg){
console.log(arg);
}


当像 node test.js一样运行时

它打开浏览器窗口,然后单击按钮。但是不知道如何在确认弹出窗口中按“确定”按钮,这样我才能进行下一个测试。单击“确定”按钮不需要响应,只需在确认窗口中单击“确定”按钮即可。

很感谢任何形式的帮助。

谢谢

最佳答案

default preload script覆盖window.promptwindow.alertwindow.confirm。您正在用自定义脚本覆盖默认脚本。如果您的自定义预加载脚本无法重现默认脚本的行为,那么您所拥有的将无法正常工作。

为了完整起见,这是默认预加载脚本的片段,其中显示了window方法替代以及将事件关联起来的IPC消息:

  // overwrite the default alert
window.alert = function(message){
__nightmare.ipc.send('page', 'alert', message);
};

// overwrite the default prompt
window.prompt = function(message, defaultResponse){
__nightmare.ipc.send('page', 'prompt', message, defaultResponse);
}

// overwrite the default confirm
window.confirm = function(message, defaultResponse){
__nightmare.ipc.send('page', 'confirm', message, defaultResponse);
}

关于node.js - 如何使用nightmare.js页面事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com