Suppose you have a table with contents you want to assert. The usage of assertion mode during test case recording can be a tedious task – it requires to select each and every row in a table and then select columns we want to assert. Here's an example of asserting a table with Installed JREs in Eclipse (and it is just a single row of three-column table, imagine how it would look like in case of 5 rows and 10 columns):

get-preferences-menu | click
get-window Preferences | get-tree | select "Java/Installed JREs"

with [get-window Preferences | get-table] {
    get-item "Java SE 6 \\(MacOS X Default\\)" | get-property columns -index 0 | equals "Java SE 6 (MacOS X Default)" 
        | verify-true
    get-item "Java SE 6 \\(MacOS X Default\\)" | get-property columns -index 1 
        | equals "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" | verify-true
    get-item "Java SE 6 \\(MacOS X Default\\)" | get-property columns -index 2 | equals "MacOS X VM" | verify-true
}
get-window Preferences | get-button OK | click

This code is hard to understand and maintain, there must be a better way. And starting from Q7 version 1.2.2 there is – take a look at the following snippet:

get-preferences-menu | click
with [get-window Preferences] {
	get-tree | select "Java/Installed JREs"
	get-table | get-table-data | eq [read-csv-file "workspace:/assertData/table.csv"] | 
		assert-true "Data in table does not match input file" 
	get-button OK | click
}

In the example above we take a data from the table as structured EMF object using get-table-data command and then comparing it to another EMF object taken from a CSV file by read-csv-file. To prepare CSV file, it is possible to use write-csv-file command by manually putting it into a snippet in Control Panel like this (note that you need to create an assertData project to make snippet above working):

get-preferences-menu | click
with [get-window Preferences] {
	get-tree | select "Java/Installed JREs"
	get-table | get-table-data | write-csv-file "workspace:/assertData/table.csv"
	get-button OK | click
}

Once assertion data is in your AUT workspace, it is trivial to put it into a Workspace context so that during test execution it will always be there.

Commands read-csv-file and write-csv-file, as well as a data objects they manipulate are the part of open-source ECL library, their implementation can be found here – ReadCsvFileService and WriteCsvService. So it is possible to write a custom ECL command which will return the same Table EMF object and use it with these commands.

 

Also there are two useful commands for filtering table data:

  • exclude-columns names – excludes given column names from a table.
  • select-columns names – selects only given column names from a table.
 

Here's an example:

 
get-view "Error Log" | get-tree | get-table-data | exclude-columns "Plug-in" 
get-view "Error Log" | get-tree | get-table-data | select-columns "Message" "Plug-in" 


When data in tree is hierarchical, when we save a table into CSV file, we add an extra column _indent, which identifies the depth level of a given row (considering that the parent is always listed on top), for example if we have a project "foo" with two files "bar" and "baz" and we save table data into CSV, the contents of CSV file will be like this:

_indent, text
0, foo
1, bar
1, baz

Thanks to Marie-France for giving us an idea for these commands!