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:
  1. Continue editing the file Mod_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.

  1. 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"); 
  2. Assign the action's name to a variable, for output to the results page or warning messages:
    String checkLabel = "check row count";                  
  3. Note: Each control has its own instance (object) of the AbtElement class, which can be obtained by using the OpenElement method of AbtAutomation.
    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 class AbtElement, 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:

  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;
    	}                   
  2. Next, use getRowCount() to determine the number of rows in the table:
    int rowCount = table.getRowCount();                   
  3. Cycle through all rows of the table, using getCellText() 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;			
    	}                  
  4. 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 the administerCheck() 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 the Integer.toString() function in Java.
  5. Open build.xml in the editor: In Package Explorer, double-click the build.xml node under javaharness.
  6. 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.)
  7. 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.

  8. Set the compiler setting for your Java project to 1.6:
    1. In Package Explorer, right-click your Java project, then select Properties.
    2. In the left-hand panel of the Properties for <project name> dialog box, select Java Compiler.
    3. In the Java Compiler panel, select the Enable project specific settings check box.
    4. Select 1.6 as the desired compiler compliance level.

    5. Click OK to apply the setting and close the Properties for <project name> dialog box.
  9. 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);
			
	}