Squish/Iterating Through a Table: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(correct second typo)
m (style)
 
Line 1: Line 1:
{{LangSwitch}}
[[Category:Squish]]
[[Category:Squish]]


Line 6: Line 4:


In the end, the usage of any of these three approaches would simply be:
In the end, the usage of any of these three approaches would simply be:
<code>verifyNoEmptyCells(":MyQTableWidget")<code>
<code>
verifyNoEmptyCells(":MyQTableWidget")
</code>


== Qt API approach ==
== Qt API approach ==
Line 12: Line 12:
This approach uses the Qt API to access the model directly and iterate through the rows and columns of the table.
This approach uses the Qt API to access the model directly and iterate through the rows and columns of the table.


</code>def verifyNoEmptyCells(tableName):
<code>
table = waitForObject(tableName)
def verifyNoEmptyCells(tableName):
model = table.model()
  table = waitForObject(tableName)
columnCount = model.columnCount()
  model = table.model()
rowCount = model.rowCount()
  columnCount = model.columnCount()
  rowCount = model.rowCount()


for col in range(columnCount):
  for col in range(columnCount):
for row in range(rowCount):
    for row in range(rowCount):
itemText = str(model.data(model.index(row, col), Qt.DisplayRole).toString())
      itemText = str(model.data(model.index(row, col), Qt.DisplayRole).toString())
test.verify(itemText != "")<code>
      test.verify(itemText != "")
</code>


== waitForObjectItem() ==
== waitForObjectItem() ==
Line 27: Line 29:
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.
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.


</code>def verifyNoEmptyCells(tableName):
<code>
table = waitForObject(tableName)
def verifyNoEmptyCells(tableName):
columnCount = table.columnCount
  table = waitForObject(tableName)
rowCount = table.rowCount
  columnCount = table.columnCount
  rowCount = table.rowCount


for col in range(columnCount):
  for col in range(columnCount):
for row in range(rowCount):
    for row in range(rowCount):
item = waitForObjectItem(tableName, str(row)+"/"+str(col))
      item = waitForObjectItem(tableName, str(row) + "/" + str(col))
test.verify(item.text != "")<code>
      test.verify(item.text != "")
</code>


== Real name approach ==
== Real name approach ==
Line 42: Line 46:
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.
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.


</code>def verifyNoEmptyCells(tableName):
<code>
waitFor("object.exists(tableName)")
def verifyNoEmptyCells(tableName):
columnCount = findObject(tableName).columnCount
  waitFor("object.exists(tableName)")
rowCount = findObject(tableName).rowCount
  columnCount = findObject(tableName).columnCount
  rowCount = findObject(tableName).rowCount


for col in range(columnCount):
  for col in range(columnCount):
for row in range(rowCount):
    for row in range(rowCount):
item = waitForObject("{column='" + str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")
      item = waitForObject("{column='" + str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")
test.verify(item.text != "")<code>
      test.verify(item.text != "")
</code>

Latest revision as of 10:05, 11 August 2020


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 != "")