Squish/Iterating Through a Table
There are several ways to iterate through a table in your test script to be used when you need to verify or take action on a given set of rows or columns. In the examples below we will use all three approaches to verify that there are no empty cells in a table.
In the end, the usage of any of these three approaches would simply be:
verifyNoEmptyCells(":MyQTableWidget")
Qt API approach
This approach uses the Qt API to access the model directly and iterate through the rows and columns of the table.
def verifyNoEmptyCells(tableName):
table = waitForObject(tableName)
model = table.model()
columnCount = model.columnCount()
rowCount = model.rowCount()
for col in range(columnCount):
for row in range(rowCount):
itemText = str(model.data(model.index(row, col), Qt.DisplayRole).toString())
test.verify(itemText != "")
waitForObjectItem()
Using waitForObjectItem() you can access items of a specific widget, such as actions in a menu or, in this case, cells in a table. It takes two parameters, the first being the name of the parent object and the second being the identification for the item. For cells in a table, that is two integers in the form of row/col.
def verifyNoEmptyCells(tableName):
table = waitForObject(tableName)
columnCount = table.columnCount
rowCount = table.rowCount
for col in range(columnCount):
for row in range(rowCount):
item = waitForObjectItem(tableName, str(row) + "/" + str(col))
test.verify(item.text != "")
Real name approach
Squish offers two ways of identifying your objects in a test script, either using the symbolic name which maps to an entry in the object map, or using the real name which lists all properties and their expected value in an identification string. Behind the scenes, the symbolic name of course maps to this real name, using the symbolic name generally makes the scripts more readable though.
By using the real name to identify cells, we can change the expected values of the properties and hence access different table cells as seen below.
def verifyNoEmptyCells(tableName):
waitFor("object.exists(tableName)")
columnCount = findObject(tableName).columnCount
rowCount = findObject(tableName).rowCount
for col in range(columnCount):
for row in range(rowCount):
item = waitForObject("{column='" + str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")
test.verify(item.text != "")