Ok you got me, I’ve been slacking! I missed last week because I took delivery of my new bike a Revolution Courier since then I’ve spent all my spare time tearing around on that. Anyhoo back to work. Last time we got our environment all setup so this time we going to write a Ruby/Watir script which talks to the portal.
OUR FIRST SCRIPT
This script will:
- Reference the Watir library
- Open Internet Explorer
- Goto the portal login page
- Enter a username and password
I think that’s quite enough for our first script so here it goes. Using your favorite editor create a file called c:\watir\scripts\myfirstscript.rb with the following content:
ie = Watir::IE.new
Now lets just break this script down and explain what is going on.
REFERENCE THE WATIR LIBRARY
We first need to pull in the Watir library which will allow us to interact with Internet Explorer:
The require keyword is simply Ruby’s way of referencing an external library.
OPEN INTERNET EXPLORER
Next we create a variable ie which we’ll use to reference a object allowing us to send commands to Internet explorer.
ie = Watir::IE.new
GOTO THE PORTAL LOGIN PAGE
Next call the method goto passing a URL which Internet Explorer will open.
Obviously you need to use your own portal url here.
ENTER A USERNAME AND PASSWORD
We now use the text_field method to fill in a username and password.
Notice we are passing two parameters to the text_field method, a :id symbol and a string. The :id symbol is the html attribute id and the string is the value of the id attribute. In plain english the first text_field line:
Is actually saying – find the text field with an id attribute of ‘logonuidfield’ and set it’s value to ‘myuserid’. Again you will need to use a real user id and password for your portal.
Finally we click the Logon button.
Here we are doing the same as the text_field method, we are passing two parameters to the button method which are used to identify and click the Login button.
Let’s run the script and see what happens. Open a DOS window and type the following commands:
As if by magic you should now see an Internet Explorer window open with your portal login page showing. The User ID and Password fields will turn yellow as they are filled with the values from your script. Finally the Log on button will turn yellow as it is clicked. You should now be logged into your portal!
COMING UP IN PART FOUR
Ok that enough for this post next time we’ll integrate our script with the Ruby unit testing framework, the key to running hundreds of test scripts from one simple command.