3.10.Modularizing Test Scripts
Marathon supports modules - set of methods that can be called from within a test script. Modules improve reusability of script,
since a single module method can be used in multiple places. Marathon modules are regular ruby script files with a set
of methods. The difference between regular ruby modules and Marathon modules is that you can insert a Marathon module
method while recording a script.
Using Marathon you can create templates for a module method and save them. MarathonITE provides an Extract Module refactoring
using which you can easily create a module method.
In this chapter after a quick look at advantages of refactoring scripts you will see how you can create modules in Marathon.
Modularizing is the act of splitting a test script into methods. Using methods in a test script makes it easier
to read and understand it. Methods also allow you to avoid duplication and the insert script Marathon action while recording,
allows you to reuse the methods even while recording a test script.
There are many valid reasons for creating a method. Let us enumerate some of them here.
1. Reducing complexity
You need to think about what is inside a method while developing or changing the method itself, but once a method is written
you just use it.
2. Avoiding duplicate code
By extracting the code into a common method (may be with some parameters) you can avoid duplication.
3. Isolate changes
Suppose you have a dialog where you enter user details. What happens when the dialog is changed? If the enter user details
operation is in a method even though it may have been used hundreds of times - you need to modify only a single method.
4. Isolating operations on custom components
There comes a time when you need to support a custom component in your test project. Fortunately, Marathon facilitates accessing
the underlying Java component. You can access that component and perform the operations. But what happens when you need
to do this in a lot of test scripts? Pull that part of code as a module method and you can use insert script command
to perform all the jugglery.
5. Script readability
It's easier to understand when a certain code is extracted to a method rather than going through all the code.