<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>Bob&apos;s Blog - ColdFusion Tidbits - Selenium</title>
			<link>http://www.silverwareconsulting.com/index.cfm</link>
			<description>Some stuff about ColdFusion and Transfer</description>
			<language>en-us</language>
			<pubDate>Tue, 07 Sep 2010 06:38:58 -0400</pubDate>
			<lastBuildDate>Mon, 07 Jun 2010 17:54:00 -0400</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>bob.silverberg@gmail.com</managingEditor>
			<webMaster>bob.silverberg@gmail.com</webMaster>
			
			<item>
				<title>Checking for JavaScript Errors with Selenium</title>
				<link>http://www.silverwareconsulting.com/index.cfm/2010/6/7/Checking-for-JavaScript-Errors-with-Selenium</link>
				<description>
				
				I&apos;ve recently been writing some end-to-end tests for &lt;a href=&quot;http://www.validatethis.org&quot; target=&quot;_blank&quot;&gt;ValidateThis&lt;/a&gt;, 
my validation framework for ColdFusion objects, using &lt;a href=&quot;http://seleniumhq.org/&quot; target=&quot;_blank&quot;&gt;Selenium&lt;/a&gt;. One of the things I wanted to test
were the client-side validations that the framework generates automatically. Selenium makes it very easy to test these, as I can use Selenium&apos;s
&lt;em&gt;assertText&lt;/em&gt; command to locate failure messages that have been generated by the &lt;a href=&quot;http://bassistance.de/jquery-plugins/jquery-plugin-validation/&quot; target=&quot;_blank&quot;&gt;jQuery Validation plugin&lt;/a&gt;,
using XPath, and check whether the message is what I expected.&lt;/p&gt;
&lt;p&gt;I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors),
so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that
there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I&apos;m going to describe one of them 
here. Let me state again, for the record, that this is pretty hacky. I&apos;d love to hear from others who may have better solutions.&lt;/p&gt;
&lt;p&gt;I simply add a script to my page that will catch any JS errors by intercepting the &lt;em&gt;window.onerror&lt;/em&gt; event:
&lt;code&gt;&lt;script type=&quot;text/javascript&quot;&gt;
	window.onerror=function(msg){
		$(&quot;body&quot;).attr(&quot;JSError&quot;,msg);
	}
&lt;/script&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This will cause an attribute called &lt;em&gt;JSError&lt;/em&gt; with a value corresponding to the JavaScript error message to be added to the &lt;em&gt;body&lt;/em&gt; tag of my document if a JavaScript 
error occurs. Note that I&apos;m using jQuery to do this, so this specific example won&apos;t work if jQuery fails to load. 
Then, in my Selenium test, I just use the command &lt;em&gt;assertElementNotPresent&lt;/em&gt; with a target of &lt;em&gt;//body[@JSError]&lt;/em&gt;.
Now, if any JavaScript errors occur on the page my test will fail and I&apos;ll know I have to address them first. If, for some strange reason, I want to
check for a particular JavaScript error, I could use the &lt;em&gt;assertElementPresent&lt;/em&gt; command with a target of &lt;em&gt;//body[@JSError=&apos;the error message&apos;]&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Note that I&apos;m using this with a test fixture page, it&apos;s not an actual page that is part of an application. I&apos;m not sure that this would be very
useful in an automated testing environment, I&apos;m just using it for some TDDing, where I want to write an end-to-end acceptance test first, and
then write my unit tests. Getting the test to fail because of a JavaScript error is a nice way, imo, of satisfying the criteria that I must have a 
failing test before writing code to make it work. The usefullness of this technique in terms of pure testing is questionable, but I find it useful 
for my purposes, so I thought I&apos;d put it out there for others.&lt;/p&gt; 
				</description>
				
				<category>TDD</category>				
				
				<category>Selenium</category>				
				
				<pubDate>Mon, 07 Jun 2010 17:54:00 -0400</pubDate>
				<guid>http://www.silverwareconsulting.com/index.cfm/2010/6/7/Checking-for-JavaScript-Errors-with-Selenium</guid>
				
			</item>
			</channel></rss>