硒对事件做出反应
我目前正在使用最新版本的 Selenium 及其 .net 绑定来执行一些测试。 不幸的是,我必须模拟一些在可变时间发生的反应,并且只能在非常有限的时间间隔内执行,并且连续轮询 DOM 不够快。 Selenium 有没有办法挂钩 DOM 事件,以便我可以拦截代码中我感兴趣的事件? 如果是这样,示例 .net 代码将不胜感激。
提前致谢
P.S.:我对浏览器基本上不关心,但是在 Chrome 中工作的解决方案将是首选
I'm currently using the latest version of Selenium and its .net bindings to perform some tests.
Unfortunately I have to simulate some reaction that happens at a variable time and can only be performed within a very limited time interval, and polling the DOM continuously is not fast enough.
Is there a way for Selenium to hook to DOM events so that I could intercept the events I'm interested into in my code?
If so, sample .net code would be greatly appreciated.
Thanks in advance
P.S.: I'm largely indifferent to the browser, however a solution that works in Chrome would be preferred
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你唯一的希望就是用 JavaScript 来完成。您可以使用
ISelenium.GetEval()
或ISelenium.RunScript()
运行任意 JavaScript 代码。GetEval()
将同步执行代码并返回块中最后一个表达式的值。RunScript()
实际上会用您的代码在 DOM 中构造一个新的元素,这将导致浏览器异步运行它,直到 JavaScript 引擎启动空闲(即,至少在
RunScript()
命令完成之后)。您可能想使用 RunScript() 来挂钩所需的事件,然后让测试脚本等待,直到事件有足够的时间触发,然后您可以使用 GetEval()< /code> 检索或验证事件处理程序的结果。
Your only hope of this is to do it in JavaScript. You can run arbitrary JavaScript code using either
ISelenium.GetEval()
orISelenium.RunScript()
.GetEval()
will execute the code synchronously and return the value of the last expression in the block.RunScript()
will actually construct a new<SCRIPT>
element in the DOM with your code, which will cause the browser to run it asynchronously, waiting until the JavaScript engine is idle (i.e., until at least after theRunScript()
command completes).You probably want to use
RunScript()
to hook the event you want, and then have your test script wait until the event has had enough time to fire, then you can useGetEval()
to retrieve or verify the results of the event handler.