简体   繁体   中英

Testing User Interaction with JavaScript

I can't get Lettuce / Splinter or JsTestDriver to run tests of user interaction with Backbone-generated DOM objects. 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.

JsTestDriver: I can get a reference to the button element and call click() on it, but then document.getElementsByTagName("li") fail (or return null?).

Johansen's excellent book says that testing event handlers isn't properly a unit testing task. So I tried my BDD (?) tools for Django:

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. (It does handle these operations for elements created via HTML.) Neither world.browser.is_text_present() nor find_by_id() work; 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. 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.

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). I usually use jQuery to fire events, and this can safely be done with eg JsTestDriver. This should work even if you don't use jQuery in your production code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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