Scott Hanselman

WATIR for .NET - WatiN approaches 0.8 release and automating IE from PowerShell

August 18, '06 Comments [7] Posted in ASP.NET | PowerShell | Ruby | Watir
Sponsored By

I'm a big fan of WATIR (pronounced "Water"). However many folks have said they wish they could have a similar tool in .NET. WatiN may be that tool. Looks like they'll be releasing 0.8.0 around September 1st with these changes:

  • An even more Watir like syntax to access an element. There's no need anymore to use MainDocument to access, for example, a TextField. You will now write code like:
    ie.TextField("textfieldid).Text 
  •  Support for finding matching element attribute values by using regular expressions.
  • Out of the box support for finding images by their Src attribute (no need to create your own AttributeValue class for this anymore).
  • Updated documentation.

Looks like a pretty nice, pretty clean implementation of an IE abstraction layer for .NET that will be getting even cleaner very soon. 

Aside: Another great .NET-based IE Automator is Alex Furman's SWExplorerAutomation. Here's an example of integrating SWEA with NUnit.

Back on the Watir side, one of the coolest things about Watir is "putting IE on a string" from the Interactive RuBy shell, or IRB. Leon has a great 3 mins to Watir tutorial that uses the IRB with Watir to interactively poke around at a site. Many folks find this more useful and interesting than using a recorder tool.

For example, assuming you've installed Ruby with the Windows Ruby Installer, open up a cmd.exe prompt and run

gem install Watir

to get Watir. Then run IRB.exe (Interactive Ruby) and do this:

irb(main):001:0> require 'watir'
irb(main):002:0> include Watir
irb(main):003:0> ie = Watir::IE.new
irb(main):004:0> ie.goto("
http://google.com")
irb(main):005:0> ie.text_field(:name, "q").set("Scott Hanselman")
irb(main):006:0> ie.button(:name, "btnG").click

And that's interactive Watir, right? Yay.

So, if I take a look at this WatiN (pronounced WHAT'n as in What'n'tha'heck maybe?) using the only interactive .NET Shell I have available to me...could I do Watir/IRB style interactive work using PowerShell?

PS[1] C:\WatiN-0.7.0.4000\bin
> [System.Reflection.Assembly]::LoadFile((get-item WatiN.Core.dll).FullName)

GAC    Version        Location
---    -------        --------
False  v1.1.4322      C:\WatiN-0.7.0...

PS[2] C:\WatiN-0.7.0.4000\bin
> $ie = new-object WatiN.Core.IE("
http://www.google.com")
New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load fi
le or assembly 'Interop.SHDocVw, Version=1.1.0.0, Culture=neutral, PublicKeyTok
en=null' or one of its dependencies. The system cannot find the file specified.
"
At line:1 char:17
+ $ie = new-object  <<<< WatiN.Core.IE("
http://www.google.com")

Looks like since he's using an unsigned IE interop assembly it can't load it from the current application directory. That's c:/Program Files/Windows PowerShell/v1.0/, not the current directory. At this point I have two choices. I can copy the unsigned intern assembly Interop.SHDocVw.dll to the PowerShell folder, or I can rebuild the project against a Signed Primary Interop Assembly for IE. I'll copy it over for now.

PS[1] C:\WatiN-0.7.0.4000\bin
> [System.Reflection.Assembly]::LoadFile((get-item WatiN.Core.dll).FullName)

GAC    Version        Location
---    -------        --------
False  v1.1.4322      C:\WatiN-0.7.0...

PS[2] C:\WatiN-0.7.0.4000\bin
> $ie = new-object WatiN.Core.IE("
http://www.google.com")
New-Object : Exception calling ".ctor" with "1" argument(s): "Specified cast is
 not valid."
At line:1 char:17
+ $ie = new-object  <<<< WatiN.Core.IE("
http://www.google.com")

Yikes. Looks like folks are pissed off; so pissed off this forcibly closed PowerShell also.

Ok, so WatiN is doing something tricky that PowerShell doesn't like. Well, since PowerShell has its own concept of how .NET and COM should interact. Let's cut out the middle man (WatiN in this case) and go direct to IE from PowerShell .

PS[1]>$psie = new-object -com InternetExplorer.Application
PS[2]>$psie.Navigate("
http://www.google.com")
PS[3]>$q = ($psie.Document.GetElementsByTagname("input") | where { $_.Name -eq "q" } #get the input box called "q"
PS[4]>$q.value = "Scott Hanselman"
PS[5]>$search = $psie.Document.GetElementsByTagname("input") | where { $_.Name -eq "btnG" }
PS[6]>$search.click()

Gross, not easy, but shows potential. You know, a weekend, some UpdateType-Data action and a few functions and one could get a WatiPSH (!?) prototype running that would be very PowerShelly...I wonder if the PowerShell type extension stuff works as nicely with COM objects...

If the WatiN team considers PowerShell in their use cases, their abstraction layer might make a suitable interface for PowerShell scripting. Or, perhaps a few well written PowerShell scripts could give Watir-like syntax on PowerShell. Either way, WatiN has the potential to be pretty useful, particularly within NUnit if you find the Watir NUnit Integration distasteful (some do, I don't, I started it).

For now, I'll stick with the simplicity and IJW of Watir, but I'm keeping my eye on WatiN.

Now playing: Stephen Lynch - Superhero

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

New Release of WatirMaker now WatirRecorder

July 25, '06 Comments [5] Posted in Ruby | Watir
Sponsored By

Watirrecorder1Rutger Smit has given the original WatirMaker an overhaul and we're re-releasing it as WatirRecorder++. Here's the original screencast if you want to see what it does. A roadmap will follow soon. Thanks Rutger!

This version compiles and runs under 1.1, but it upgrades smoothly to 2.0, so we'll probably do that one day. We're in the process of setting up an Open Source BSD Project at CodePlex.com so if you've made changes yourself, or you want to join up, now's the time. We'll get the source uploaded as soon as the permissions are setup. If you become a developer or want the source, you'll be able to interact with the Team System source control system using a number of clients. Be sure to read the manual.

UPDATE: We had some trouble getting everyone access on CodePlex so we're putting WatirMaker/WatirRecorder up on OpenQA.org, where Watir itself is hosted.

You can check out the source anonymously using the Subversion URL http://svn.openqa.org/svn/watir-recorder/trunk You can get the source by doing the following:

svn co https://svn.openqa.org/svn/watir-recorder/trunk

If you've never installed Watir, here's the easiest possible thing to do:

  • Install the Ruby One-Click Installer.
  • Open cmd.exe and type "gem install watir"
  • Install WatirRecorder++, hit Start, do some stuff (do NOT use the keyboard to submit forms). Hit stop, and playback.
  • Now, STOP everything and remind yourself that this is a jump-starter, that's it.

Enjoy.

File Attachment: WatirRecorder_Setup_Lite.msi (2858 KB)

As an aside, keep an eye out for FireWatir. It's tricky to install and requires a number of bleeding edge things, but it's very shiny.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Time Saver - Using Watir as a Startup Program in your ASP.NET Projects

July 20, '06 Comments [10] Posted in ASP.NET | Ruby | Watir | Bugs
Sponsored By

Often when you're inching forward within an ASP.NET project you'll find yourself repeating actions over and over again to get to a certain page, often three or four actions in to the application. As someone who hates repetitive actions this is what I do.

(These examples are in VS2005, but will work in VS2003, although the property dialogs have changed)

I use Watir to automate the clicks that will get me to where I'm going and set my Watir script to startup when I press F5 to start my project debugging.

Aspnetrubystart1

In this example, I have a script called justsignon.rb that signs on, visits the customer's accounts page, then goes to their Account History page. It's that page that I am currently debugging so I want to automatically show up there in a certain state when I start debugging.

require 'watir'

include Watir

require 'test/unit'

class WatirMakerRecorded < Test::Unit::TestCase

    def test_recorded

        ie = IE.new

        ie.goto('http://localhost:4970/MobileDemo/default.aspx')

 

        ie.text_field(:name, 'userTextBox').set('testuser1')

        ie.text_field(:name, 'passwordTextBox').set('123456')

        ie.button(:name, 'signInCommand').click

        ie.link(:text, /Hanselman/).click

        ie.link(:text, "Account History").click

        #UPDATE - the debugger will detach if the spawned process ends, so wait for ENTER

        gets

    end

end

Aspnetrubystart2

As I move forward in my development process I use different scripts to get me to different states.  This easily adds up to as much as 15 minutes to a half hour of rote "monkey clicking" that is usually wasted time.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

WatirMaker written again in Ruby

July 7, '06 Comments [1] Posted in ASP.NET | Ruby | Watir
Sponsored By

WatirmakerrubyI always thought WatirMaker was a pretty good idea. It was meant not as a recorder, per se, but rather as a "faster typer."

I use it to jump start spikes like my recent Vonage script. If I wrote Watir often enough I'd just use the Ruby interactive shell.

(By the way, if you  have 15 minutes - maybe it's lunch - visit here: http://tryruby.hobix.com/ and try Ruby out, guilt- and install-free, in your browser.)

After I did WatirMaker in C#, Michael Kelly and John Hann wrote it again in native Ruby with the tiniest bit of help from me early on. It's rockin' sweet IMHO.

You can run it by simply running "ruby watirmaker.rb" from the command-line or by redirecting to a file "ruby watirmaker.rb > myscript.rb."

John emailed me and said that he and Michael are going to look for a permanent home for this, but until then it's here.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Sapphire In Steel - Ruby within Visual Studio 2005

July 2, '06 Comments [0] Posted in PowerShell | Ruby | Watir | Bugs
Sponsored By

Steel_ideWell, apparently I'm the last to notice this one (likely because I've been up to my ears in PowerShell, not Ruby) but "SapphireInSteel" is a Visual Studio 2005 Project add-on that adds Ruby *editing* support to Visual Studio.

This has nothing to do with turning Ruby into IL or making Ruby a .NET Language (although those projects exist.) This is about editing and debugging Ruby within Visual Studio 2005.

If you haven't played with Ruby, take a moment and do this:

This'll be great for everyone who does both Ruby and .NET, but especially for those doing .NET plus Watir development. This is apparently a very actively development project so subscribe to their blog.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Page 1 of 5 in the Watir category Next Page

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.