-1

I need help with a problem I'm facing, and am sure node-horseman can handle it.

I'm trying to accept a "window.confirm" alert and tried many things without success. Basically, after clicking on a button, I want to be able to "accept" the confirm message using horseman.

Going through the docs, I found this:

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

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

Nevertheless, when I put the .at like this, before any .open

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

I get an error saying:

TypeError: horseman.at is not a function

What I'm looking for: a way to accept a window.confirm using node-horseman.

Any help will be really appreciated!

learn2day
  • 1,566
  • 1
  • 14
  • 17

1 Answers1

0

Reading this answer helped me solve the issue.

Instead of using .at I did the following:

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')

And this worked!

Basically, I override the window.confirm function for the next time it is called, and restore it to the previous value during the execution.

I hope this will help others as well.

Community
  • 1
  • 1
learn2day
  • 1,566
  • 1
  • 14
  • 17