Procedures and Libraries
Procedures allow to create pieces of reusable code. If you have paid attention to the previous topics, you might have already spotted one procedure called startApp
.
Each procedure consists of a header, body and the right closing curly brace. The header must be defined on a single line and consists of the "procedure" keyword, procedure name (case sensitive) and the left opening curly brace. Procedures may be called by name anywhere in the script after their definition. Such calls may contain any number of parameters which are then made available in the body through numbered variables starting from one, for example "{1}", "{2}" etc. The zero variable, "{0}" always contains the procedure name and may be used for example for reporting purposes.
Procedures together with global variables allow to create libraries. By a library we understand a standalone script which usually contains reusable variables and procedures. The scripting language supports including of libraries through the Include and Run commands. If the library contains just variables and procedures, both commands just import these objects to the current script. If the library contains some standalone commands (meaning it is a normal script with the main code body), the Run command executes them (Include not). Note that the commands can also load and execute Java code; see the Interoperability of Scripts and Java code topic for details.
Let's reuse the calculator scripts defined earlier on in this tutorial:
calculator.tpr (Windows version) | calculator.tpr (Linux/Gnome version) |
---|---|
CODE
|
CODE
|
Our goal is to extract reusable pieces of code to a library and make the script ready for generic calculator testing both on Windows and Linux. For this purpose we will define a new procedure called calculate which will calculate a formula specified through its parameter regardless of the tested OS. To show the resulting screen shots we will also for the first time in this tutorial create an HTML report.
The resulting code looks like follows.
calculator.tpr (Windows version) | calculator-lib.tpr |
---|---|
CODE
|
CODE
|
calculator.tpr (Linux version) | |
CODE
|
Now you may object that there are still two script versions, one for Windows and another one for Linux. It doesn't necessarily have to be true. If you execute the script manually from the GUI, you will have to either use two scripts or rewrite the OS variable depending on your test environment. For automated execution there will be however just one script which will be just differently executed from CLI with the "OS" variable set to "Windows" or "Linux" depending on the target test system. We have already shown you how to do it in one of the previous topics:
- Remote desktop is Windows:
java -jar robot.jar -c <desktop> -p <password> -r calculator.tpr -v OS=Windows
- Remote desktop is Linux:
java -jar robot.jar -c <desktop> -p <password> -r calculator.tpr -v OS=Linux
This is of course not the only solution. You may also create two separate libraries, one for Windows and another one for Linux. To include them dynamically specify the Include argument as variable (" Include"{MYLIB}"
") and set the value to the appropriate library file at the CLI.
An obvious question is whether the OS detection can be automatic. The answer is that it is possible, but not with VNC. The RFB protocol unfortunately doesn't provide any mechanism allowing to identify OS of the remote desktop. We could only guess from the desktop image which would never be reliable enough. OS identification may be however supported by other technologies which are on our road map, such as RDP or local desktop drivers.