Dashboard > Watir > ... > Contributions > Automating Gmail with Watir
Watir Log In View a printable version of the current page.
Automating Gmail with Watir
Added by Bill Agee, last edited by Bill Agee on Jul 17, 2008  (view change)
Labels: 
(None)

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
require 'test/unit'
require 'watir'

class GmailDemo < Test::Unit::TestCase
  def test_gmaildemo
    ie = Watir::IE.new
    ie.goto('mail.google.com')

    # We need the iframe with ID 'canvas_frame'.  Store it in a var.
    canvas_frame = ie.frame(:id, 'canvas_frame')

    # Get the current user's email address.  We'll be sending email to it.
    my_address = canvas_frame.div(:id, 'guser').text.slice(/^.*@gmail\.com/)
    mail_subject = 'Hello Watir world!'
    mail_body_text = 'Hi.'

    canvas_frame.span(:text, 'Compose Mail').click # Compose new mail
    canvas_frame.text_field(:name, 'to').set(my_address)
    canvas_frame.text_field(:name, 'subject').set(mail_subject)
    # The mail body field is contained in its own iframe, which only contains
    # a body element.  So, save the iframe in a var, then set the innerText
    # property of the iframe's body element.
    mail_body_frame = canvas_frame.frame(:index, 1)
    # TODO: there must be a friendlier way to do this, while still avoiding
    # hardcoding of any unfriendly identifiers. Does Watir need a body method?
    mail_body_frame.document.body.setproperty('innerText', mail_body_text)

    canvas_frame.button(:text, 'Send').click    # Send the message
    canvas_frame.link(:text, 'Sent Mail').click # Browse to sent mail page
    assert(canvas_frame.contains_text('Hello Watir world!'))
    canvas_frame.link(:text, /Inbox/).click     # Return to the inbox page
    ie.close
  end
end

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators