<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ketan&#039;s Musings &#187; reuse functional tests</title>
	<atom:link href="http://ketan.padegaonkar.name/tag/reuse-functional-tests/feed" rel="self" type="application/rss+xml" />
	<link>http://ketan.padegaonkar.name</link>
	<description>Where he blogs about his eclipse musings</description>
	<lastBuildDate>Sat, 26 Feb 2011 18:24:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Reusing Functional Tests &#8211; Part 1</title>
		<link>http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html</link>
		<comments>http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html#comments</comments>
		<pubDate>Sat, 21 Jun 2008 07:18:17 +0000</pubDate>
		<dc:creator>Ketan</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[reuse functional tests]]></category>
		<category><![CDATA[swtbot]]></category>

		<guid isPermaLink="false">http://ketan.padegaonkar.name/?p=232</guid>
		<description><![CDATA[While writing some tests for SWTBot, there was quite some code duplication that could be removed by using some simple test patterns. This is part of a series of articles about how test code could be written better. This example uses SWTBot as a driver to drive an Eclipse application used by ACME Corporation. Here [...]]]></description>
			<content:encoded><![CDATA[<p>While writing some tests for <a href="http://swtbot.org">SWTBot</a>, there was quite some code duplication that could be removed by using some simple test patterns. This is part of a series of articles about how test code could be written better. This example uses <a href="http://swtbot.org">SWTBot</a> as a driver to drive an Eclipse application used by <a href="http://en.wikipedia.org/wiki/Acme_Corporation">ACME Corporation</a>.</p>
<p>Here is a snippet of a Test Case that creates an account in ACME Corporation&#8217;s customer management system.</p>
<pre>
<span style="font-family: monospace;font-size: 10pt"><span>public</span> <span>void</span> <span>testCreatesAccount()</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>bot.menu(</span><span>&quot;Accounts&quot;</span><span>).menu(</span><span>&quot;New&quot;</span><span>).click();</span>
    <span>// nice wizard
    </span><span>bot.shell(</span><span>&quot;Create a new account&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Name:&quot;</span><span>).setText(</span><span>&quot;Will Coyote&quot;</span><span>);</span>
    <span>bot.textWithLabel(</span><span>&quot;Email:&quot;</span><span>).setText(</span><span>&quot;will@coyote&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Next&gt;&quot;</span><span>).click();</span>
    <span>// yet another wizard
    </span><span>bot.shell(</span><span>&quot;Address Details&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Mailing Address:&quot;</span><span>).setText(</span><span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Finish&quot;</span><span>).click();</span>

    <span>assertThatAccountIsCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>
<span>}</span></span>
</pre>
<p>That&#8217;s a great start to begin writing tests. But there&#8217;s the problem: What happens if the test fails somewhere and the &#8220;Create a new account&#8221; or the &#8220;Address Details&#8221; window remains open ? This means that the rest of the tests fail or do not run at all. Not a problem, tearDown() to the rescue:</p>
<pre>
<span style="font-family: monospace;font-size: 10pt"><span>protected</span> <span>void</span> <span>tearDown()</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>closeWindow(</span><span>&quot;Create a new account&quot;</span><span>);</span>
    <span>closeWindow(</span><span>&quot;Address Details&quot;</span><span>);</span>
<span>}</span>

<span>private</span> <span>void</span> <span>closeWindow(String</span> <span>windowTitle)</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>try</span> <span>{</span>
    <span>bot.shell(windowTitle).close();</span>
    <span>}</span> <span>catch</span> <span>(WidgetNotFoundException</span> <span>e)</span> <span>{</span>
        <span>// do nothing if the window is not found
    </span><span>}</span>
<span>}</span></span>
</pre>
<p>Now we write a test that tests if you&#8217;re able to create two users with the same name.</p>
<pre>
<span style="font-family: monospace;font-size: 10pt"><span>public</span> <span>void</span> <span>testShouldNotCreateTwoAccountsWithTheSameName()</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>bot.menu(</span><span>&quot;Accounts&quot;</span><span>).menu(</span><span>&quot;New&quot;</span><span>).click();</span>
    <span>// nice wizard
    </span><span>bot.shell(</span><span>&quot;Create a new account&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Name:&quot;</span><span>).setText(</span><span>&quot;Will Coyote&quot;</span><span>);</span>
    <span>bot.textWithLabel(</span><span>&quot;Email:&quot;</span><span>).setText(</span><span>&quot;will@coyote&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Next&gt;&quot;</span><span>).click();</span>
    <span>// yet another wizard
    </span><span>bot.shell(</span><span>&quot;Address Details&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Mailing Address:&quot;</span><span>).setText(</span><span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Finish&quot;</span><span>).click();</span>

    <span>assertThatAccountIsCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>

    <span>bot.menu(</span><span>&quot;Accounts&quot;</span><span>).menu(</span><span>&quot;New&quot;</span><span>).click();</span>
    <span>// nice wizard
    </span><span>bot.shell(</span><span>&quot;Create a new account&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Name:&quot;</span><span>).setText(</span><span>&quot;Will Coyote&quot;</span><span>);</span>
    <span>bot.textWithLabel(</span><span>&quot;Email:&quot;</span><span>).setText(</span><span>&quot;will@coyote&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Next&gt;&quot;</span><span>).click();</span>
    <span>// yet another wizard
    </span><span>bot.shell(</span><span>&quot;Address Details&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Mailing Address:&quot;</span><span>).setText(</span><span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>bot.button(</span><span>&quot;Finish&quot;</span><span>).click();</span>

    <span>assertThatAccountIsNotCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>
<span>}</span></span>
</pre>
<p>Duplication, not a big deal, some bit of refactoring and you get to this. This also reduces the number of places where you&#8217;ll need to fix the test, if the label on one of the screens change.</p>
<pre>
<span style="font-family: monospace;font-size: 10pt"><span>public</span> <span>void</span> <span>testCreatesAccount()</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>createAccount(</span><span>&quot;Will Coyote&quot;</span><span>,</span> <span>&quot;will@coyote&quot;</span><span>,</span> <span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>assertThatAccountIsCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>
<span>}</span>

<span>public</span> <span>void</span> <span>testShouldNotCreateTwoAccountsWithTheSameName()</span> <span>throws</span> <span>Exception</span> <span>{</span>
    <span>createAccount(</span><span>&quot;Will Coyote&quot;</span><span>,</span> <span>&quot;will@coyote&quot;</span><span>,</span> <span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>assertThatAccountIsCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>
    <span>createAccount(</span><span>&quot;Will Coyote&quot;</span><span>,</span> <span>&quot;will@coyote&quot;</span><span>,</span> <span>&quot;Will Coyote, middle of, nowhere&quot;</span><span>);</span>
    <span>assertThatAccountIsNotCreated(</span><span>&quot;Will Cooyte&quot;</span><span>,</span> <span>&quot;will@coyote.com&quot;</span><span>);</span>
<span>}</span>

<span>private</span> <span>void</span> <span>createAccount(String</span> <span>name,</span> <span>String</span> <span>email,</span> <span>String</span> <span>snailMail)</span> <span>throws</span> <span>WidgetNotFoundException,</span> <span>TimeoutException</span> <span>{</span>
    <span>bot.menu(</span><span>&quot;Accounts&quot;</span><span>).menu(</span><span>&quot;New&quot;</span><span>).click();</span>
    <span>// nice wizard
    </span><span>bot.shell(</span><span>&quot;Create a new account&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Name:&quot;</span><span>).setText(name);</span>
    <span>bot.textWithLabel(</span><span>&quot;Email:&quot;</span><span>).setText(email);</span>
    <span>bot.button(</span><span>&quot;Next&gt;&quot;</span><span>).click();</span>
    <span>// yet another wizard
    </span><span>bot.shell(</span><span>&quot;Address Details&quot;</span><span>).activate();</span>
    <span>bot.textWithLabel(</span><span>&quot;Mailing Address:&quot;</span><span>).setText(snailMail);</span>
    <span>bot.button(</span><span>&quot;Finish&quot;</span><span>).click();</span>
<span>}</span></span>
</pre>
<p>In the next article, you&#8217;ll get to see how you can reuse code better by using <em>Screen Objects</em></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Reusing+Functional+Tests+-+Part+1&amp;link=http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html&amp;notes=While%20writing%20some%20tests%20for%20SWTBot%2C%20there%20was%20quite%20some%20code%20duplication%20that%20could%20be%20removed%20by%20using%20some%20simple%20test%20patterns.%20This%20is%20part%20of%20a%20series%20of%20articles%20about%20how%20test%20code%20could%20be%20written%20better.%20This%20example%20uses%20SWTBot%20as%20a%20driver%20to%20drive%20an%20Eclipse%20application%20used%20by%20ACME%20Cor&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Reusing+Functional+Tests+-+Part+1&amp;link=http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html&amp;notes=While%20writing%20some%20tests%20for%20SWTBot%2C%20there%20was%20quite%20some%20code%20duplication%20that%20could%20be%20removed%20by%20using%20some%20simple%20test%20patterns.%20This%20is%20part%20of%20a%20series%20of%20articles%20about%20how%20test%20code%20could%20be%20written%20better.%20This%20example%20uses%20SWTBot%20as%20a%20driver%20to%20drive%20an%20Eclipse%20application%20used%20by%20ACME%20Cor&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Reusing+Functional+Tests+-+Part+1&amp;link=http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html&amp;notes=While%20writing%20some%20tests%20for%20SWTBot%2C%20there%20was%20quite%20some%20code%20duplication%20that%20could%20be%20removed%20by%20using%20some%20simple%20test%20patterns.%20This%20is%20part%20of%20a%20series%20of%20articles%20about%20how%20test%20code%20could%20be%20written%20better.%20This%20example%20uses%20SWTBot%20as%20a%20driver%20to%20drive%20an%20Eclipse%20application%20used%20by%20ACME%20Cor&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=RT+%40ketanpkr+%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Reusing+Functional+Tests+-+Part+1&amp;link=http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html&amp;notes=While%20writing%20some%20tests%20for%20SWTBot%2C%20there%20was%20quite%20some%20code%20duplication%20that%20could%20be%20removed%20by%20using%20some%20simple%20test%20patterns.%20This%20is%20part%20of%20a%20series%20of%20articles%20about%20how%20test%20code%20could%20be%20written%20better.%20This%20example%20uses%20SWTBot%20as%20a%20driver%20to%20drive%20an%20Eclipse%20application%20used%20by%20ACME%20Cor&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Reusing+Functional+Tests+-+Part+1&amp;link=http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html&amp;notes=While%20writing%20some%20tests%20for%20SWTBot%2C%20there%20was%20quite%20some%20code%20duplication%20that%20could%20be%20removed%20by%20using%20some%20simple%20test%20patterns.%20This%20is%20part%20of%20a%20series%20of%20articles%20about%20how%20test%20code%20could%20be%20written%20better.%20This%20example%20uses%20SWTBot%20as%20a%20driver%20to%20drive%20an%20Eclipse%20application%20used%20by%20ACME%20Cor&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ketan.padegaonkar.name/2008/06/21/reusing-functional-tests-part-1.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

