questions about DataGrid and finding elements

Class library usage, coding and language questions.
jasong
Posts: 49
Joined: Fri Oct 26, 2007 9:38 pm
Location: Texas

questions about DataGrid and finding elements

Post by jasong » Wed Jan 02, 2008 5:26 pm

Is there a way to search the grid for a particular value (optionally submitting a column or row) and have the (column, row) coordinates returned?

I just used ElementFindChildValue successfully with MouseClick to select the item. However, this only works if that element is onscreen. If you must scroll down then it will not click on it yet ElementFindChildValue does indeed find the element.

I could not get ElementSelect to work correctly.

Code: Select all

child_element = Ranorex.ElementFindChildValue(parent_element,
                                                  select_text,
                                                  Ranorex.MATCH_SUBSTRING)
    if not child_element:
        return 1
    
    #ret = Ranorex.ElementSelect(child_element,
                                 #Ranorex.STATE_SYSTEM_FOCUSABLE | \
                                 #Ranorex.STATE_SYSTEM_SELECTABLE)
    #if not ret:
        #return 1
    
    ret2 = Ranorex.MouseClickElement(child_element,
                                     Ranorex.MOUSE_LEFT_BUTTON)
    if ret2:
        return 1
The MouseClickElement returns 0.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: questions about DataGrid and finding elements

Post by Support Team » Thu Jan 03, 2008 2:07 pm

jasong wrote:Is there a way to search the grid for a particular value (optionally submitting a column or row) and have the (column, row) coordinates returned?
You can either first search for a row element and then search only the children of that row or you search for the element name using regular expressions (the name of a cell is a contatenation of the column name, a white space, and the row name).
jasong wrote:I could not get ElementSelect to work correctly.
You have to use the right flag, namely Ranorex.SELFLAG_TAKESELECTION. So change the line to the following:

Code: Select all

ret = Ranorex.ElementSelect(child_element, Ranorex.SELFLAG_TAKESELECTION)
That should select the cell element - regardless of its visibility.

Alex
Ranorex Support Team

jasong
Posts: 49
Joined: Fri Oct 26, 2007 9:38 pm
Location: Texas

searching a value by column name

Post by jasong » Mon Jan 28, 2008 9:33 pm

Sorry for my long absence as I am just not getting back to test automation where I work.

I will try to use the column_row regular expression to search for a value. What method would I perform this search in?

For example, if I have a column named "Data ID" and I have my ID I wish to search for, how would I go about searching for this particular ID under that column?

Note that I need an approach that is highly reusable so that I can feed in variables of the column to search and the value to search for.

I may just be overcomplicating this... I hope I am. :)

Jason

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue Jan 29, 2008 1:18 pm

If you know the index of the column, the quickest way is to iterate through the rows and compare the value of the cell in that column:

Code: Select all

dataGridElement = Ranorex.ControlGetElement(dataGrid)
dataGridView = Ranorex.ElementFindChild(dataGridElement, Ranorex.ROLE_SYSTEM_TABLE)
rowCount = Ranorex.ElementGetChildCount(dataGridView)
for rowIndex in range(1, rowCount):
    row = Ranorex.ElementGetChild(dataGridView, rowIndex)
    columnCount = Ranorex.ElementGetChildCount(row)
    columnIndex = 1
    cell = Ranorex.ElementGetChild(row, columnIndex)
    cellValue = Ranorex.ElementGetValue(cell)
    if cellValue == 'SearchValue':
        print 'Found value at (' + str(rowIndex) + ',' + str(columnIndex) + ')'
The much slower method (because more cells have to be searched) is to search for all the cells of a column first by using regular expressions and then iterate these cells for the search value:

Code: Select all

dataGridElement = Ranorex.ControlGetElement(dataGrid)
dataGridView = Ranorex.ElementFindChild(dataGridElement, Ranorex.ROLE_SYSTEM_TABLE)
foundCellsCount = Ranorex.ElementFindChildren(dataGridView, Ranorex.ROLE_SYSTEM_CELL, "^Data ID ", "", Ranorex.MATCH_REGEXP)
for foundCellsIndex in range(0, foundCellsCount):
    cell = Ranorex.ElementGetChildFieldItem(foundCellsIndex)
    cellValue = Ranorex.ElementGetValue(cell)
    if cellValue == 'SearchValue':
        print 'Found the cell at row ' + str(foundCellsIndex)
Regular expressions can be used in all of the FindChild/FindChildren functions.

Alex
Ranorex Support Team

jasong
Posts: 49
Joined: Fri Oct 26, 2007 9:38 pm
Location: Texas

row has no indexed child elements

Post by jasong » Wed Jan 30, 2008 5:24 pm

Note that this control is a listview (ListView32) as I discovered through the spy.

I have the following code:

Code: Select all

col_count = Ranorex.ListViewGetColumnCount(parent)
    # now find the column index by comparing the text of each column to the
    # variable passed in
    for col_index in range(col_count):
        col_text = Ranorex.ListViewGetColumnText(parent, col_index)
        if col_text == vars['var1']:
            column_index = col_index
            break
    
    if not column_index:
        return 1
    
    child_element = None
    row_count = Ranorex.ElementGetChildCount(parent_element)
    # now search until we find the desired value passed in as a variable
    for row_index in range(1, row_count):
        row = Ranorex.ElementGetChild(parent_element, row_index)
        cell = Ranorex.ElementGetChild(row, column_index)
        if not cell:
            return 1
        cell_value = Ranorex.ElementGetValue(cell)
        if cell_value == vars['var2']:
            child_element = cell
            break
However, "row" does not contain any columns when I query for them or try to get the child with an index (ElementGetChild).


Thanks,
Jason

jasong
Posts: 49
Joined: Fri Oct 26, 2007 9:38 pm
Location: Texas

how can I select an item of a ListView by index and not name

Post by jasong » Wed Jan 30, 2008 6:11 pm

I switched to using ListView methods and am able to successfully find the value I am comparing against.

However, once I find that ListView item (with the subitem text I am searching for) then I am lost as how to select that entire item.

The problem is that the 'itemname' as far as I can tell from the Ranorex Spy is actually not unique. The reason for this is that the first column is the date received of our files and there are many with that same date.

Here is what I am using now:

Code: Select all

    row_count = Ranorex.ListViewGetItemCount(parent)
    for row_index in range(row_count):
        cell_value = Ranorex.ListViewGetItemText(parent, row_index, column_index)
        if cell_value == vars['var2']:
            ??? <-- here is where I am lost
            break
I get a complete count of items using this method (35) however if I try to get the element of the control ('parent') and count the number of children there, I only get 7 which is the number of the first group of number of dates (first column).

I am running around the block with this and figure there is an elegant solution that I have missed.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Thu Jan 31, 2008 11:20 am

jasong wrote:I get a complete count of items using this method (35) however if I try to get the element of the control ('parent') and count the number of children there, I only get 7 which is the number of the first group of number of dates (first column).
The immediate children of the ListView element aren't the ListView items, but its title bar, scroll bars and so on. This is why the ListView element always got 7 children.

Just use the FindChildren method to search the ListView element for elements with the role ROLE_SYSTEM_LISTITEM. This should give you the right number of item elements and you can easily select one by its index then.

We'll add a ListViewSelectItem method that takes an index to the ListView methods to simplify that selecting algorithm. This method will be included in V1.4.

Alex
Ranorex Support Team

jasong
Posts: 49
Joined: Fri Oct 26, 2007 9:38 pm
Location: Texas

now I am cooking with gas

Post by jasong » Thu Jan 31, 2008 8:42 pm

I have implemented what you suggested and now I can search for element values (text) passed in by variable.

Thank you doubly, as this greatly increases the reuse of our scripts.