Automating Gmail with Watir
This document shows how to script a simple Gmail session using Watir.
It was created to provide an example of using Watir to automate a fairly complex (yet publicly accessible) web app.
Environment
The example code was tested using the following environment:
- IE7 on Windows XP SP3
- Ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
- Watir gem version: 1.5.6.1263
Notes on using the IE Developer Toolbar
When scripting an application like Gmail, the easiest way to get started is to use a tool like the Internet Explorer Developer Toolbar to explore the application, and discover how best to interact with its elements. Examining the raw HTML tends to be a less efficient strategy with an application of Gmail's complexity.
The easiest way to find the IE Dev Toolbar is to simply google the string "ie dev toolbar". (I'm leaving the current download URL out of this document since the download URL on microsoft.com is likely to change at some point.)
After installation, you should have a button in your IE command bar that will display or hide the dev toolbar.
To examine an element, enable the toolbar, then click 'Find->Select Element by Click', and click on the target element. You can then check the toolbar's tree view to discover the attributes of the element, as well as the attributes of any elements that contain it.
Example code
The script below assumes you've previously logged in to gmail using IE, and saved your credentials. This avoids any need to hardcode a username and password in the script.
This script will:
- Start IE
- Browse to Gmail's URL and load the main page
- Click the 'Compose Mail' link
- Compose an email to the current user
- Send the mail
- Navigate to sent mail and assert that the previous step's mail subject is present in the frame
- Navigate back to the inbox
- Close the browser