Object Mentor Blog: Testing GUIs Part I: RoR.tag:blog.objectmentor.com,2005:TypoTypo2007-02-11T12:59:07-06:00Uncle Boburn:uuid:6fcd0b8d-2d9f-4d06-b44d-926bb905a0972007-01-13T17:24:00-06:002007-02-11T12:59:07-06:00Testing GUIs Part I: RoR.<p>Testing GUIs is one of the the holy grails of Test Driven Develoment (TDD). Many teams who have adopted <span class="caps">TDD</span> for other parts of their projects have, for one reason or another, been unable to adequately test the <span class="caps">GUI</span> portion of their code.</p>
<p>In this series of article I will show that <span class="caps">GUI</span> testing is a solved problem. Over the years the <span class="caps">TDD</span> community has produced and accumulated tools, frameworks, libraries, and techniques that allow any team to test their <span class="caps">GUI</span> code as fully as any other part of their code.</p><p>Testing GUIs is one of the the holy grails of Test Driven Develoment (TDD). Many teams who have adopted <span class="caps">TDD</span> for other parts of their projects have, for one reason or another, been unable to adequately test the <span class="caps">GUI</span> portion of their code.</p>
<p>In this series of article I will show that <span class="caps">GUI</span> testing is a solved problem. Over the years the <span class="caps">TDD</span> community has produced and accumulated tools, frameworks, libraries, and techniques that allow any team to test their <span class="caps">GUI</span> code as fully as any other part of their code.</p>
<h1>Testing GUIs Part I: <span style="color:red;">Ruby on Rails</span></h1>
<p>In the world of web development, no community has solved the problem of <span class="caps">GUI</span> testing better than the Ruby on Rails community. When you work on a rails project, testing the <span class="caps">GUI</span> is simply <em>de-rigeur</em>. The rails framework provides all the necessary tools and access points for testing all aspects of the application, including the generation of <span class="caps">HTML</span> and the structure of the resulting web pages.</p>
<p>Web pages in rails are specified by <code>.rhtml</code> files that contain a mixture of <span class="caps">HTML</span> and ruby code similar to the way Java and <span class="caps">HTML</span> are mixed in <code>.jsp</code> files. The difference is that <code>.rhtml</code> files are translated at runtime rather than being compiled into servlets the way <code>.jsp</code> pages are. This makes it very easy for the rails environment to generate the <span class="caps">HTML</span> for a web page <em>outside</em> of the web container. Indeed, the web server does not need to be running.</p>
<p>This ease and portability of generating <span class="caps">HTML</span> means that the rails test framework merely needs to set up the variables needed by the ruby scriptlets within the <code>.rhtml</code> files, generate the <span class="caps">HTML</span>, and then parse that <span class="caps">HTML</span> into a form that the tests can query.</p>
<h3>A typical example.</h3>
The tests query the <span class="caps">HTML</span> using an xpath-like syntax coupled with a suite of very powerful assertion functions. The best way to understand this is to see it. So here is a simple file named: <code>autocomplete_teacher.rhtml</code>.
<pre><code><ul class="autocomplete_list">
<% @autocompleted_teachers.each do |t| %>
<li class="autocomplete_item"><%= "#{create_name_adornment(t)} #{t.last_name}, #{t.first_name}"%></li>
<% end %>
</ul>
</code></pre>
You don’t have to be a ruby programmer to understand this. All it is doing is building an <span class="caps">HTML</span> list. The Ruby scriptlet between <code><% and %></code> tokens simple loops for each teacher creating an <code><li></code> tag from an “adornment”, and the first and last name. (The adornment happens to be the database id of the teacher in parentheses.)
A simple test for this <code>.rhtml</code> file is:
<pre><code> def test_autocomplete_teacher_finds_one_in_first_name
post :autocomplete_teacher, :request=>{:teacher=>"B"}
assert_template "autocomplete_teacher"
assert_response :success
assert_select "ul.autocomplete_list" do
assert_select "li.autocomplete_item", :count => 1
assert_select "li", "(1) Martin, Bob"
end
end</code></pre>
<ul>
<li>The <code>post</code> statement simply invokes the controller that would normally be invoked by a <span class="caps">POST</span> url of the form: <code>POST /teachers/autocomplete_teacher</code> with the <code>teacher</code> parameter set to <code>"B"</code>.</li>
<li>The first assertion makes sure that the controller rendered the <code>autocomplete_teacher.rhtml</code> template.</li>
<li>The next makes sure that the controller returned success.</li>
<li>the third is a compound assertion that starts by finding the <code><ul></code> tag with a <code>class="autocomplete_list"</code> attribute. (Notice the use of <code>css</code> syntax.)
<ul>
<li>Within this tag there should be an <code><li></code> tag with a <code>class="autocomplete_item"</code> attribute, </li>
<li>and containing the text <code>(1) Martin, Bob</code>.</li>
</ul></li>
</ul>
<p><br>
It should not come as any surprise that this test runs in a test environment in which the database has been pre-loaded with very specific data. For example, this test database always has “Bob Martin” being the first row (<code>id=1</code>) in the <code>Teacher</code> table.</p>
<p>The <code>assert_select</code> function is <em>very</em> powerful, and allows you to query large and complex <span class="caps">HTML</span> documents with surgical precision. Although this example give you just a glimpse of that power, you should be able to see that the rails testing scheme allows you to test that all the scriptlets in an <code>.rhtml</code> file are behaving correctly, and are correctly extracting data from the variables set by the controller.</p>
<h3>An example using <em>RSpec</em> and <em>Behavior Driven Design</em>.</h3>
<p>What follows is a more significant rails example that uses an alternate testing syntax known as Behavior Driven Design (BDD). The tool that accepts this syntax is called <em>RSpec</em>.</p>
Imagine that we have a page that records telephone messages taken from teachers at different schools. Part of that page might have an .rhtml syntax that looks like this:
<pre><code><h1>Message List</h1>
<table id="list">
<tr class="list_header_row">
<th class="list_header">Time</th>
<th class="list_header">Caller</th>
<th class="list_header">School</th>
<th class="list_header">IEP</th>
</tr>
<%time_chooser = TimeChooser.new%>
<% for message in @messages %>
<%cell_class = cycle("list_content_even", "list_content_odd")%>
<tr id="list_content_row">
<td id="time" class="<%=cell_class%>"><%=h(time_chooser.format_time(message.time)) %></td>
<td id="caller" class="<%=cell_class%>"><%=h person_name(message.caller) %></td>
<td id="school" class="<%=cell_class%>"><%=h message.school.name %></td>
<td id="iep" class="<%=cell_class%>"><%=h (message.iep ? "X" : "") %></td>
</tr>
<% end %>
</table>
</code></pre>
Clearly each message has a time, caller, school, and some kind of boolean field named “IEP”.
We can test this <code>.rhtml</code> file with the following <em>RSpec</em> specification:
<pre><code>context "Given a request to render message/list with one message the page" do
setup do
m = mock "message"
caller = mock "person",:null_object=>true
school = mock "school"
m.should_receive(:school).and_return(school)
m.should_receive(:time).and_return(Time.parse("1/1/06"))
m.should_receive(:caller).any_number_of_times.and_return(caller)
m.should_receive(:iep).and_return(true)
caller.should_receive(:first_name).and_return("Bob")
caller.should_receive(:last_name).and_return("Martin")
school.should_receive(:name).and_return("Jefferson")
assigns[:messages]=[m]
assigns[:message_pages] = mock "message_pages", :null_object=>true
render 'message/list'
end
specify "should show the time" do
response.should_have_tag :td, :content=>"12:00 AM 1/1", :attributes=>{:id=>"time"}
end
specify "should show caller first and last name" do
response.should_have_tag :td, :content=>"Bob Martin", :attributes=>{:id=>"caller"}
end
specify "should show school name" do
response.should_have_tag :td, :content=>"Jefferson", :attributes=>{:id=>"school"}
end
specify "should show the IEP field" do
response.should_have_tag :td, :content=>"X",:attributes=>{:id=>"iep"}
end
end
</code></pre>
I’m not going to explain the <code>setup</code> function containing all that <em>mock</em> stuff you see at the start. Let me just say that the mocking facilities of <em>RSpec</em> are both powerful and convenient. Actually you shouldn’t have too much trouble understanding the <code>setup</code> if you try; but understanding it is not essential for this example. The interesting testing is in the <code>specify</code> blocks.
<p>You shouldn’t have too much trouble reading the <code>specify</code> blocks. You can understand all of them if you understand the first. Here is what it does:</p>
<ul>
<li>The first spec ensures that <code><td id="time">12:00 AM 1/1</td></code> exists in the <span class="caps">HTML</span> document. This is not a string compare. Rather it is a semantic equivalence. Whitespace, and other attributes and complications are ignored. This spec will pass as long as there is a <code>td</code> tag with the appropriate id and contents.</li>
</ul>
<h3><span class="caps">HTML</span> Testing Discipline and Strategy</h3>
<p>One of the reasons that <span class="caps">GUI</span> testing has been so problematic in the <code>.jsp</code> world is that the java scriptlets in those files often reach out into the overall application domain and touch code that ties them to the web container and the application server. For example, if you make a call from a <code>.jsp</code> page to a database gateway, or an entity bean, or some other structure that is tied to the database; then in order to test the <code>.jsp</code> you have to have the full enabling context running. Rails gets away with this because the enabling context is lightweight, portable, and disconnected from the web container, and the live database. Even so, rails applications are not always as decoupled as they should be.</p>
<p>In Rails, Java, or any other web context, the discipline should be to make sure that none of the scriptlets in the <code>.jsp</code>, <code>.rhtml</code>, etc. files know anything at all about the rest of the application. Rather, the controller code should load up data into simple objects and pass them to the scriptlets (typically in the <code>attributes</code> field of the <code>HttpServletRequest</code> object or its equivalent). The scriptlets can fiddle with the format of this data (e.g. data formats, money formats, etc.) but <em>should not do any calculation, querying, or other business rule or database processing.</em> Nor should the scriptlets navigate through the model objects or entities. Rather the controller should do all the navigating, gathering, and calculating and present the data to the scriptlets in a nice little package.</p>
<p>If you follow this simple design discipline, then your web pages can be generated completely outside of the web environment, and your tests can parse and inspect the html in a simple and friendly environment.</p>
<h3>Conclusion</h3>
<p>I’ll have more to say about <em>RSpec</em> in a future blog. <span class="caps">BDD</span> is an exciting twist on the syntax of testing, that has an effect far greater than the simple syntax shift would imply.</p>
<p>I hope this article has convinced you that the rails community has solved the problem of testing <span class="caps">HTML</span> generation. This solution can be easily extrapolated back to Java and .NET as future blogs in this series will show.</p>
<p>Clearly the problem of testing Javascript, and the ever more complex issues of Web2.0 and <span class="caps">GTK</span> are not addressed by this scheme. However, there are solutions for Javascript that we will investigate in future blogs in this series.</p>
<p>Finally, this technique does not test the integration and workflow of a whole application. Again, those are topics for later blogs.</p>
<p>I hope this kickoff blog has been informative. If you have a comment, question, or even a rant, please don’t hesitate to add a comment to this blog.</p>Ray Ban Wayfarer sunglassesurn:uuid:8c0e4375-5047-4d52-b3dd-7e3bd40e07e22012-05-24T02:17:22-05:002012-05-24T02:17:22-05:00Comment on Testing GUIs Part I: RoR. by Ray Ban Wayfarer sunglasses<p>The Unique<a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>Ray Ban Wayfarer uk</strong></a> design was developed in 1952 by visual developer Raymond Stegeman, who obtained a multitude of patents for Bausch and Lomb, <a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>new ray ban sunglasses</strong></a> parent or guardian organization. Like Ray-Ban Aviators, the <a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>ray ban sunglasses uk</strong></a> was initially developed and promoted as eyewear for pilots.</p>Ray Ban Wayfarer sunglassesurn:uuid:843ac673-afd2-41a7-b61b-1348f23944802012-05-24T02:15:02-05:002012-05-24T02:15:02-05:00Comment on Testing GUIs Part I: RoR. by Ray Ban Wayfarer sunglasses<p>The Unique<a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>Ray Ban Wayfarer uk</strong></a> design was developed in 1952 by visual developer Raymond Stegeman, who obtained a multitude of patents for Bausch and Lomb, <a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>new ray ban sunglasses</strong></a> parent or guardian organization. Like Ray-Ban Aviators, the <a href="http://www.newraybansunglassesuk.com/" rel="nofollow"><strong>ray ban sunglasses uk</strong></a> was initially developed and promoted as eyewear for pilots.</p>ping ironsurn:uuid:d91ed7ac-22fa-4b7a-ad72-42a0d78e2f562012-05-16T02:23:05-05:002012-05-16T02:23:05-05:00Comment on Testing GUIs Part I: RoR. by ping irons<p>Just know teachers collective strike, to<a href="http://www.sportsoutsale.com" rel="nofollow">ping g20 driver</a> blocking defaults salary ready to ‘run road’ boss.” The teachers that night to haikou meilan airport “intercept” and clashes happened.Hundreds of students and parents know the truth, worry about the tuition “skip stone”, also collective action<a href="http://www.sportsoutsale.com" rel="nofollow">ping g20 fairway woods</a> find li wei money back. Teachers and parents in the police station found that li wei said the company account is only 600 yuan, can’t pay the teacher’s salary. But li wei denied “run road”, said that the company did not collapse, although there are a wage but have been<a href="http://www.sportsoutsale.com" rel="nofollow">ping g20 irons</a> trying to make up for. According to preliminary statistics, the company defaults teacher pay plus education service fees have not yet provide a total of more than 100 ten thousand yuan.</p>???????urn:uuid:d1b20914-47f1-473a-a044-9a9332d307772012-05-11T07:23:04-05:002012-05-11T07:23:04-05:00Comment on Testing GUIs Part I: RoR. by ???????<p>Strongly recommended for shopping at
???
<a href="http://www.cafe-hbal.com/vb/" rel="nofollow">منتديات</a></p>Link Building Company Indiaurn:uuid:997410de-08b9-4428-a594-edfa8cb2c46b2012-05-01T14:35:25-05:002012-05-01T14:35:25-05:00Comment on Testing GUIs Part I: RoR. by Link Building Company India<p>Promote your websites with Linkbuildingtraffic.com: a search engine optimization and internet marketing company specializing in Professional Link Building + SEO and SEM services. please contact at Mail Id: <a href="mailto:linkbuildingtraffic@gmail.com" rel="nofollow">linkbuildingtraffic@gmail.com</a></p>Craig Chilversurn:uuid:534d0d79-81c5-458b-8a73-83336abbf6512012-05-01T12:30:24-05:002012-05-01T12:30:24-05:00Comment on Testing GUIs Part I: RoR. by Craig Chilvers<p>Your article is extremely impressive. I never considered that it was feasible to
accomplish something like that until after I looked over your post .
Please visit our website for more information .</p>louboutin salesurn:uuid:dcb91a4e-c66e-4328-988b-0f50869230702012-04-20T18:17:01-05:002012-04-20T18:17:01-05:00Comment on Testing GUIs Part I: RoR. by louboutin sales<p>Testing GUIs Part I: RoR.
137
hoo,good article!!I like the post!4</p>youurn:uuid:30841825-dd89-4c63-9dd8-7d4163aafb812012-04-20T10:36:48-05:002012-04-20T10:36:48-05:00Comment on Testing GUIs Part I: RoR. by you<p>I have bookmarked it.
<a href="http://mytorridcoupons.blogspot.com" rel="nofollow"> torrid coupons </a></p>reurn:uuid:3527abca-2728-4d4b-8638-544b92e933db2012-04-20T10:34:37-05:002012-04-20T10:34:37-05:00Comment on Testing GUIs Part I: RoR. by re<p>This is fantastic article.
<a href="http://medicalbillingandcodingjobs.biz" rel="nofollow"> Medical billing and coding jobs </a></p>youngbrownurn:uuid:ae41ff16-5ca0-4e25-8ae4-e6a7e6c6aea62012-03-27T03:46:26-05:002012-03-27T03:46:26-05:00Comment on Testing GUIs Part I: RoR. by youngbrown<p>Thanks for the information, I’ll visit the site again to get update information
<a href="http://toys.shop.ebay.in/" rel="nofollow">Toys</a></p>taylormade r11 driverurn:uuid:a41f3d0a-5e2d-4063-a129-1476a9f6b1d12012-03-26T20:12:20-05:002012-03-26T20:12:20-05:00Comment on Testing GUIs Part I: RoR. by taylormade r11 driver<p>to the hegemonictitleist 910D3 control of her life, love Ping G20 is not desire Continuous stretch of the war is about people’s nerves TaylorMade R11s Driverscut off powerful,</p>china craftsurn:uuid:5d95fcd9-d6a6-4d39-b189-2ca0f655ce3b2012-03-07T10:42:41-06:002012-03-07T10:42:41-06:00Comment on Testing GUIs Part I: RoR. by china crafts<p>I wanted to thank you for this great read!! I definitely enjoying every little bit of it. I have bookmarked it to check out new stuff you post.</p>Massagem Tantricaurn:uuid:3f52c61e-6e28-4c7c-801d-c433b4ad62a82012-03-05T16:11:40-06:002012-03-05T16:11:40-06:00Comment on Testing GUIs Part I: RoR. by Massagem Tantrica<p>They</p>
<p>’ll normally inform you whether the boots growing to be bought is</p>
<p>genuine or an imitation.</p>Payday Loansurn:uuid:00e9ea3c-6389-45e2-8f64-af03838d8cf32012-02-16T14:27:17-06:002012-02-16T14:27:17-06:00Comment on Testing GUIs Part I: RoR. by Payday Loans<p>Great post! Thank you so much for sharing this article about Testing GUI’s.</p>evdo wireless interneturn:uuid:2b03a365-903d-4035-978e-77357153c4502012-02-16T06:01:56-06:002012-02-16T06:01:56-06:00Comment on Testing GUIs Part I: RoR. by evdo wireless internet<p>That is one fine ride. The Shimano’s Dura-Ace Di2 and MSC Koncept frame are sublime. <a href="http://www.evdodepotusa.com" rel="nofollow">evdo wireless internet</a>.</p>iphone sms to mac backupurn:uuid:7829ca05-0f6c-4e41-8d51-bd9f00b6976d2012-02-15T20:21:25-06:002012-02-15T20:21:25-06:00Comment on Testing GUIs Part I: RoR. by iphone sms to mac backup<p>Most of us will delete the SMS file if the iPhone inbox is full. For some of the very important text file, you would like to save it to Mac hard drive and read it later or you need print them. So, why not export the text message to HDD and save it now?</p>PDF ? PPTurn:uuid:13916883-83f7-4ea7-a857-e76c200b17f82012-01-29T19:05:30-06:002012-01-29T19:05:30-06:00Comment on Testing GUIs Part I: RoR. by PDF ? PPT<p><a href="http://www.pdftoppt.ru" rel="nofollow">PDF ? PPT</a> PDF to PowerPoint Converter lets you convert PDF to PowerPoint (PPT, PPTX, PPS, PPTM, PPSX, PPSM, POT, etc.), fully supports MS 2010, 2007, 2003.</p>shakopee townhomesurn:uuid:b765d040-0fbd-4c5d-b778-6619f5eeb80d2012-01-20T02:42:15-06:002012-01-20T02:42:15-06:00Comment on Testing GUIs Part I: RoR. by shakopee townhomes<p>Thanks for the post. It was informative and interesting.</p>Wholesale clothing manufacturersurn:uuid:0904542f-1900-4014-9e95-51d352b9432b2012-01-19T22:04:01-06:002012-01-19T22:04:01-06:00Comment on Testing GUIs Part I: RoR. by Wholesale clothing manufacturers<p>Nice article, I’ve bookmarked this one.</p>led displaysurn:uuid:395565dc-6e2c-4679-a091-1cbc95b9d8852012-01-13T00:24:08-06:002012-01-13T00:24:08-06:00Comment on Testing GUIs Part I: RoR. by led displays<p>I have to say this is huge work.I have read the whole article and it is really superbly written article.Testing guis part 1 is really an informative article and wanna say we the readers wanna see some more like this.</p>Urdu poetryurn:uuid:cc560cb8-15e0-44ec-9859-b6c574c8ac972011-12-29T01:06:11-06:002011-12-29T01:06:11-06:00Comment on Testing GUIs Part I: RoR. by Urdu poetry<p>Thanks for sharing these wonderful comic videos with us. They are really funny. Will look after for some more updates.</p>dentist in houstonurn:uuid:8be42780-82d3-48a3-99ff-e22f4386eae62011-12-27T09:16:02-06:002011-12-27T09:16:02-06:00Comment on Testing GUIs Part I: RoR. by dentist in houston<p>First I must tell you we appreciate the great and informative entry.Need to admit that I never learned about this review, I noticed many new facts, in my opinion. God bless you for sharing this information useful and interesting and I also will probably anticipate other interesting posts you closer to the inside future.keep.</p>Software Testing Servicesurn:uuid:321e36a1-298a-481c-86d7-3515c80ce34a2011-11-28T00:17:44-06:002011-11-28T00:17:44-06:00Comment on Testing GUIs Part I: RoR. by Software Testing Services<p>The article very surprised to me! Your writing is good. In this I learned a lot! Thank you! Share with us the good times.
<b><<a href="http://www.kualitatem.com/" rel="nofollow">Software Quality Assurance</a></b></p>banyo dekorasyonurn:uuid:5bafb167-36a4-4113-bf88-2c7835bee8582011-11-24T06:48:34-06:002011-11-24T06:48:34-06:00Comment on Testing GUIs Part I: RoR. by banyo dekorasyon<p>Yes, I plan on discussing the testing of thick client GUIs, <a href="http://www.akinyapi.net/BanyoDekorasyon.html" rel="nofollow">Banyo Dekorasyon</a> and GUIs with lots of JavaScript.</p>car rental service delhiurn:uuid:5422e7df-1f60-41ff-ba86-9707839547dd2011-11-24T03:59:56-06:002011-11-24T03:59:56-06:00Comment on Testing GUIs Part I: RoR. by car rental service delhi<p>If only I had found this blog before. The advice in this post are very helpful and I will certainly read the other posts in this series too. Thank you for posting this.</p>