Horseman.js -> 处理 window.confirm

Horseman.js -> handling window.confirm

我需要帮助解决我面临的问题,我确信 node-horseman 可以处理它。

我正在尝试接受 "window.confirm" 警报,但尝试了很多都没有成功。基本上,单击按钮后,我希望能够 "accept" 使用 horseman 确认消息。

通过文档,我发现了这个:

.at(event, callback) Respond to page events with the callback.

.at('confirm', function(msg) {
    return msg === 'Like this?' ? true : false;
})

然而,当我像这样放置 .at 时,在任何 .open

之前
horseman
    .at('confirm', function(msg) {
        return msg === 'Do you accept?' ? true : false;
    })

我收到一条错误消息:

TypeError: horseman.at is not a function

我在寻找什么:一种使用 node-horseman 接受 window.confirm 的方法。

任何帮助将不胜感激!

阅读 this answer 帮助我解决了问题。

我没有使用 .at,而是执行了以下操作:

horseman
    .open(myUrl)
    .evaluate(function(){
        var realConfirm=window.confirm;  
        // override the window.confirm function just once
        window.confirm=function(msg){
            window.confirm = realConfirm;
            if (msg=="message I am Expecting"){  
                return true;
            }else{
                return false;
            }
        };
    })
    .click('#mybutton')

这成功了!

基本上我是覆盖了window.confirm函数,下次调用的时候,在执行的时候恢复到之前的值。

我希望这对其他人也有帮助。