Squish/Iterating Through a Table: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (style)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
this page in: [[Category:Tools::Squish::IteratingThroughATableSpanish|Español]]
[[Category:Squish]]
 
[[Category:Tools::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.
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:<br /><code>verifyNoEmptyCells(":MyQTableWidget")<code>
In the end, the usage of any of these three approaches would simply be:
<code>
verifyNoEmptyCells(":MyQTableWidget")
</code>


== Qt API approach ==
== Qt API approach ==
Line 13: 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):<br /> table = waitForObject(tableName)<br /> model = table.model()<br /> columnCount = model.columnCount()<br /> rowCount = model.rowCount()
<code>
def verifyNoEmptyCells(tableName):
  table = waitForObject(tableName)
  model = table.model()
  columnCount = model.columnCount()
  rowCount = model.rowCount()


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


== waitForObjectItem() ==
== waitForObjectItem() ==
Line 21: 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):<br /> table = waitForObject(tableName)<br /> columnCount = table.columnCount<br /> rowCount = table.rowCount
<code>
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 != "")
</code>
 
== 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.
 
<code>
def verifyNoEmptyCells(tableName):
  waitFor("object.exists(tableName)")
  columnCount = findObject(tableName).columnCount
  rowCount = findObject(tableName).rowCount


for col in range(columnCount):<br /> for row in range(rowCount):<br /> item = waitForObjectItem(tableName, str(row)+"/"''str(col))<br /> test.verify(item.text != "")<code>
  for col in range(columnCount):
<br />h2. Real name approach
    for row in range(rowCount):
<br />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.
      item = waitForObject("{column='" + str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")
<br />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.
      test.verify(item.text != "")
<br /></code>def verifyNoEmptyCells(tableName):<br /> waitFor("object.exists(tableName)")<br /> columnCount = findObject(tableName).columnCount<br /> rowCount = findObject(tableName).rowCount
</code>
<br /> for col in range(columnCount):<br /> for row in range(rowCount):<br /> item = waitForObject("{column='"'' str(col) + "' container='" + tableName + "' row='" + str(row) + "' type='QModelIndex'}")<br /> 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 != "")