Writing Mochitest Tests
Tips on writing Mochitest tests for Thunderbird.
This document offers some basic tips for writing Mochitest tests. (See also this Mochitest page in the Firefox docs, which is Firefox-centric but may still be useful.)
Adding a New Test
You may be writing a new test in an existing test file, or you may have set up a new test file as described in Adding Tests. Either way, add a new test with the add_task
function:
Helper Functions
Many essential functions live in these files.
EventUtils
and BrowserTestUtils
do not need to be imported as they are already available in Mochitest files. mailTestUtils
requires importing:
This document is a basic introduction. To go further, explore these files (particularly the docstrings of the various functions in them) and look at existing tests.
Assertion Functions
Use the is
and ok
functions to make test assertions. is
compares two values (using JavaScript's ===
equality comparison). ok
asserts that a single value is truthy (in JavaScript's sense of truthy).
The last argument to each is a message printed to the console to identify the assertion. It is optional but is a good practice for more understandable test logs.
Mouse Clicks
Do a single click on a DOM element:
To double-click change the clickCount
from 1 to 2. There is a shorthand for a single click:
If the test is interacting with a window that is not the main one, pass the relevant window as the (optional) third argument:
Keyboard Keys
Type some text with the keyboard, or type a single key, even a non-character one like the tab key:
Some other valid keys for sendKey
include: RETURN, BACK_SPACE, DELETE, HOME, END, UP, DOWN, LEFT, RIGHT, PAGE_UP, PAGE_DOWN, SHIFT, CONTROL, ALT, ESCAPE, F1, F2, etc. (Not an exhaustive list.)
If the test is interacting with a window that is not the main one, pass the relevant window as the (optional) second argument:
Modifier Keys (Ctrl, Alt, Shift)
To press a key along with one or more modifier keys:
Some other options are altKey
, shiftKey
, and ctrlKey
(a non-exhaustive list).
Similar to sendString
and sendKey
, there is an optional third window
argument to use when interacting with a specific window.
Waiting for Events
Tests move faster than users. Sometimes too fast. The test may need to wait for an event to occur before doing the next thing.
Letting Thunderbird Respond Before Proceeding
Sometimes the test needs to let the application respond to something the test did before moving on to the next step, and there is not an event to listen for. Here is a simple way to do this:
Interacting with Regular Windows
Some tests will need to interact with windows that are not the main window. For example, below is a function that opens the address book window. It returns the window
(nsIDOMWindow
) object for the address book window, which can then be used when calling functions like EventUtils.sendKey
.
The key point is the use of BrowserTestUtils.domWindowOpened
, but this example also demonstrates some of the other tips found in this document. (See below for dialog windows which are handled differently.)
Interacting with Dialog Windows
Interact with dialog windows by using BrowserTestUtils.promiseAlertDialog
.
Interacting with Trees
Trees are not like other DOM elements and require special handling.
Last updated