<?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>Huddled Masses &#187; Testing</title>
	<atom:link href="http://huddledmasses.org/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Did you know PowerShell can use Selenium?</title>
		<link>http://huddledmasses.org/did-you-know-powershell-can-use-selenium/</link>
		<comments>http://huddledmasses.org/did-you-know-powershell-can-use-selenium/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 05:46:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1789</guid>
		<description><![CDATA[This is sort-of a place-holder for a full-length post that I really ought to write about driving web testing from PowerShell using Selenium. I actually have a little module around for doing that with WaTiN, but honestly the Selenium project seems to be a lot more active, and has quite a bit of muscle behind [...]]]></description>
			<content:encoded><![CDATA[	<p>This is sort-of a place-holder for a full-length post that I really ought to write about driving web testing from PowerShell using Selenium.  I actually have a little module around for doing that with WaTiN, but honestly the Selenium project seems to be a lot more active, and has quite a bit of muscle behind it since they&#8217;ve merged with WebDriver&#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-path</span> ~\Downloads\selenium<span style="color: #66cc66;">-</span>dotnet<span style="color: #66cc66;">-</span>2.16.0\net40\WebDriver.<span style="color: #003366;">dll</span> <br />
<br />
<span style="color: #666666; font-style: italic;"># Navigate to google in IE (or Firefox, Chrome, Opera, etc)</span><br />
<span style="color: #660033; font-weight: bold;">$driver</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> OpenQA.<span style="color: #003366;">Selenium</span>.<span style="color: #003366;">IE</span>.<span style="color: #003366;">InternetExplorerDriver</span> <br />
<span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">Url</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://google.com&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Type PowerShell into the query box, the page will update via AJAX</span><br />
<span style="color: #666666; font-style: italic;"># Note we won't hit enter or anything</span><br />
<span style="color: #660033; font-weight: bold;">$q</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">FindElementByname</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;q&quot;</span><span style="color: #333;">&#41;</span> <br />
<span style="color: #660033; font-weight: bold;">$q</span>.<span style="color: #003366;">SendKeys</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PowerShell&quot;</span><span style="color: #333;">&#41;</span> <br />
<br />
<span style="color: #666666; font-style: italic;"># Use a CSS selector to find the first result link and click it</span><br />
<span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">FindElementByCssSelector</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;li.g h3.r a&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<h3>One Catch</h3>

	<p><a href="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/ProtectedMode.png" style="float:right; padding-left: 1em; "><img src="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/ProtectedMode.png" alt="The Security tab of the Internet Options dialog" title="ProtectedMode" width="423" height="541" class="size-full wp-image-1796" /></a>If you try this with IE and you get the error <em>Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones</em> ... it means exactly what it says.  You need to open &#8220;Internet Options&#8221; from your start menu (or from IE), and go through each &#8220;zone&#8221; and set the &#8220;Enabled Protected Mode&#8221; check box to the same value for each zone (either all checked, obviously the most secure, or all unchecked).  I&#8217;m not going to debate whether setting them all <em>unprotected</em> is a good idea &#8230; I set mine to all protected, but I don&#8217;t generally use IE anyway. </p>

	<p>If you want more help, Selenium&#8217;s documentation is great, and there&#8217;s a section on <a href="http://seleniumhq.org/docs/03_webdriver.html#getting-started-with-selenium-webdriver">Getting Started with Selenium WebDriver</a> which I found quite helpful (make sure your examples are in &#8220;csharp&#8221; and you can almost just copy and paste &#8212; someone should offer to do them in PowerShell).  </p>

	<p>If you want more information about the Internet Explorer driver and this problem in particular, the short answer is that &#8220;Protected Mode&#8221; is a security boundry, so if you cross over it the <span class="caps">COM</span> automation object doesn&#8217;t work &#8212; thus, you need to make sure you always stay on the same side. There&#8217;s a good discussion <a href="http://code.google.com/p/selenium/issues/detail?id=1795">on the mailing list archive</a> about how it works and why, as well a weird alternative <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html#INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS">documented on the Selenium JavaDocs</a></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/did-you-know-powershell-can-use-selenium/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Arrange &#8211; Act &#8211; Assert: Intuitive Testing</title>
		<link>http://huddledmasses.org/arrange-act-assert-intuitive-testing/</link>
		<comments>http://huddledmasses.org/arrange-act-assert-intuitive-testing/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 03:31:56 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[3As]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1757</guid>
		<description><![CDATA[Today I have a new module to introduce you to. It&#8217;s a relatively simple module for testing, and you can pick it up in short order and start testing your scripts, modules, and even compiled .Net code. If you put it together with WASP you can pretty much test anything The basis for the module [...]]]></description>
			<content:encoded><![CDATA[	<p>Today I have a new module to introduce you to. It&#8217;s a relatively simple module for testing, and you can pick it up in short order and start testing your scripts, modules, and even compiled .Net code. If you put it together with <a href="http://poshcode.org/2483" title="my UI Automation module">WASP</a> you can pretty much test anything  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>The basis for the module is the arrange-act-assert model of testing. First we <strong>arrange</strong> the things we&#8217;re going to test: set up data structures or whatever you need for testing. Then we <strong>act</strong> on them: we perform the actual test steps. Finally, we <strong>assert</strong> the expected output of the test. Normally, the expectation is that during the assert step we&#8217;ll return <code>$false</code> if the test failed, and that&#8217;s all there is to it. Of course, there&#8217;s plenty more to testing, but lets move on to my new module.</p>

	<p>The module is called <a href="/downloads/PSaint.7z">PSaint</a> (pronounced &#8220;saint&#8221;), and it stands, loosely, for PowerShell Arrange-Act-Assert in testing. Of course, what it stands for isn&#8217;t important, just remember the name is PSaint  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<p>PSaint is really a very simple module, with only a few functions. There are two major functions which we&#8217;ll discuss in detail: Test-Code and New-RhinoMock, and then a few helpers which you may or may not even use:</p>

	<h3>Set-TestFilter </h3>

	<p>Sets filters (include and/or exclude) for the tests by name or category.</p>

	<h3>Set-TestSetup (alias &#8220;Setup&#8221;)</h3>

	<p>Sets the test setup ScriptBlock which will be run before each test.</p>

	<h3>Set-TestTeardown (alias &#8220;Teardown&#8221;)</h3>

	<p>Sets the test teardown ScriptBlock which will be run after each test.</p>

	<h3>Assert-That</h3>

	<p>Assserts something about an object (or the output of a scriptblock) and throws if that assertion is false.  This function supports asserting that an exception should be thrown, or that a test is false &#8230; and supports customizing the error message as well.</p>

	<h3>Assert-PropertyEqual</h3>

	<p>This is a wrapper around Compare-Object to compare the properties of two objects.</p>

	<h2>How to test with PSaint: Test-Code</h2>

	<p>Test-Code (alias &#8220;Test&#8221;) is the main driver of functionality in PSaint, and you use it to define the tests that you want to run.  Let&#8217;s jump to an example or two so you can see the usefulness of this module.</p>

	<p>Let&#8217;s start with an extremely simple function that we want to write: New-Guid.  We want a function that generates a valid random <span class="caps">GUID</span> as a string. We&#8217;ll start by writing a couple of tests. First we&#8217;ll test that the output of the function is a valid <span class="caps">GUID</span>.</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Now, to verify that the test works, you should define this function (the GUID-looking thing is one letter short) and then run that test:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Another proof that it works would be that it should fail on this function too, because &#8220;x&#8221; is not a valid character in a Guid:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So, let&#8217;s write a minimal New-Guid that actually generates a valid Guid:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>If you run our test on that, you will see:</p>

	<div class="text code text" style="font-family:monospace;">&nbsp; &nbsp;Result: Pass<br />
<br />
Result Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Category<br />
------ ---- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--------<br />
Pass &nbsp; New-Guid outputs a Guid<br />
&nbsp;</div>

	<p>If you don&#8217;t like the fact that the Category is empty, you could add a category or two to the end of our test.  We should also switch to using Assert-That if we want to know which test in the assert failed. Finally, we want to write another test which would test that New-Guid doesn&#8217;t just return the same Guid every time, the way ours does right now:</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Assert-<span style="font-style: normal;">That</span></span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-FailMessage</span> <span style="color: #009900;">&quot;New-Guid returned a $($guid.GetType().FullName)&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span> &nbsp;<span style="color: #666666; font-style: italic;"># Throws relevant errors already</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, ValidGuid<br />
<br />
test <span style="color: #009900;">&quot;New-Guid outputs different Guids&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;arrange <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># generate a bunch of Guids</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">+=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># compare each guid to all the ones after it</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$j</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Assert-<span style="font-style: normal;">That</span></span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-FailMessage</span> <span style="color: #009900;">&quot;There were equal Guids: $($guids[$i])&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, RandomGuids<br />
&nbsp;</div>

	<p>Now, we have to actually fix our New-Guid function to generate real random Guids:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Guid</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">NewGuid</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>And at that point, we should have a function, and a couple of tests that verify it&#8217;s functionality&#8230;</p>

	<h3>The finer points of assertions</h3>

	<p>One thing you&#8217;ll notice the first time you use Get-Member after loading the PSaint module is a few script properties have been added to everything.  I did this because I found myself writing the same Assert-That calls over and over and decided that it would be slicker to make these extension methods than to write new functions for each one:</p>

	<div class="posh code posh" style="font-family:monospace;">MustBeA<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Type</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustBeFalse<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustBeTrue<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustEqual<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Object<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustNotEqual<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Object<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>There&#8217;s also a <span style="font-family:monospace;">MustThrow<span style="color: #333;">(</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">[</span><span style="color: #003366; font-weight: bold;">Type</span><span style="color: #333;">]</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">[</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">]</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">)</span></span> which can be used on script blocks (note that this function executes the ScriptBlock immediately, so be careful how you use it).</p>

	<p>We can use these to tidy up our tests quite a bit, while still getting good error messages when tests fail:</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid String&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span>.<span style="color: #003366;">MustBeA</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #666666; font-style: italic;"># Throws relevant errors already</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, ValidGuid<br />
<br />
test <span style="color: #009900;">&quot;New-Guid outputs different Guids&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;arrange <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># generate a bunch of Guids</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">+=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># compare each guid to all the ones after it</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$j</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">MustNotEqual</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, RandomGuids<br />
&nbsp;</div>

	<h3><span class="caps">COM</span> Objects </h3>

	<p>PSaint also has a wrapper for <span class="caps">COM</span> objects to help with testing them. It adds GetProperty and SetProperty methods to allow you to access <span class="caps">COM</span> object properties which don&#8217;t show up on boxed <span class="caps">COM</span> objects (a common problem when working with MSOffice, for instance).  It also adds InvokeMethod for <span class="caps">COM</span> objects to invoke methods that don&#8217;t show up for similar reasons.  These, of course, only help you if you&#8217;re already fairly literate with the <span class="caps">COM</span> object in question.</p>

	<h3>Mock Objects</h3>

	<p>PSaint includes New-RhinoMock, a function for generating a new mock object using <a href="http://hibernatingrhinos.com/open-source/rhino-mocks">RhinoMocks</a> (which is included).  Rhino Mocks is a BSD-licensed dynamic mock object framework for the .Net platform. Its purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.</p>

	<p>I have to admit that this New-RhinoMock function is incomplete, and exposes only a fraction of the options and power in RhinoMocks, but it&#8217;s been sufficient for the few times when I&#8217;ve wanted to actually mock objects from PowerShell, so I&#8217;m including it here.</p>

	<p>For those of you (developers) who want to know why RhinoMocks instead of your favorite mocking framework, the answer is astonishingly simple: it had the least number of necessary generic methods (which are impossible to call in PowerShell 2).</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/arrange-act-assert-intuitive-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using PowerShell and WatiN (PowerWatin?)</title>
		<link>http://huddledmasses.org/using-powershell-and-watin-powerwatin/</link>
		<comments>http://huddledmasses.org/using-powershell-and-watin-powerwatin/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 06:10:50 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[WatiN]]></category>
		<category><![CDATA[WebDesign]]></category>
		<category><![CDATA[WebDevelopment]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1111</guid>
		<description><![CDATA[This is an update to a previous article. Someone asked (on Twitter) about using WatiN from PowerShell, and pointed to this old post by Scott Hanselman saying he was having the same problems &#8230; so I wrote this to help them out: WatiN requires -STA mode Note: WatiN requires Single Threaded Apartment mode, so you [...]]]></description>
			<content:encoded><![CDATA[	<h2>This is an update to a previous article.</h2>

	<p>Someone asked (on Twitter) about using <a href="http://watin.sourceforge.net">WatiN</a> from PowerShell, and pointed to <a href="http://www.hanselman.com/blog/WATIRForNETWatiNApproaches08ReleaseAndAutomatingIEFromPowerShell.aspx">this old post by Scott Hanselman</a> saying he was having the same problems &#8230; so I wrote this to help them out:</p>

	<h5>WatiN requires -<span class="caps">STA</span> mode</h5>

	<p>Note: WatiN requires Single Threaded Apartment mode, so you need to be using PowerShell 2.0 (currently in CTP3) in order for any of this to work, and you need to pass the -<span class="caps">STA</span> parameter to PowerShell. Regardless, I thought I&#8217;d throw two tips out here:</p>

	<h3>Don&#8217;t use <code>LoadFile</code>, use <code>LoadFrom</code></h3>

	<p>I&#8217;m not 100% sure when it&#8217;s appropriate to use <strong>LoadFile</strong> in PowerShell, but I can tell you that if your assembly is in a folder with a bunch of other assemblies upon which it depends &#8230; you need to use <code>[Reflection.Assembly]::LoadFrom( $path )</code> instead &#8212; because the .net loader will be able to find the dependencies.</p>

	<h3>Generate some functions to help yourself out</h3>

	<p>It&#8217;s trivial to do code-generation in PowerShell, and WatiN is not friendly to the PowerShell syntax, so you&#8217;re going to want to generate a bunch of them.  To get you started, here&#8217;s a set of Find-* functions to let you find each type of element that WatiN recognizes and automates&#8230; by name, id, class, style &#8230; well, by any attribute, really:</p>

<span id="more-1111"></span>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Watin</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">## CHANGE this to point to your WatiN.Core.dll</span><br />
<span style="color: #660033; font-weight: bold;">$WatinPath</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Path</span></span> <span style="color: #009900;">&quot;$(Split-Path $Profile)\Libraries\WatiN.Core.dll&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;">## Load the assembly</span><br />
<span style="color: #660033; font-weight: bold;">$global</span>:watin <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$WatinPath</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;">## Create an initial window (I'm creating IE, but WatiN handles Firefox too)</span><br />
<span style="color: #660033; font-weight: bold;">$global</span>:ie <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> WatiN.<span style="color: #003366;">Core</span>.<span style="color: #003366;">IE</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;http://www.google.com&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;">## Generate Find-Button, Find-TextField, etc:</span><br />
<span style="color: #660033; font-weight: bold;">$ie</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-Type</span> Method <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$type</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Definition</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>WatiN.<span style="color: #003366;">Core</span>.<span style="color: #003366;">Element</span><span style="color: #333;">&#93;</span></span>.<span style="color: #003366;">IsAssignableFrom</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">type</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$type</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Item</span></span> <span style="color: #000066;">-Path</span> <span style="color: #009900;">&quot;Function:Find-$($_.Name)&quot;</span> <span style="color: #000066;">-Value</span> $<span style="color: #333;">&#40;</span> <span style="color: #660033;">iex</span> @<span style="color: #009900;">&quot;<br />
{<br />
Param(<span style="color: #000099; font-weight: bold;">`$</span>Attribute, [regex]<span style="color: #000099; font-weight: bold;">`$</span>value)<br />
<span style="color: #000099; font-weight: bold;">`$</span>ie.$($_.Name)( ([Watin.Core.Find]::By( <span style="color: #000099; font-weight: bold;">`$</span>Attribute, <span style="color: #000099; font-weight: bold;">`$</span>value)) )<br />
}<br />
&quot;</span>@ &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>With just that little bit of code, I generated all of these functions:</p>

	<ul>
		<li>Find-Area</li>
		<li>Find-Button</li>
		<li>Find-CheckBox</li>
		<li>Find-Div</li>
		<li>Find-Element</li>
		<li>Find-FileUpload</li>
		<li>Find-Form</li>
		<li>Find-Image</li>
		<li>Find-Label</li>
		<li>Find-Link</li>
		<li>Find-Para</li>
		<li>Find-RadioButton</li>
		<li>Find-SelectList</li>
		<li>Find-Span</li>
		<li>Find-Table</li>
		<li>Find-TableBody</li>
		<li>Find-TableCell</li>
		<li>Find-TableRow</li>
	</ul>
	<ul>
		<li>Find-TextField</li>
	</ul>

	<p>And each function takes the name of an attribute, like say: Alt, Class, For, Id, Index, Name, Src, Style, Text, Title, Url, Value &#8230; or just &#8220;Element&#8221; ... and a regular expression to match against the value of that attribute, and finds the control(s) that match.  So for instance &#8230; since I&#8217;ve got a browser open to Google, I could trigger a search for my name by just doing:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Find-<span style="font-style: normal;">TextField</span></span> Name q <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">TypeText</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;Joel Bennett&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Find-<span style="font-style: normal;">Button</span></span> Name btnG <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Drat, the lawyer and that pottery guy still beat me out, maybe I should stop using &#8220;Jaykul&#8221; everywhere and start using my real name more &#8230;</p>

	<h2> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <em>Edit</em>: Another example</h2>

	<p>Someone asked in the comments about how to parse a table, and I thought I&#8217;d post an example, because it&#8217;s pretty cool how well PowerShell works for stuff like this.  Lets say I wanted to scan down a column in a table, and depending on the values, return a value from another column. For an example, I&#8217;ll take a table off Wikipedia which lists multi-protocol IM clients, and try to return the names of the clients which support <span class="caps">IRC</span>.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Using the function defined earlier ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Watin</span></span><br />
<span style="color: #666666; font-style: italic;">## Now navigate to the page</span><br />
<span style="color: #660033; font-weight: bold;">$ie</span>.<span style="color: #003366;">Goto</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;http://en.wikipedia.org/wiki/Multiprotocol_instant_messaging_application&quot;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;">## Find that first table (yeah, you just have to know the ID):</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Find-<span style="font-style: normal;">Table</span></span> Id sortable_table_id_0<br />
<br />
<span style="color: #666666; font-style: italic;">## we COULD do this to figure out which column is the IRC column:</span><br />
<span style="color: #666666; font-style: italic;">## It's hard because there's no mapping for TH in WatiN</span><br />
<span style="color: #666666; font-style: italic;">## Personally, I think they should show up in .TableCells</span><br />
<span style="color: #666666; font-style: italic;">## -1 for the &quot;th&quot; element that's missing from TableRow[1..n].TableCells</span><br />
<span style="color: #660033; font-weight: bold;">$column</span><span style="color: #66cc66;">=-</span><span style="color: #cc66cc;">1</span>; <br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">TableRows</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Elements</span> <span style="color: #66cc66;">|</span> Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">TagName</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;th&quot;</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;ForEach<span style="color: #66cc66;">-</span>Object<span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-match</span> <span style="color: #009900;">&quot;IRC&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">break</span> <span style="color: #333;">&#125;</span> <span style="color: #660033; font-weight: bold;">$column</span><span style="color: #66cc66;">++</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now we loop all rows, and examine that column </span><br />
<span style="color: #666666; font-style: italic;">## If we find a &quot;Yes&quot;, print out the table header for that row:</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">TableRows</span> <span style="color: #66cc66;">|</span> Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">TableCells</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$column</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-match</span> <span style="color: #009900;">&quot;Yes&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Elements</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;Where<span style="color: #66cc66;">-</span>Object &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">TagName</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;th&quot;</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Text</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Clearly WatiN needs to fix the missin <code>&#60;th&#62;</code> TableHeader problem, but other than that, this wasn&#8217;t too painful. Of course, it would be a lot easier if we could cast ScriptBlocks as the generic <code>Predicate&#60;T&#62;</code> that the various .Find methods accept, but if I try this, it just freezes up and never returns:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">TableRows</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Element</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$arg</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">TagName</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;th&quot;</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span></div>

	<h3>Future Work</h3>

	<p>You could easily take this a lot further: dump those functions into a file for reusing next time (so you don&#8217;t have to regenerate them each time), and in fact, it would be relatively simple to make functions for each of the methods on each of the types we found above, so you would have a <code>Click-Button</code> function, and a <code>TypeText-TextField</code> function which could take those same search parameters (attribute and regular expression) and you could even generate appropriate parameters to take, for example, the text to type.  In fact, you could generate a whole <span class="caps">DSL</span> for yourself &#8230; <a href="http://www.codinginstinct.com/2008/11/browser-automation-dsl-using-mgrammar.html">this guy did it using MGrammar</a> &#8230; but why not PowerShell.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/using-powershell-and-watin-powerwatin/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

