Prerequisites

Getting started

This script will open gedit, enter "hello, world" in the edit window and save it as hello.txt. Check for the existence of this file to confirm that the script has worked correctly.

Recording scripts

Dogtail ships with a script recorder that allows you to quickly record test cases, saving them in dogtail-python format. These will often need to be edited by hand afterwards to craft more useful test scripts but the script recorder is a great way of generating the bulk of the script.

Tip: Give descriptive names to your test scripts, including the name of the application being tested. e.g. gcalctool-adding-test-1.0

Playback

To play back the script you have just recorded, simply click the Play button on the recording GUI. If the playback fails at any stage you should run the test script from the command line to get further information about the failure:

cd udtp
python ./gcalctool-simple-test-1.0

Failures in playback occur quite frequently and are often caused by the test system itself (including the system slow-down it causes) but may also represent a shortcoming in the accessibility support of the application being tested. The former can often be worked around by structuring the test differently or inserting pause statements.

Examples

Next we consider two recording examples, one which works as expected and one which gives an error when played back (on Hardy at time of writing)

The following screen shot shows a recording of the act of changing the view mode of the calculator from Basic to Advanced.

script-recording-view.png

Changing view mode of the calculator

The script records and plays back as expected both in the GUI and on the command line.

In the next example we try adding 1 and 1 with the calculator. It records fine, but fails to play back. In the GUI ('Play' button) it just fails silently while on the command line it produces useful errors we can use to start debugging.

script-recording-add.png

A simple recording of adding 1 and 1

Debugging

TBD: write a more in-depth description on how to debug scripts that fail, including filing bugs against the test system (dogtail) and the application being tested. Also describe work-arounds to get the script running, like inserting timeouts or using keyboard shortcuts.

Modifying recorded scripts

Beyond just getting the recorded script to work you might also wish to modify it to add functionality that is not possible to record. This may include adding loops for stress testing or combining several recorded snippets to produce longer tests. [write more detail on this]

The dogtail API

You may want to read /usr/share/python-support/python-dogtail/dogtail/procedural.py to get a feeling for the API, though it is somewhat tricky Python code.

Some notes on the dogtail API:

Additional notes:

%> python
>>> from dogtail import tree
>>> gcalctool = tree.root.application('gcalctool')
>>> gcacltool.dump()

You'll see something like:

{"gcalctool" application}
 Node roleName='frame' name='Calculator  - Scientific' description=''
  Node roleName='filler' name='' description=''
   Node roleName='menu bar' name='' description=''
    Node roleName='menu' name='Calculator' description='' text='Calculator'
     click
     Node roleName='tear off menu item' name='' description='' text=''
      click
     Node roleName='menu item' name='Quit' description='' text='Quit'
      click
     Node roleName='menu item' name='Empty' description='' text='Empty'
      click
    Node roleName='menu' name='Edit' description='' text='Edit'
     click

Sample program to do a simple Gedit test

Save this as as gedit.py:

# coding=utf-8

# A small sample script for dogtail. Starts Gedit, inserts some text,
# saves it, quits Gedit, and verifies that the saved file is correct.

import os
import shutil
import tempfile

from dogtail.procedural import *

os.environ["LC_ALL"] = "C"

dirname = tempfile.mkdtemp()
filename = os.path.join(dirname, "hello.txt")
string = "hello, world"

run("gedit")

focus.application("gedit")
focus.text()
type(string)

click("Save")
focus.text()
type(filename)
focus.widget(name="Save As…", roleName="dialog")
click.button("Save")
# At this point we have what is technically called a race condition.
# The save might not have happened by the time click.button() returns,
# and so we might be quitting too early. On my desktop machine it is
# fast enough, inside qemu not.

click("Quit")

saved_string = file(filename).read()
if saved_string != string + "\n": # gedit adds a newline to the end, always!
    print "error: bad content saved"
    print repr(saved_string)
else:
    print "ok"

shutil.rmtree(dirname)

Testing/Automation/DogtailTutorial (last edited 2008-08-06 17:01:14 by localhost)