Search code examples
javascriptnightmare

Nightmarejs to click the window.confirm's button


I haven't found the related api.

I want to click the "确定"

enter image description here


Solution

  • I realize this is an old question, but for posterity and completeness' sake, being able to override default behavior for input, confirm, and alert were added in v2.1.0 by defining a custom preload script.

    I am assuming that is a confirm prompt, and the selection you want is the affirmative. By default, Nightmare will now use the default response specified by the confirm. If you want to override that behavior, you could do something to the effect of:

    window.__nightmare = {};
    __nightmare.ipc = require('ipc');
    window.confirm = function(message, defaultResponse){
      if(message == "sure to delete?")
           return true;
      return defaultResponse;
    }
    

    ... in a custom preload file.