1

I am writing nightmare script to test a website. The problem is for page events. There is a script

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

and the nightmare script is

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);
  }

When run like node test.js

It opens the browser window and click the button. But don't know how to press 'OK' button in the confirm pop up so that I can go to next test. The response not required from the 'OK' button just need to click the 'OK' button in the confirm window.

any help is very much appreciated.

Thanks

Know Nothing
  • 1,121
  • 2
  • 10
  • 21

1 Answers1

0

The default preload script overrides window.prompt, window.alert, and window.confirm. You're overriding the default script with a custom one. If your custom preload script doesn't reproduce the behavior of the default script, what you have will not work.

For completeness, this is a snip from the default preload script that shows the window method overrides as well as the IPC messages to wire the events up:

  // 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);
  }
Ross
  • 2,448
  • 1
  • 21
  • 24