Scripting a Python-based GUI-interfacing action
In Python 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 file
mod_TableSupport.py
by declaring the method definitionaction_checkRowCount()
:def action_checkRowCount():
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.windowName = LIBRARY.NamedArgument("window") tableName = LIBRARY.NamedArgument("table") columnName = LIBRARY.NamedArgument("column") value = LIBRARY.NamedArgument("value") expected = LIBRARY.NamedArgument("expected")
Assign the action’s name to a variable, for output to the results page or warning messages:
checkLabel = "check row count"
- Note:Each control has its own instance (object) of the
AbtElement
class, which can be obtained by using theOpenElement
method ofABT
.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):
table = ABT.OpenElement(windowName, tableName) if table == None: LIBRARY.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 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 throughGetCellText()
, 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:column = table.GetColumnIndex(columnName) if column == None: LIBRARY.ReportWarning(checkLabel + ": Unable to locate column '" + columnName + "' in table '" + tableName + "'") return
Next, use
GetRowCount()
to determine the number of rows in the table:rowCount = table.GetRowCount()
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.count = 0 for i in range(1, rowCount): if table.GetCellText(i, column) == 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 the
AdministerCheck()
function to send either a PASS or FAIL report to the TestArchitect results page:if expected == format(count): LIBRARY.AdministerCheck(checkLabel, expected, format(count), 1) else: LIBRARY.AdministerCheck(checkLabel, expected, format(count), 0)
Note:TestArchitect functions always work with strings. Therefore, integer results like count must be converted to strings. This is handled by theformat()
function in Python.
Your entire action definition for action_checkRowCount()
should now look similar to this:
# check row count <window> <table> <column> <value> <expected> action implementation
# verify the number of rows with a given value in a specified column
def action_checkRowCount():
# get the arguments and assign a label for this check
windowName = LIBRARY.NamedArgument("window")
tableName = LIBRARY.NamedArgument("table")
columnName = LIBRARY.NamedArgument("column")
value = LIBRARY.NamedArgument("value")
expected = LIBRARY.NamedArgument("expected")
checkLabel = "check row count"
# identify the window and the table on screen
table = ABT.OpenElement(windowName, tableName)
if table == None:
LIBRARY.ReportWarning(checkLabel + ": Unable to identify table '" + tableName + "'")
return
# determine the column of interest
column = table.GetColumnIndex(columnName)
if column == None:
LIBRARY.ReportWarning(checkLabel + ": Unable to locate column '" + columnName + "' in table '" + tableName + "'")
return
# get the number of table rows
rowCount = table.GetRowCount()
# count the number of rows matching the column value
count = 0
for i in range(1, rowCount):
if table.GetCellText(i, column) == value :
count += 1
if expected == format(count):
LIBRARY.AdministerCheck(checkLabel, expected, format(count), 1)
else:
LIBRARY.AdministerCheck(checkLabel, expected, format(count), 0)
Ensure that your Python files, mod_TableSupport.py
and ta_main.py
are saved. Next, you’ll take care of hings on the TestArchitect client side.