<?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; WebDesign</title>
	<atom:link href="http://huddledmasses.org/tag/webdesign/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<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>

