Cogitek RIATest 3 Documentation Copyright © Cogitek Inc.

Working with DataGrid and AdvancedDataGrid

This section contains useful tips on working with Flex DataGrid and AdvancedDataGrid components.

Selecting row by cell data

If you want to select a row that contains a particular cell use the following code:

// Select using 'data' of the cell
FlexDataGrid("dg1")=>select({data:{first:"Arthur"}});

The above code will select a row with "Arthur" text in the column with data field named "first".

An alternate approach is to use listData property:

// Using dataField to specify column and label to specify row
FlexDataGrid("dg1")=>select({listData:{dataField:"first",label:"Paul"}});

This will select row with text equal to "Paul" in the column "first".

You can use regular expressions for partial matching of cell text, e.g.:

// This will select row which contains text "John". Note regular expression usage.
FlexDataGrid("dg1")=>select(/.*John.*/);  

You can also use regular expression for sub-property matching, e.g. combining the samples above:

// Select using 'data' of the cell
FlexDataGrid("dg1")=>select({data:{first:/.*John.*/}});

This will select a row that contains a cell in the "first" column which contains text "John" (but not necessarily equal to it).

Selecting row by index in DataGrid

The AdvancedDataGrid has a selectIndex event, however DataGrid does not have the equivalent event. The following code will help you select a row by index:

var index = 5;
FlexDataGrid("dg")=>select({automationIndex:"firstColumn:"+index});

The above code assumes that there is a column named "firstColumn" in the grid.

Selecting multiple rows

Use EVT_MOUSE and KM_CTRL to emulate ctrl-click and select a row without un-selecting previously selected row(s).
For example the following code will select rows 0, 1 and 3:

FlexAdvancedDataGrid("adg1")=>selectIndex(0);
FlexAdvancedDataGrid("adg1")=>selectIndex(1,EVT_MOUSE,KM_CTRL);
FlexAdvancedDataGrid("adg1")=>selectIndex(3,EVT_MOUSE,KM_CTRL);

The same applies to regular select() function:

FlexDataGrid("dg1")=>select("*js* | John | Smith");
FlexDataGrid("dg1")=>select("*pw* | Paul | Brown",EVT_MOUSE,KM_CTRL);  
FlexDataGrid("dg1")=>select("*aw* | Arthur | White",EVT_MOUSE,KM_CTRL); 

Selecting last row

Use the following code:

FlexDataGrid("dg")=>keyPress(KEY_END); 

Finding out the number of rows in a grid

To find out the number of all rows (not just visible) in a grid use getTabularData function, e.g.:

var rowCount = new Locator({id:"myDataGrid"})=>getTabularData().length;
trace(rowCount);

Accessing entire data in a grid

If you want to access all the data in the cells you can use getTabularData() to get cell values in tabular form, e.g.:

var comp = FlexDataGrid("dg1"); 
verifyEqual(comp=>getTabularData(), 
[["pw","Paul","Brown"], 
 ["js","John","Smith"], 
 ["aw","Arthur","White"]]);

You can also get just the visible portion, e.g.:

var comp = FlexList("list1");
verifyEqual(comp=>getTabularData(comp=>automationTabularData.firstVisibleRow, 
    comp=>automationTabularData.lastVisibleRow), 
[["Item 1"], 
 ["Item 2"], 
 ["Item 3"], 
 ["Item 4"]]);

This also works for many other component types, not only DataGrid or AdvancedDataGrid.

Searching for data in a grid

If you need to search for a data in a grid use the getTabularData function to get the entire data and loop through it to search. For example to count the number of occurrences of certain text in a grid use this code:

var td = FlexAdvancedDataGrid("adg1")=>getTabularData(); 
var textToSearch = "World"; 
var count = 0; 
for (var i=0; i<td.length; i++) 
{ 
    var row = td[i]; 
    for (var j=0; j<row.length; j++) 
    { 
        if (row[j]==textToSearch) 
            count++; 
    } 
} 

Verifying that a cell is not present in the grid

To ensure that some particular cell does not exist in the grid use the isPresent function:

verify(!isPresent(FlexDataGrid("someGrid")->FlexListLabel("col1 | col2 | *item123* | col4"))); 

Scroll to find an item

If you need to find an item but are not sure whether it is visible or not you can use the following function:

function findAndSelectItem(grid,item) 
{ 
    var itemCount = grid=>getTabularData().length; 
    var pos = 0; 
    while (!grid->FlexListLabel(item)=>visible && pos<itemCount)
        scroll(pos++,SCROLL_VERTICAL,SCROLL_LINEDOWN); 
    grid=>select(item); 
} 
findAndSelectItem(FlexDataGrid("myDG"),"An Item");

The function uses tabular data to get the number of rows, then scrolls down until it finds the item, then selects it.

 


Found a typo? Have a suggestion? Please submit your request here.