简体   繁体   English

使用JavaScript测试用户交互

[英]Testing User Interaction with JavaScript

I can't get Lettuce / Splinter or JsTestDriver to run tests of user interaction with Backbone-generated DOM objects. 我无法让Lettuce / Splinter或JsTestDriver来运行与Backbone生成的DOM对象进行用户交互的测试。 For example, at the hello backbone tutorial , I want to simulate the user's click on the button labeled "Add list item", then verify that a <li> element of text "hello world1" has appeared. 例如,在hello主干教程中 ,我要模拟用户单击标为“添加列表项”的按钮,然后验证是否出现了文本“ hello world1”的<li>元素。

JsTestDriver: I can get a reference to the button element and call click() on it, but then document.getElementsByTagName("li") fail (or return null?). JsTestDriver:我可以获取对按钮元素的引用并对其上的click()进行调用,但是然后document.getElementsByTagName("li")失败(或返回null?)。

Johansen's excellent book says that testing event handlers isn't properly a unit testing task. Johansen的出色著作说,测试事件处理程序不是正确的单元测试任务。 So I tried my BDD (?) tools for Django: 因此,我尝试了针对Django的BDD(?)工具:

Lettuce / Splinter / Django: The Splinter Browswer object can't see the text in the button, never mind get a reference to it or click it. Lettuce / Splinter / Django: Splinter Browswer对象看不到按钮中的文本,不必介意获取引用或单击它。 (It does handle these operations for elements created via HTML.) Neither world.browser.is_text_present() nor find_by_id() work; (它确实处理了通过HTML创建的元素的这些操作。)world.browser.is_text_present()和find_by_id()都不起作用。 passing a wait time to the former didn't help. 将等待时间传递给前者没有帮助。

I would really prefer to avoid going straight to Selenium, and thought that these tools made that unnecessary. 我真的更希望避免直接使用Selenium,并认为这些工具使这不必要。 So now what? 那么现在怎么办?

While firing DOM events may technically not be "unit testing", it doesn't mean you can't use a unit testing framework to do it :) This is more of a case of definition. 尽管从技术上讲,触发DOM事件可能不是“单元测试”,但这并不意味着您不能使用单元测试框架来完成它:)这更多是一种定义。

Anyway, you can achieve what you want with eg JsTestDriver, but I would advise against doing it manually through eg click() (which likely does not do what you expect it to). 无论如何,您都可以使用JsTestDriver来实现所需的功能,但是我建议您不要通过click()手动进行操作(这可能无法实现您的期望)。 I usually use jQuery to fire events, and this can safely be done with eg JsTestDriver. 我通常使用jQuery触发事件,并且可以安全地通过JsTestDriver完成。 This should work even if you don't use jQuery in your production code. 即使您在生产代码中不使用jQuery,这也应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM