Search the web
Sign In
New User? Sign Up
TestFirstUserInterfaces
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
asserting HTML by example   Message List  
Reply | Forward Message #1037 of 1051 |
extremists:

HTML is easy to test and hard to test well. A unit test must intercept and parse
it, hopefully _before_ it goes thru a web server.

I just wrote an assertion which turns that problem on its head. Instead of
asserting how to parse that HTML, we will assert how to build an example of HTML
that the production HTML must match.

This post is the high-level, language-agnostic, overview of these announcement
posts:

http://www.ruby-forum.com/topic/181145#792950

Here's the example:

user = users(:Moses)
get :edit_user, :id => user.id # fetches a web page

assert_xhtml do

form :action => '/users' do
fieldset do
legend 'Personal Information'
label 'First name'
input :type => 'text',
:name => 'user[first_name]'
:value => user.first_name
end
end

end

That's all. Inside the assertion's block, you declare the HTML you want, using a
Builder notation. (Nokogiri, in Ruby's case.)

The assertion skips over any intervening elements, such as <ul>, <table>, etc,
that the block did not build. In the above example, <fieldset> only needs to
appear somewhere inside the <form> - not necessarily its first element. But
those <label> and <input> fields must appear somewhere inside that <fieldset>,
and in the given order!

To create this assertion in your language, start with your nearest HTML builder.
Allow the user-programmer to build an example, then traverse its DOM, and
convert each item into an XPath seeking that item in the production HTML.
Declare a match if all the specified attributes match, and if most of the text
matches - disregarding blanks.

Then, for each match, recursively match each built child element, applying its
XPath relative to the current node in the production HTML's DOM.

When you find a fault, build an error message by rendering the current nodes to
HTML, and return this out of your loop statements. This Gist explicates the
algorithm:

http://gist.github.com/76136

The algorithm essentially matches two trees, depth-first and in-order. The
example HTML must be a subset of the production HTML.

If you survive all that recursing and matching then your assertion passes. You
verified a huge number of details, using extremely lean code, and you ignored
many more details, allowing them to change freely as you upgrade your website.

--
Phlip
http://www.zeroplayer.com/



Thu Mar 12, 2009 5:27 am

phlipcpp
Offline Offline
Send Email Send Email

Forward
Message #1037 of 1051 |
Expand Messages Author Sort by Date

extremists: HTML is easy to test and hard to test well. A unit test must intercept and parse it, hopefully _before_ it goes thru a web server. I just wrote an...
Phlip
phlipcpp
Offline Send Email
Mar 12, 2009
5:28 am

For structural HTML testing you should check out assert_select. It comes with Rails 2.0. Useful for testing XML too. ...
Jason Citron
clash_y
Offline Send Email
Mar 12, 2009
5:33 am

... Try writing the equivalent of my assertion with it. I suspect you can't, because it can't constrain the element order. And the rest would be extremely...
Phlip
phlipcpp
Offline Send Email
Mar 12, 2009
12:29 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help