Scripting a Java-based GUI-interfacing action
In Java code, implement the action that will interface with a target application.Define the function action_CheckRowCount(), which will handle the execution of the check row count action in TestArchitect.
The definition of action_CheckRowCount() remains to be implemented. Compared to the earlier action_hello() function, this involves considerably more work. In particular, the code needs to:
- find the table in the system under test,
- access it to retrieve its rows one-by-one,
- check a given cell of each row for an expected value,
- maintain a count of matching rows, and
- in the end, perform a “test check” (of the actual count against an expected count), and inform TestArchitect of the result.
Continue editing the fileMod_Table.java by declaring the method definition action_checkRowCount():
/** * "check row count" action implementation */ public static void action_checkRowCount() { //Insert action implementation code here }
The first step within your method is to grab the values of the action line arguments and stuff them into variables.
Enter the code lines below.
The interpreter function NamedArgument(), which is part of the standard LIBRARY object, and which you also used in the previous exercise, is used to fetch the argument values.
String windowName = AbtLibrary.NamedArgument("window"); String tableName = AbtLibrary.NamedArgument("table"); String columnName = AbtLibrary.NamedArgument("column"); String value = AbtLibrary.NamedArgument("value"); String expected = AbtLibrary.NamedArgument("expected");
Assign the action’s name to a variable, for output to the results page or warning messages:
String checkLabel = "check row count";
- Note:Each control has its own instance (object) of the AbtElement class, which can be obtained by using the OpenElement method ofAbtAutomation.
Obtain the instance of the table object for the table specified by the values of windowName and tableName (derived from the logical TA names of the window and table arguments of the action):
AbtTable table = (AbtTable) AbtAutomation.openElement(windowName, tableName); if (table == null) { AbtLibrary.reportWarning(checkLabel + ": Unable to identify table '" + tableName + "'"); return; }
If the OpenElement() is successful (meaning, if it does indeed find the window and table it’s told to look for), it returns an object of classAbtElement, then it will be converted it into class AbtTable, a sub-class of AbtElement. Of this class, you will make use of the following functions:
- getColumnIndex(), which returns the number of the column you are interested in, based on the column name you provide (which, in turn, comes from the column argument in the action line)
- getRowCount(), which returns the number of rows in the table, letting you know how many rows you need to search through
- getCellText(), to get the value of a given cell. (Cells are identified with a row and column number, and start with row 1.)
Use getColumnIndex()to get the number of the column specified by the column name argument:
int column = table.getColumnIndex(columnName); if (column == -1) { AbtLibrary.reportWarning(checkLabel + ": Unable to locate column '" + columnName + "' in table '" + tableName + "'"); return; }
Next, use getRowCount()to determine the number of rows in the table:
int rowCount = table.getRowCount();
Cycle through all rows of the table, usinggetCellText()in each case to get the contents of the cell of interest. Increment the tally every time a match is found with the value of the value argument of the action.
int count = 0; for (int i = 0; i < rowCount; i++) { if (table.getCellText(i, column).equals(value)) count += 1; }
Determine whether the actual count of matching rows is equal to the number expected (as given by the action’s expected argument). Depending on the result, use theadministerCheck()function to send either a PASS or FAIL report to the TestArchitect results page:
if (expected.equals(Integer.toString(count))) AbtLibrary.administerCheck(checkLabel, expected, Integer.toString(count), 1); else AbtLibrary.administerCheck(checkLabel, expected, Integer.toString(count), 0);
Note:TestArchitect functions always work with strings. Therefore, integer results like count must be converted to strings. This is handled by theInteger.toString() function in Java.Open build.xml in the editor: In Package Explorer, double-click the build.xml node under javaharness.
Modify the setting of the destfile attribute to specify the filepath of the JAR, java_harness.jar, that is to be built. (We’ll specify only the file name, so that it will be generated at the same level as build.xml.)
Modify the setting of the dir attribute to specify the root of the directory tree of the bin file set,where the compiled java class files will be stored.
Set the compiler setting for your Java project to 1.8:
In Package Explorer, right-click your Java project, then select Properties.
In the left-hand panel of the Properties for <project name> dialog box, select Java Compiler.
In the Java Compiler panel, select the Enable project specific settings check box.
Select 1.8 as the desired compiler compliance level.
Click OK to apply the setting and close the Properties for <project name> dialog box.
In Package Explorer, right-click the file build.xml and choose Run As > Ant Build to build an executable JAR file, java_harness.jar.
The java_harness.jar file is created at the specified location within your Eclipse workspace.
Your entire action definition for action_checkRowCount() should now look similar to this:
/**
* "check row count" action implementation
*/
public static void action_checkRowCount() {
String windowName = AbtLibrary.NamedArgument("window");
String tableName = AbtLibrary.NamedArgument("table");
String columnName = AbtLibrary.NamedArgument("column");
String value = AbtLibrary.NamedArgument("value");
String expected = AbtLibrary.NamedArgument("expected");
String checkLabel = "check row count";
AbtTable table = (AbtTable) AbtAutomation.openElement(windowName, tableName);
if (table == null){
AbtLibrary.reportWarning(checkLabel + ": Unable to identify table '" + tableName + "'");
return;
}
int column = table.getColumnIndex(columnName);
if (column == -1){
AbtLibrary.reportWarning(checkLabel + ": Unable to locate column '" + columnName + "' in table '" + tableName + "'");
return;
}
int rowCount = table.getRowCount();
int count = 0;
for (int i = 0; i < rowCount; i++){
if (table.getCellText(i, column).equals(value))
count += 1;
}
if (expected.equals(Integer.toString(count)))
AbtLibrary.administerCheck(checkLabel, expected, Integer.toString(count), 1);
else
AbtLibrary.administerCheck(checkLabel, expected, Integer.toString(count), 0);
}