使用 WatiN,我想等待 jQuery 事件
我希望有人已经做到了这一点。我有一些自定义 jQuery 修改,它们利用 jQuery 事件系统来启动某些事件进行处理。我希望能够针对这些进行自动化测试。
有没有人研究过从 WatiN 到 jQuery 的更好的结合?我看过一些关于 jQuery 选择器传递的帖子,以及这里关于等待给定文本更改的帖子...如果有人添加 jQuery 选择和事件支持,那就太酷了...甚至是 document.getElementsBySelector可能会很好。
I'm hoping that someone out there has done this. I have some custom jQuery modifications that utilize the jQuery event system in order to launch certain events for processing. I'd like to be able to do automated testing in response to these.
Has anyone worked on a better tie in from WatiN to jQuery? I've seen a few posts on a passthrough for jQuery selectors, as well as a post here about waiting for given text to change... Would be cool if someone added jQuery selection, and event support... even the document.getElementsBySelector might be nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道这是否是您所期望的,但我花了一些时间进行调查,并找到了问题的解决方案:有了 WatiN,我想等待 jQuery 事件。
目标是用 C# 编写:
我编写了一个测试页(可在 http://www.sp4ce.net 上获取) /debug.html),当您单击按钮时,它会触发链接上的
onclick
事件。然后我编写了扩展方法
.WaitForEvent
该方法使用
EventMonitorWatcher
来检查事件是否已被调用。该监视器使用三个 javascript(jquery 支持)方法来启动监视、停止监视并检查事件是否已被调用。
在这里,您需要在运行浏览器时将此 javascript 作为字符串注入您的页面。
在这个测试中,当你点击“click”按钮时,测试应该直接结束,并且你的浏览器被关闭;
I don't know if it is what you expect, but I had some time to investigate and I came with a solution to the question : With WatiN, I'd like to wait for a jQuery event.
The goal is to write in C#:
I wrote a test page (available on http://www.sp4ce.net/debug.html) where it triggers a
onclick
event on the link when you click on the button.Then I wrote the extension method
.WaitForEvent
This methods uses a
EventMonitorWatcher
that check is the event has been called.This monitor use three javascript (jquery powered) methods that starts a monitoring, stops a monitoring and checks if the event has been called.
Here you are, you then need to inject this javascript as a string your page when you run the browser.
In this test, when you click on the "click" button, the test should end directly and your browser be closed;
为什么不注入一些附加到您想要侦听的事件的 JavaScript。在事件触发时调用的 javascript 事件代码中,您可以将一个元素添加到 DOM 中,其中包含您感兴趣的结果。例如,创建一个 Div 并使用您想要检查的输出设置 innerHtml。给 Div 一个唯一的 ID。
基本流程:
browser.RunScript(javascript_here_to_attach_to_the_events_you_are_interested_in)
让我们假设当事件被触发时,您注入的代码创建一个 Id = "my_event_result" 的 Div。
执行触发验证代码的操作(例如输入正确/错误的值)
示例: browser.TextField(textFieldIdWithValidation).Typetext("some value");
等待 Div 出现:
browser.Div("my_event_result").WaitUntilExists();
在 div 中断言值:
bool result = browser.Div("my_event_result").Text == "The预期测试";
哈特哈,
杰罗恩
Why not inject some javascript which attaches to the event(s) you want to listen for. In the your javascript eventcode that gets called when the event(s) fire, you can add an element to the DOM with the results you are interested in. Create a Div for instance and set the innerHtml with the output you want to check. Give the Div an unique ID.
Basic flow:
browser.RunScript(javascript_here_to_attach_to_the_events_you_are_interested_in)
Lets assume that when the event is fired your injected code creates a Div with the Id = "my_event_result".
Do the actions that trigger your validation code (like inputting good/wrong values)
example: browser.TextField(textFieldIdWithValidation).Typetext("some value");
Wait for the Div to appear:
browser.Div("my_event_result").WaitUntilExists();
Assert the value(s) in the div:
bool result = browser.Div("my_event_result").Text == "The expected test";
HTH,
Jeroen
我不希望这是正确的答案,因为我没有时间查看 WatiN,只有一些注释可以帮助您完成您的工作,第一个猴子补丁 jQuery:
因为所有 jQuery 事件都是通过该函数触发的扳机。
这将使您的页面在每次触发某些事件时提醒给定的所有参数(作为以“-”作为分隔符的字符串),通常第一个参数是事件。 WatiN 应该具有某种可以捕获警报的功能,如果没有,我建议放弃它并使用 watij 之类的东西,我个人推荐 HTMLUNIT
另外,如果您对触发事件的原因感兴趣,在上面的函数中,
this
将指向该事件:)您应该检查一下:
http://api.jquery.com/event.result/
http://api.jquery.com/event.data/
I'm not expecting for this to be the right answer since I dont have the time to check out WatiN, just a couple of notes that will help you do your thing, 1st monkey patch jQuery:
since all jQuery events is triggered with the function trigger.
This would make your page alert all the arguments given each time something is triggered (as a string with '-' as a separator), usually the first argument would be the event. WatiN should have some kind of function that will capture alerts, if not I suggest dropping it and going with watij or something, personally I recommend HTMLUNIT
Additionally if your interested in what triggered the event, inside the function above,
this
would point to that :)Stuff you should check out:
http://api.jquery.com/event.result/
http://api.jquery.com/event.data/