<?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>Andrey Shchekin &#187; WatiN</title>
	<atom:link href="http://blog.ashmind.com/index.php/category/watin/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ashmind.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 18:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Evaluating Javascript in WatiN</title>
		<link>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/</link>
		<comments>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 19:52:03 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Solutions]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/09/05/evaluating-javascript-in-watin/</guid>
		<description><![CDATA[The WatiN framework is quite cool, but it lacks two important things.First one is searching by CSS selectors, or, at least, classes.Find.ByCustom(&#8220;className&#8221;, &#8220;X&#8221;) is way too ugly. Or am I missing something? The second (more important) one is a weak access to Javascript.First thing I wanted to do with WatiN was to change something and [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://watin.sourceforge.net/">WatiN</a> framework is quite cool, but it lacks two important things.<br />First one is searching by CSS selectors, or, at least, classes.<br />Find.ByCustom(&#8220;className&#8221;, &#8220;X&#8221;) is way too ugly. Or am I missing something? </p>
<p>The second (more important) one is a weak access to Javascript.<br />First thing I wanted to do with WatiN was to change something and then check some script state.<br />And getting some values from script was not obvious.</p>
<p>I didn&#8217;t want to use <a href="http://www.ayende.com/Blog/archive/2007/04/03/Eval-with-WatiN.aspx">Ayende&#8217;s evil hack</a> (no harm intentended, it gets the work done) — putting javascript state into DOM is not pretty and too string oriented.<br />I thought that browser COM interfaces should definitely have a way to get Javascript <em>objects</em> outside, that is just the way MS/COM people think.<br />Thanks to <a href="http://www.ayende.com/Blog/archive/2007/04/03/Eval-with-WatiN.aspx#Comments_ascx_CommentList_ctl04_NameLink">Jeff Brown&#8217;s comment</a> for explaining last obstacles.</p>
<p>So here goes the code.<br />It is quite basic, but it allows you to get value of any Javascript evaluation.<br />As you can see, I hadn&#8217;t included any error handling, I had no time to look into it. </p>
<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> JS {
        <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">object</span> Eval(Document document, <span class="kwrd">string</span> code) {
            IExpando window = JS.GetWindow(document);
            PropertyInfo property = JS.GetOrCreateProperty(window, <span class="str">"__lastEvalResult"</span>);

            document.RunScript(<span class="str">"window.__lastEvalResult = "</span> + code + <span class="str">";"</span>);

            <span class="kwrd">return</span> property.GetValue(window, <span class="kwrd">null</span>);
        }

        <span class="kwrd">private</span> <span class="kwrd">static</span> PropertyInfo GetOrCreateProperty(IExpando expando, <span class="kwrd">string</span> name) {
            PropertyInfo property = expando.GetProperty(name, BindingFlags.Instance);
            <span class="kwrd">if</span> (property == <span class="kwrd">null</span>)
                property = expando.AddProperty(name);

            <span class="kwrd">return</span> property;
        }

        <span class="kwrd">private</span> <span class="kwrd">static</span> IExpando GetWindow(Document document) {
            <span class="kwrd">return</span> document.HtmlDocument.parentWindow <span class="kwrd">as</span> IExpando;
        }
    }</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p style="font-size: 90%">Nitpicking:<br />
By the way, <a href="http://www.ayende.com/Blog">Ayende</a>, getting permalinks to comments in your blog is not obvious (I used View Source).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
