- Test Script Synchronization
- Placing a Generic Delay in Your Script
- Waiting for Test Objects to Come into Existence
- Timing How Long Something Takes to Execute in a Script
- Working with Test Objects
- Working with the Clipboard Object
- Viewing an Objects Properties
- Retrieving All Properties of a Test Object
- Retrieving the Value of a Single Property
- Programmatically Retrieving Data from an Object in My Application
- Determining a Test Objects Valid Test Data Type Arguments to getTestData()
- Using getTestData to Extract Data from a Text Field
- Using getTestData to Extract Data from a List
- Using getTestData() to Read Data in a Table
- Using getTestData() to Extract Data from a Tree
- Obtaining Data from a Test Object That the Rational Functional Tester Verification Point Wizard Does Not Capture
- Creating a Custom Verification Point
- Changing the Value of a Test Objects Property
- Evolving Custom Scripting into Reusable Methods
- Summary
Evolving Custom Scripting into Reusable Methods
Up to this point, you have learned how to make different types of enhancements to your scripts, making them more robust. You saw how to add delays, introduce timers, work with the Windows Clipboard, manipulate different objects you recorded (such as test objects and verification points), and so on. Rational Functional Tester enables you to expand these techniques by turning them into generic, reusable methods.
Methods that are reusable and, ideally generic, enable you to apply them to more than one object or script. You can often use them across your entire test project. This enables you to create methods that benefit everybody on your team, saving time on your overall test automation effort.
The first thing that is involved with creating custom methods is setting up the signature. Basically, you specify the type of data your method needs to receive and the type of data it needs to send back when it’s done executing. You can work with the typical data types and objects that the Java and VB.NET languages work with. For example, you might have an add method with a signature that looks like the following:
public int add(int i, int j)
The VB.NET equivalent is:
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
In both instances, you are merely passing two integers into the method (function in VB.NET), returning the sum of them as an integer.
Rational Functional Tester enables you to pass and return test objects. This gives you the ability to create custom methods for the objects that you capture in your test object map. A simple instance of this might be a method to test if an object is enabled or not. The method signature would like the following Java example:
public Boolean isEnabled(TestObject myGuiObject)
In VB.NET, you see:
Public Function isEnabled(ByVal myGuiObject As TestObject) As Boolean
If you use a signature, such as the preceding examples, you can pass any test object that contains enabled property to your custom method. You can test if a button is enabled before you click it, a tree is enabled before you navigate it, a text box is enabled before typing in it, and so on.
After you have the signature defined for your method, it is a matter of providing the code that accomplishes your desired task. You might write reusable methods that wait for test object properties to change, that acquire data from test objects, and set properties for test objects. Regardless of the methods that you create, you can organize and package them into Helper Superclasses, calling them from your scripts.
Helper Superclasses (also called Helper Base Classes in the VB.NET version of the tool) are an excellent way for you to create a simple, project-wide means for storing your reusable methods. You can easily create a Helper Superclass in Rational Functional Tester by performing the following steps:
- Click File > New > Helper Superclass (this launches the Create Script Helper Superclass Wizard).
- Select the location to store your Helper Superclass.
- Provide a name for your Helper Superclass.
- Click the Finish button.
If you are using the VB.NET version, the steps are:
- Click File > New > Add Helper Base Class (this will launch the Add New Item Wizard, preselecting the Script Helper Base Class template).
- Provide a name for your Helper Base Class.
- Provide a location for your Helper Base Class.
- Click the Add button.
In either case, you end up with an empty class, prebuilt to extend the default capabilities of Rational Functional Tester’s RationalTestScript class. This is where you start constructing your custom, reusable methods. The following code samples, contained in Listing 3.18, show a Java and a .NET helper class. These contain the following sample methods from prior sections:
- printValueProperties()—“Retrieving All Properties of a Test Object” section
- getAllListElements()—“Using getTestData to Extract Data from a List” section
- printRootNodes()—“Using getTestData to Extract Data from a Tree” section
Listing 3.18. Helper Classes
Java package HelperSuperclasses; import java.util.Enumeration; import java.util.Hashtable; import com.rational.test.ft.object.interfaces.TestObject; import com.rational.test.ft.script.RationalTestScript; import com.rational.test.ft.vp.ITestDataElement; import com.rational.test.ft.vp.ITestDataElementList; import com.rational.test.ft.vp.ITestDataList; import com.rational.test.ft.vp.ITestDataTree; import com.rational.test.ft.vp.ITestDataTreeNode; import com.rational.test.ft.vp.ITestDataTreeNodes; public abstract class MyHelperSuperclass extends RationalTestScript { public void printValueProperties(TestObject testObject) { Hashtable valueProperties = testObject.getProperties(); Enumeration valuePropNames = valueProperties.keys(); Enumeration valuePropValues = valueProperties.elements(); while (valuePropNames.hasMoreElements()) { System.out.println(valuePropNames.nextElement() + ":" + valuePropValues.nextElement()); } } public String[] getAllListElements(TestObject testObject) { String[] all = null; ITestDataList iList = (ITestDataList) testObject.getTestData("list"); int count = iList.getElementCount(); all = new String[count]; ITestDataElementList iElementList = iList.getElements(); for (int i = 0; i < count; i++) { ITestDataElement iElement = iElementList.getElement(i); String value = iElement.getElement().toString(); all[i] = value; } return all; } public void printRootNodes( TestObject tree ) { String selectedNode = null; ITestDataTree iTreeData = (ITestDataTree)tree.getTestData("selected"); ITestDataTreeNodes iNodes = iTreeData.getTreeNodes(); ITestDataTreeNode[] rootNodes = iNodes.getRootNodes(); for(int i = 0; i < rootNodes.length; i++) { String nodeData = rootNodes[i].getNode().toString(); System.out.println( nodeData ); } } }
VB.NET Imports Rational.Test.Ft.Script Imports Rational.Test.Ft.Vp Imports System Imports System.Collections Imports Rational.Test.Ft.Object.Interfaces Namespace ScriptHelperBaseClasses Public MustInherit Class MyScriptHelperBaseClass Inherits RationalTestScript Public Sub printValueProperties(ByVal testobject As TestObject) Dim valueProperties As Hashtable = testobject.GetProperties Dim valuePropNames As ICollection = valueProperties.Keys Dim valuePropValues As ICollection = valueProperties.Values Dim enumPropNames As IEnumerator = valuePropNames.GetEnumerator() Dim enumPropVals As IEnumerator = valuePropValues.GetEnumerator() Do While enumPropNames.MoveNext And enumPropVals.MoveNext Dim currentProp As String = enumPropNames.Current Dim currentPropVal As Object = enumPropVals.Current If TypeOf currentPropVal Is Object Then If Not (TypeOf currentPropVal Is String) Then currentPropVal = currentPropVal.ToString End If End If Console.WriteLine(currentProp + ":" + currentPropVal) Loop End Sub Public Function getAllListElements(ByVal testObject As TestObject) As String() Dim iData As ITestDataList = testObject.GetTestData("list") Dim count As Integer = iData.GetElementCount() Dim all(count - 1) As String Dim iElementList As ITestDataElementList = iData.GetElements() For i As Integer = 0 To count - 1 Dim iElement As ITestDataElement = iElementList.GetElement(i) Dim value As String = iElement.GetElement().ToString() all(i) = value Next Return all End Function Public Sub printRootNodes(ByVal tree As TestObject) Dim selectedNode As String = Nothing Dim iTreeData As ITestDataTree = tree.GetTestData("tree") Dim iNodes As ITestDataTreeNodes = iTreeData.GetTreeNodes() Dim rootNodes As ITestDataTreeNode() = iNodes.GetRootNodes() For i As Integer = 0 To rootNodes.length - 1 Dim nodeData As String = rootNodes(i).GetNode.ToString Console.WriteLine(nodeData) Next End Sub End Class End Namespace
Please note that you might need to import certain packages and namespaces (depending on whether you are using Java or VB.NET). You receive errors from the compiler that let you know that certain classes, structures, objects, and so on do not exist. In the Java version of the tool, you can usually perform a <Ctrl> + O keystroke combination. This imports the necessary packages. There might be instances where you have to review the error messages to get an idea of what package needs to be imported. The VB.NET version of the tool requires you to import the necessary namespace.
After you have your Helper Superclass set up, creating the desired reusable methods, you need to perform two more steps. The first is to tell your test scripts to use the new class. In other words, you want to tell your scripts about the new methods that you created. The second step is to actually call your custom methods from your script.
You can associate your new Helper Superclass either at the individual script level or at the project level. Associating your Helper Superclass at the project level causes any new script to be aware of your custom methods. If you already have scripts created, you need to associate your Helper Superclass to each of them. To associate your Superclass to your project, you do the following:
- Right-click your project node in the Functional Test Projects view (Solution Explorer, if you’re using Rational Functional .NET).
- Click Properties.
- Click Functional Test Project (on the left-hand side of the Properties window).
- Click the Browse button next to the Script Helper Superclass property (Script Helper Base Class property if you are using Rational Functional Tester .NET).
Select your created Helper Superclass (Helper Base Class).
Note: You might need to type the first few letters of the name of your Super Helper class to get it to display in the list when using the Java version of Rational Functional Tester.
- Click the OK button.
Associating your Helper Superclass to individual scripts is a similar process. You need only to perform the following steps:
- Right-click an existing script in your project node in the Functional Test Projects view (Solution Explorer, if you are using Rational Functional .NET).
- Click Properties.
- Click Functional Test Script (on the left-hand side of the “Properties” window).
- Click the Browse button next to the Helper Superclass property.
Click the created Helper Superclass (Helper Base Class).
Note: You might need to type the first few letters of the name of your Super Helper class to get it to appear in the list when using the Java version of Rational Functional Tester.
- Click the OK button.
After you make the necessary Helper Superclass associations, the last thing you need to do is to actually use the custom work that you created. Referring to the methods and functions in Listings 3.17 and 3.18, you simply need to call the specific custom method(s) that you need.
Figures 3.24 and 3.25 show scripts that make calls to the custom methods contained in the Helper Superclass. This should give you an idea of how your custom work can save time for the other members on your team. They do not have to reinvent the wheel you already created. They can simply reuse the code that you built and use the method(s) that you placed in a Helper Superclass.