<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>igorbrejc.net &#187; White</title>
	<atom:link href="http://igorbrejc.net/category/development/testing/white/feed" rel="self" type="application/rss+xml" />
	<link>http://igorbrejc.net</link>
	<description>Just another developer's weblog</description>
	<lastBuildDate>Sun, 12 Feb 2012 06:47:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>White Facade &#8211; Fluent Interface For Writing Tests Using White</title>
		<link>http://igorbrejc.net/development/testing/white-facade-fluent-interface-for-writing-tests-using-white</link>
		<comments>http://igorbrejc.net/development/testing/white-facade-fluent-interface-for-writing-tests-using-white#comments</comments>
		<pubDate>Mon, 21 Apr 2008 17:06:01 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[Kosmos]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[White]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/openstreetmap/kosmos/white-facade-fluent-interface-for-writing-tests-using-white</guid>
		<description><![CDATA[A few days ago I wrote about my first experiences in using White. I mentioned I wanted to create a facade around the White API in order to avoid constantly retyping the same code for common use cases. Since fluent interfaces are in fashion now, I decided to create a fluent facade. Just joking, of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Image:Facadism_in_bucharest.jpg"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Facadism_in_bucharest.jpg/120px-Facadism_in_bucharest.jpg" height="131" width="175" /></a></p>

<p>A few days ago I wrote about <a href="http://igorbrejc.net/development/testing/using-white-to-test-kosmos-gui">my first experiences in using White</a>. I mentioned I wanted to create a facade around the White API in order to avoid constantly retyping the same code for common use cases. Since fluent interfaces are in fashion now, I decided to create a fluent facade. Just joking, of course.</p>

<p>I&#8217;ll present the facade through a few tests of a <a href="http://igorbrejc.net/kosmoshome">Kosmos GUI</a>, which is currently under construction. Kosmos GUI is a MDI application for displaying maps from <a href="http://www.openstreetmap.org/">OpenStreetMap</a> geo data. You have to load a Kosmos project, which contains information about where to find the data and how to render it. So after starting the application, the first user action would typically be to open the project:</p>

<p><a href="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos1.png"><img src="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos1-thumb.png" style="border-width: 0px" alt="Kosmos1" border="0" height="127" width="247" /></a></p>

<p>After clicking the <em>File|Open Project </em>menu item, a dialog for selecting the Kosmos project file appears:</p>

<p><a href="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos2.png"><img src="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos2-thumb.png" style="border-width: 0px" alt="Kosmos2" border="0" height="176" width="244" /></a></p>

<p>One you have selected the project file, a progress box is shown with an option to cancel the operation:</p>

<p><a href="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos3.png"><img src="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos3-thumb.png" style="border-width: 0px" alt="Kosmos3" border="0" height="131" width="389" /></a></p>

<p>If you let it finish, a map appears and some additional menu items are now available:</p>

<p><a href="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos4.png"><img src="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos4-thumb.png" style="border-width: 0px" alt="Kosmos4" border="0" height="341" width="405" /></a></p>

<h4>Test Code</h4>

<p>Here&#8217;s a sample test case which goes through the above steps:</p>

<div class="dean_ch" style="white-space: wrap;"><span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw1">void</span> OpenProject<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;facade.<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;File&quot;</span>, <span class="st0">&quot;Open Project&#8230;&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">FileDialog</span> <span class="br0">&#40;</span><span class="st0">&quot;Open Kosmos Project File&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">@&#8221;D:\MyStuff\projects\OsmUtils\trunk\Data\Kosmos\KosmosProjectExample1.xml&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">MainWindow</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ModalDialog</span> <span class="br0">&#40;</span><span class="st0">&quot;Loading Kosmos Project&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">MainWindow</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">Menu</span> <span class="br0">&#40;</span><span class="st0">&quot;View&quot;</span>, <span class="st0">&quot;Zoom In&quot;</span><span class="br0">&#41;</span>.<span class="me1">AssertIsEnabled</span> <span class="br0">&#40;</span><span class="kw1">true</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;View&quot;</span>, <span class="st0">&quot;Zoom In&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;View&quot;</span>, <span class="st0">&quot;Zoom Out&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;View&quot;</span>, <span class="st0">&quot;Zoom All&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>

<p>After loading the project the test case executes some of the zoom functions available in the menu. And here&#8217;s a test case which cancels the project loading (and then checks that the <em>View|Zoom In</em> menu item is still not enabled):</p>

<div class="dean_ch" style="white-space: wrap;"><span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw1">void</span> OpenProjectCanceled <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;facade.<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;File&quot;</span>, <span class="st0">&quot;Open Project&#8230;&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">FileDialog</span> <span class="br0">&#40;</span><span class="st0">&quot;Open Kosmos Project File&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">@&#8221;D:\MyStuff\projects\OsmUtils\trunk\Data\Kosmos\KosmosProjectExample1.xml&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">MainWindow</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ModalDialog</span> <span class="br0">&#40;</span><span class="st0">&quot;Loading Kosmos Project&quot;</span><span class="br0">&#41;</span>.<span class="me1">Button</span> <span class="br0">&#40;</span><span class="st0">&quot;Cancel&quot;</span><span class="br0">&#41;</span>.<span class="me1">Click</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; .<span class="me1">MainWindow</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">Menu</span> <span class="br0">&#40;</span><span class="st0">&quot;View&quot;</span>, <span class="st0">&quot;Zoom In&quot;</span><span class="br0">&#41;</span>.<span class="me1">AssertIsEnabled</span> <span class="br0">&#40;</span><span class="kw1">false</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>

<p>Hopefully the code is self-explanatory. The basic idea is for the facade to remember the current window or other UI element to perform actions on. You can change the active window at any moment. The facade is also supplied with some basic assertion methods useful in the test code (they throw InvalidOperationExceptions, so you can use them with any unit test framework).</p>

<p>Oh I almost forgot. This is how you instantiate the facade:</p>

<div class="dean_ch" style="white-space: wrap;">WhiteFacade facade = WhiteFacade.<span class="me1">Run</span> <span class="br0">&#40;</span>applicationFilePath<span class="br0">&#41;</span>;</div>

<p>And if the facade does not offer enough for you, you can always access the underlying White&#8217;s Application object through <strong>facade.Application</strong> property.</p>

<h4>Download</h4>

<p>You can download the latest version of the <strong>WhiteFacade</strong> class from <a href="http://downloads.igorbrejc.net/develop/WhiteFacade/">here</a>. I have to warn you though: the class is in its infancy and will evolve through my effort on testing Kosmos GUI. It provides only for some basic use cases, but I think those common cases represent the majority of the GUI test code. I just wanted to give you an idea on how to make writing GUI test code as painless as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/testing/white-facade-fluent-interface-for-writing-tests-using-white/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:thumbnail url="http://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Facadism_in_bucharest.jpg/120px-Facadism_in_bucharest.jpg" />
		<media:content url="http://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Facadism_in_bucharest.jpg/120px-Facadism_in_bucharest.jpg" medium="image" />
		<media:content url="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos1-thumb.png" medium="image">
			<media:title type="html">Kosmos1</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos2-thumb.png" medium="image">
			<media:title type="html">Kosmos2</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos3-thumb.png" medium="image">
			<media:title type="html">Kosmos3</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/uploads/2008/04/kosmos4-thumb.png" medium="image">
			<media:title type="html">Kosmos4</media:title>
		</media:content>
	</item>
		<item>
		<title>Using White To Test Kosmos GUI</title>
		<link>http://igorbrejc.net/development/testing/using-white-to-test-kosmos-gui</link>
		<comments>http://igorbrejc.net/development/testing/using-white-to-test-kosmos-gui#comments</comments>
		<pubDate>Sat, 19 Apr 2008 16:43:03 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[Kosmos]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[White]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/uncategorized/using-white-to-test-kosmos-gui</guid>
		<description><![CDATA[After a few weeks of work on my new framework for multidocument interfaces called BrekiViews (yes, I know I promised to write a little about it, but I just didn&#8217;t have enough time and concentration to put the brainstorms in my head into some coherent words) and refactoring Kosmos code to use it, I&#8217;m finally [...]]]></description>
			<content:encoded><![CDATA[<p>After a few weeks of work on my <a href="http://igorbrejc.net/openstreetmap/kosmos/the-future-of-kosmos">new framework for multidocument interfaces called BrekiViews</a> (yes, I know I promised to write a little about it, but I just didn&#8217;t have enough time and concentration to put the brainstorms in my head into some coherent words) and refactoring Kosmos code to use it, I&#8217;m finally able to show some basic stuff in the new Kosmos GUI application. It&#8217;s all pretty simple at the moment, I got just one menu item working: <strong>File | Open Project</strong>. But I wanted to use this opportunity and start using <a href="http://www.codeplex.com/white">White</a> from ThoughtWorks to write automated GUI tests, since I was looking forward to trying the White out.</p>

<h4>Documentation (Or The Lack Of It)</h4>

<p>Unfortunately, the first impressions aren&#8217;t that good. The worst problem is lack of documentation. There are some pages written on the project&#8217;s CodePlex page and I also stumbled on a <a href="http://blog.benhall.me.uk/2008/02/project-white-automated-ui-testing.html">few</a> <a href="http://blogs.msdn.com/john_daddamio/archive/2008/04/04/testing-wpf-applications-with-the-white-ui-test-framework.aspx">posts</a> which have some more examples on how to use it, but in general the API is undocumented. NDoc documentation that comes in the latest release just contains the API reference, without any human-generated documentation. It even states that it&#8217;s based on .NET Framework 1.1, which was obviously autogenerated and is probably wrong, since the API includes generics. I haven&#8217;t checked the source code directly though, maybe there is something there.</p>

<p>Yes I know, it&#8217;s an open source project and I shouldn&#8217;t expect too much, but I still think that such a project for which the whole purpose is to be used through an API should contain at least some documentation. Otherwise you spend a lot of time experimenting with the API to achieve the results.</p>

<h3></h3>

<h4>Keyboard Problems</h4>

<p>Like most other UI testing frameworks, White too suffers from quirks. I wanted to start the &#8220;Open Project&#8221; menu action using the keyboard shortcut, so I tried with:</p>

<div class="dean_ch" style="white-space: wrap;">mainWindow.<span class="me1">Keyboard</span>.<span class="me1">HoldKey</span> <span class="br0">&#40;</span>KeyboardInput.<span class="me1">SpecialKeys</span>.<span class="me1">CONTROL</span><span class="br0">&#41;</span>;<br />
mainWindow.<span class="me1">Keyboard</span>.<span class="me1">Enter</span> <span class="br0">&#40;</span><span class="st0">&quot;o&quot;</span><span class="br0">&#41;</span>;</div>

<p>Which ended up forcing me to restart the computer, since the OS started acting very funny, as if the Ctrl key was still being pressed. <a href="http://www.codeplex.com/white/Thread/View.aspx?ThreadId=25536">I guess I should have added</a></p>

<div class="dean_ch" style="white-space: wrap;">mainWindow.<span class="me1">Keyboard</span>.<span class="me1">LeaveKey</span> <span class="br0">&#40;</span>KeyboardInput.<span class="me1">SpecialKeys</span>.<span class="me1">CONTROL</span><span class="br0">&#41;</span>;</div>

<p>to unpress the Ctrl key, but I&#8217;m not sure, since I couldn&#8217;t find any documentation on using the keyboard in White. Anyway, a helper method that would accept a shortcut as a string (something like <strong>KeyboardInput.Press (&#8220;Ctrl+O&#8221;)</strong> would be very helpful).</p>

<h4>Clicking The Menus</h4>

<p>After abandoning the keyboard approach, I tried to start &#8220;Open Project&#8221; by clicking on the menu item. I did some experimenting and finally managed to write a basic test, here&#8217;s the whole test fixture:</p>

<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp;<span class="br0">&#91;</span>TestFixture<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">class</span> BasicTests<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">void</span> OpenProject<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Menu fileMenu = mainWindow.<span class="me1">MenuBar</span>.<span class="me1">MenuItem</span> <span class="br0">&#40;</span><span class="st0">&quot;File&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileMenu.<span class="me1">Click</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Menu openProjectMenu = fileMenu.<span class="me1">SubMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;Open Project&#8230;&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openProjectMenu.<span class="me1">Click</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>SetUp<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">void</span> Setup<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application = Application.<span class="me1">Launch</span> <span class="br0">&#40;</span><span class="st0">@&#8221;..\..\..\Kosmos.Gui\bin\Debug\Kosmos.Gui.exe&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainWindow = application.<span class="me1">GetWindow</span> <span class="br0">&#40;</span><span class="st0">&quot;Kosmos GUI&quot;</span>, InitializeOption.<span class="me1">NoCache</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsNotNull</span><span class="br0">&#40;</span>mainWindow<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsTrue</span> <span class="br0">&#40;</span>mainWindow.<span class="me1">DisplayState</span> == DisplayState.<span class="me1">Restored</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>TearDown<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">void</span> Teardown<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application.<span class="me1">Kill</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> Application application;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> Window mainWindow;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></div>

<h4>White Facade</h4>

<p>From my experience with other UI testing frameworks, if you intend to do more than a few simple UI tests it is a good idea to create some sort of a facade around the testing API. This way you simplify the interface to common use cases you typically use and eliminate any nasty quirks (like memory or resource leaks). <a href="http://weblogs.asp.net/bsimser/archive/2008/02/21/automated-ui-testing-with-project-white.aspx">Bil Simser has done something to that affect</a> for White, although I don&#8217;t support his views on (not) using Setup and Teardown methods in unit tests.</p>

<p>Even better: why not implement a fluent inteface facade? Something like</p>

<div class="dean_ch" style="white-space: wrap;">WhiteFacade.<span class="me1">Run</span><span class="br0">&#40;</span>kosmosGuiPath<span class="br0">&#41;</span>.<span class="me1">Press</span> <span class="br0">&#40;</span><span class="st0">&quot;Ctrl+O&quot;</span><span class="br0">&#41;</span></div>

<p>or</p>

<div class="dean_ch" style="white-space: wrap;">WhiteFacade.<span class="me1">Run</span><span class="br0">&#40;</span>kosmosGuiPath<span class="br0">&#41;</span>.<span class="me1">ClickMenu</span> <span class="br0">&#40;</span><span class="st0">&quot;File&quot;</span>, <span class="st0">&quot;Open Project&#8230;&quot;</span><span class="br0">&#41;</span></div>

<p>would be much nicer than the above code, don&#8217;t you think? Okey, I&#8217;m oversimplifying, I know <img src='http://igorbrejc.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but I will try to do something in the direction of a fluent interface facade, since I don&#8217;t intend to abandon the White just yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/testing/using-white-to-test-kosmos-gui/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
	</item>
	</channel>
</rss>

