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

标签 node.js nightmare browser-testing

我正在编写 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/

相关文章:

node.js - 检查语法正确但语义不正确的 JSON Schema

javascript - 从 Node 模块在 VueJS 中提供 firebaseui.css 的问题

performance - 如何在 Functional Automation Suite 运行时测量浏览器上的应用程序性能?

laravel - 使用 Laravel 5.4 模拟邮件(黄昏)

node.js - 如何自定义管道目标来解析读取流中的数据?

javascript - React JSX 中的动态标签名称

javascript - 使用 NightmareJS 模拟内置对象

node.js - 我可以指示nightmare.js转到本地主机上的快速路由吗?

javascript - 使用带有 NodeJS 的 NightmareJS 进行抓取

javascript - 如何通过 selenium-webdriver javascript API 设置 "debuggerAddress"chromeOption?