Writing Tests
Thursday, August 6th, 2009 @ 16:12 by whimboo![]()
![]()
Now that you know how to run existing Mozmill tests and how they work we want to go one step further. Our next target is to write our own tests. It's not a complicated task but you have to obey some simple rules so we can guarantee long living and understandable tests for everyone.
How to Start
To make it easier for you to create your first Mozmill tests we have prepared a couple of template files for your usage. They will help you to get familiar with the license block, needed test functions, shared modules, and they way how modal dialogs have to be handled. You can find these files in your local version of the test repository or online.
Take care about the following bullets when using the templates:
- Please update the name and the email address in the license block
- Enter the Litmus test id and description to the comment right below the license block. If your test covers multiple Litmus tests duplicate the lines as many times as needed.
- Use a meaningful name for your test function which indicates the overall target of the test.
Coding Styles
Additional to the initial changes to the test files there are some other rules you should take care of. It will help us to better understand the tests of others:
- Try to follow the Javascript Style Guides especially for whitespaces, naming convenstions and indentation. For now we don't restrict lines to 80 characters or less because some Lookup strings can be much longer. But try to shorten them as much as possible (e.g. by moving the element declaration to its own line).
- Use comments wisely. Group lines of code belonging together and give those a combined comment. See a shared module as example.
- Use the Litmus testcase description for naming your test file and save it into a subfolder of the firefox directory which has the same name as the Litmus subgroup. If you are working on a restart test instead the file naming convention says to use testX.js where X is a number. Place all test files into its own folder under firefox/restartTests.
If that was not enough information you should have a look at the already existing tests in the Mozmill Test repository.
Mozmill API Documentation
Mozmill offers a huge set of functions. It's too much for a simple replication for this tutorial. So please check the Mozmill documentation which give a full explanation for each single function.
Further we have implemented helper functions which haven't been made into the Mozmill code yet. Those can help you in various situations. To get an overview check the "Shared Modules" chapter.
Tips and Tricks
Sometimes you will run into trouble while creating Mozmill tests. It's our attitude to help you in solving those problems and to simplify your test creation process.
- Use the Inspector or Recorder to create the skeleton of your test. You have to add additional steps like calls to the sleep function or element checks before the test can be run.
- Use controller.sleep(gDelay) between different major steps. It will slow down the test execution which is helpful for testing the Mozmill test.
- If you are using controller.open() to load a web page a controller.waitForPageLoad() has to be used right afterward. A controller.sleep() call is not sufficient.
- Use the menu API to reach additional commands which are only available via the main menu. A list of existing id's for menu items can be found in that browser-menubar.inc. Due to our localization efforts please never use the label attribute of menu items.
- If your test needs exactly one tab open use "UtilsAPI.closeAllTabs(controller);" as the first line in your test.
- If you modify preferences or other global data make sure to reset those values inside the teardownModule function. That will clean-up the environment for the next Mozill test.
- Avoid using any hardcoded strings for the elementslib Lookup function. Those will break Mozmill tests for localized builds. After using the inspector you have to manually remove those attributes (e.g. label or accesskey) from the element string (see the next bullet).
- If an element can only be referenced by the elementslib Lookup function please try to remove as much as possible attributes from each hierarchy. That will make the test more readable and can avoid failed lookups when the code in Firefox changes.
When your test is ready you have to create a patch and to ask for review.