In the previous section, I showed how to reduce duplication of data in tests, with the goal of reducing maintenance cost. Another way to reduce maintenance cost is to reduce duplication of logic with the use of Macros and Functions, as described in this section.

In the previous section, I moved data out of my test so that it would not be duplicated in every test. In this section, I will move a series of steps out of my test into a macro, so that they will not be duplicated in every test.

A Macro is a re-usable group of steps that can be called from a test with a Call Macro step. When the macro is called, the test runs all the steps in the macro as if they were part of the test.

It is likely that many of my tests will start by opening a browser and going to the starting URL that I defined. So I want to pull those two steps out of the test into a re-usable resource. I start by creating a macro.


Then I copy-and-paste the first two steps from my test into the macro.


Finally, update the test to call the macro start-test.


When the test is run in MuseIDE, you will see each step in the macro executed.


In addition to macros, you can also create functions in the same way.

A function is similar to a macro - it is a re-usable set of steps. However, it has three key differences:

  1. It runs in an isolated variable scope.
  2. You can pass named parameters into the function (optional).
  3. The function can return a value (optional).

An isolated variable scope means that it cannot reference variables defined in the test it is called from. It can, however, reference variables injected into the test context by initializers, default test variables or a parameterized test suite.

In this test, I did not need a function. So here is an example (from the Muse Examples project) of a function that references some parameters that were passed to it. Note the reference to variables counter and param1, which are not defined within the function.


Next, take a look at how the function is called. To define the parameters to pass to the function, and the name of the variable to assign the return value to, you must use the step editor (click the more link when configuring the step).


Continue to re-using locators.

Return to Tutorials page