<?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>marthijn. &#187; Watir</title>
	<atom:link href="http://www.marthijnvandenheuvel.com/tag/watir/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marthijnvandenheuvel.com</link>
	<description></description>
	<lastBuildDate>Fri, 30 Dec 2011 13:03:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Unit testing in Watir</title>
		<link>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/</link>
		<comments>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 15:17:12 +0000</pubDate>
		<dc:creator>Marthijn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Watir]]></category>

		<guid isPermaLink="false">http://www.marthijnvandenheuvel.com/?p=149</guid>
		<description><![CDATA[Write a simple unit test in Watir]]></description>
			<content:encoded><![CDATA[<p><a href="http://watir.com/" target="_blank">Watir</a> is an open-source Ruby library for automating web browsers. Combined with unit testing for Ruby it is a really powerful tool to test websites and webapplications. In this post I explain how to write a test for a simple website.</p>
<div id="attachment_150" class="wp-caption aligncenter" style="width: 120px"><a href="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/04/watir.png" rel="lightbox[149]"><img class="size-full wp-image-150" title="Watir" src="http://www.marthijnvandenheuvel.com/wp-content/uploads/2010/04/watir.png" alt="Watir" width="110" height="103" /></a><p class="wp-caption-text">Watir</p></div>
<p><span id="more-149"></span></p>
<p>First download and install <a href="http://www.ruby-lang.org/en/downloads/" target="_blank">Ruby</a>. If you are familiar with <a href="http://www.eclipse.org/" target="_blank">Eclipse</a>, <a href="http://www.aptana.com/" target="_blank">Aptana</a> is a nice web development plugin (see <a href="http://osdir.com/ml/RubyonRailsTalk/2009-08/msg02136.html" target="_blank">this simple installation tutorial</a>). If you don&#8217;t know how to use Ruby, <a href="http://www.basvandijk.eu/" target="_blank">Bas</a> has a <a href="http://www.basvandijk.eu/2010/03/28/getting-started-with-ruby/" target="_blank">nice article</a> about getting started with Ruby.</p>
<p>The second step is <a href="http://watir.com/installation/" target="_blank">install Watir</a>. You can try <a href="http://watir.com/examples/" target="_blank">these examples</a> to see what Watir does.</p>
<h2>Testing a simple website</h2>
<p>For one of my school projects I had to write a simple test for one of the function of this <a href="http://www.calcenstein.com/" target="_blank">online calculator</a>. In this tutorial we will test the <a href="http://www.calcenstein.com/calc/0416.php" target="_blank">GCF and LCM function</a>. Start with creating a new Ruby script containing the right requirements and a test case class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'watir'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test/unit'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> TC_LCM_GCD <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span>
 <span style="color:#9966CC; font-weight:bold;">def</span> test_lcm_gcd
  <span style="color:#008000; font-style:italic;"># ...</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In order to let Watir start a new browser add the following to the test_lcm_gcd function:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Watir::Browser</span>.<span style="color:#9900CC;">default</span> = <span style="color:#996600;">&quot;ie&quot;</span>
b = <span style="color:#6666ff; font-weight:bold;">Watir::Browser</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.calcenstein.com/calc/0416.php&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># GCF and LCM</span></pre></div></div>

<p>If you want to use Firefox for testing, change &#8220;ie&#8221; to &#8220;firefox&#8221; and don&#8217;t forget to install the <a href="http://watir.com/installation/" target="_blank">plugin</a>. The second line starts a new browser window.</p>
<p>Our goal is to let Watir insert two values (the &#8216;First Value&#8217; and &#8216;Second Value&#8217; input field), and then press the &#8216;Solve&#8217; button. In order to do so we need to know the name or id (or value in some cases) of the input fields and the &#8216;Solve&#8217; button. Instead of looking at the source I can recommend to use the <a href="https://addons.mozilla.org/nl/firefox/addon/1843" target="_blank">Firebug</a> add-on for Firefox, and use the inspect function. The &#8216;First Value&#8217; input is named &#8216;a&#8217;, and the &#8216;Second Value&#8217; input is named &#8216;b&#8217;. We want to set &#8216;a&#8217; to 30 and &#8216;b&#8217; to 45:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">set</span> <span style="color:#996600;">&quot;30&quot;</span>
b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;b&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">set</span> <span style="color:#996600;">&quot;45&quot;</span></pre></div></div>

<p>When these values are set, Watir must press the &#8216;Solve&#8217; button. It appears this button doesn&#8217;t have a name or id, so get it by value and execute the click action:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">b.<span style="color:#9900CC;">button</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:value</span>, <span style="color:#996600;">&quot;Solve&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">click</span></pre></div></div>

<p>Now the click action is executed and the result is printed in the other two input fields (named &#8216;lcmA&#8217; and &#8216;gcdA&#8217;). Like in all other test cases, the result must be verified. The correct result is LCM = 90 and GCF = 15, so use assertions to check the values:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">assert<span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;lcmA&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">value</span> == <span style="color:#996600;">&quot;90&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
assert<span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color:#9900CC;">text_field</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">&quot;gcdA&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">value</span> == <span style="color:#996600;">&quot;15&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This simple test case is now complete. The test output should be something like this:</p>
<pre>Loaded suite My_test_suite
Started
.
Finished in 5.579 seconds.

1 tests, 2 assertions, 0 failures, 0 errors</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.marthijnvandenheuvel.com/2010/04/14/unit-testing-in-watir/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

