Clicking #39;OK#39; on alert or confirm dialog through 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 确认对话框?


基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01