简体   繁体   English

我可以对DOM进行单元测试吗?

[英]Can I unit test the DOM?

We have a Ruby on Rails app that uses some javascript in the views. 我们有一个Ruby on Rails应用程序,它在视图中使用了一些javascript。 Since we have good test coverage on the rest of the app, we want to continue that here. 由于我们对应用程序的其余部分有良好的测试覆盖率,因此我们希望在此处继续。 The js we use manipulates the DOM to show/hide divs and perform incremental searching for a combobox selecting from a table. 我们使用的js操纵DOM来显示/隐藏div并执行从表中选择的组合框的增量搜索。

Does anyone know of ways to test DOM manipulation? 有谁知道测试DOM操作的方法?

Frank 坦率

You can use the java libraries HttpUnit or HtmlUnit that can test the finished DOM after being manipulated by JS. 您可以使用java库HttpUnit或HtmlUnit,它们可以在JS操作后测试完成的DOM。

http://htmlunit.sourceforge.net/javascript-howto.html http://httpunit.sourceforge.net/doc/cookbook.html http://htmlunit.sourceforge.net/javascript-howto.html http://httpunit.sourceforge.net/doc/cookbook.html

Both use the full-featured JavaScript engine Mozilla Rhino, and then add their own DOM implementations. 两者都使用功能齐全的JavaScript引擎Mozilla Rhino,然后添加自己的DOM实现。

This will allow you to run these test automatically in the background without any browser or sideeffects. 这将允许您在后台自动运行这些测试,而不会产生任何浏览器或副作用。

Bonus: You can use call these libraries directly from Ruby using JRuby. 额外:您可以使用JRuby直接从Ruby调用这些库。

jQuery library ships with a pretty good unit testing library you can use to test dom manipulation. jQuery库附带了一个非常好的单元测试库,可用于测试dom操作。 The library's called QUnit. 该库名为QUnit。

您可以使用YUI TestSelenium

使用capybara https://github.com/jnicklas/capybara与启用JS的驱动程序,如capybara-webkit或poltergeist。

过去我用jsUnit完成了这个。

I just answered a similar question . 我刚刚回答了类似的问题 In short: take a look at Dojo's DOH , doh.robot and dijit.robotx (using Dojo is not required for DOH, and it doesn't depend on Dojo). 简而言之:看看Dojo的DOHdoh.robotdijit.robotx (DOH不需要使用Dojo,它不依赖于Dojo)。

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

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