在警报上单击“确定"或通过 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.alertwindow.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 确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)