在警报上单击“确定"或通过 jquery/javascript 确认对话框?

Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)

本文介绍了在警报上单击“确定"或通过 jquery/javascript 确认对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑用backbone.js 和jquery 编写一些UI 测试.它们可能不是最好的方法,但这是我正在考虑的事情 - 通过纯代码自动化测试而无需记录和回放.

I was thinking of writing some UI tests in backbone.js and jquery. They may not be the best way to do it but it's something that I was thinking about - to automate the tests without record and playback - through plain code.

使用这种方法唯一让我摸不着头脑的是:在某些用例流程"(执行)中会出现确认/警报对话框.我想单击确定"并继续流程 - 这甚至可以通过纯 JavaScript 代码实现吗?怎么样?

The only thing that made me scratch my head using this approach is this: In some 'use-case flow' (of the execution) confirm/alert dialogs would show up. I'd like to click 'Ok' and continue the flow - is this even doable through plain javascript code? How?

注意:我确实知道存在 GUI 测试库,但我想知道如何仅使用 jQuery/javascript 代码来完成它,如果可能的话.

Note: I do know GUI testing libraries exist, but I want to know how to do it using just jQuery/javascript code, if at all possible.

推荐答案

据我所知,如果您使用标准的 alert() 调用,则无法触发确定"点击,因为警报调用阻塞正常的 JS 事件循环.

As far as I know if you use a standard alert() call you cannot trigger an "OK" click because the alert call blocks the normal JS event loop.

但是您应该能够将 window.alert 和 window.confirm 替换为您自己的不执行任何操作的函数:

However you should be able to replace window.alert and window.confirm with your own function that does nothing:

window.alert = function() {
    console.log.apply(console, arguments);
};

在加载其他任何内容之前将它们放在 JS 的顶部,随后对 alert() 或 confirm() 的任何调用都会改为调用它们.

Place these at the top of your JS before anything else is loaded and any subsequent calls to alert() or confirm() will call these instead.

这篇关于在警报上单击“确定"或通过 jquery/javascript 确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在警报上单击“确定"或通过 jquery/javascript 确认对话框?