Ranorex

questions about DataGrid and finding elements

 
Post new topic   Reply to topic    Ranorex Forum Index -> RanorexPython
View previous topic :: View next topic  
Author Message
jasong



Joined: 26 Oct 2007
Posts: 49
Location: Texas

PostPosted: Wed Jan 02, 2008 6:26 pm    Post subject: questions about DataGrid and finding elements
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: click into code to enlarge

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.
Back to top
View user's profile Send private message
Support Team
Site Admin


Joined: 07 Jul 2006
Posts: 256

PostPosted: Thu Jan 03, 2008 3:07 pm    Post subject: Re: questions about DataGrid and finding elements
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: click into code to enlarge
ret = Ranorex.ElementSelect(child_element, Ranorex.SELFLAG_TAKESELECTION)

That should select the cell element - regardless of its visibility.

Alex
Ranorex Support Team
Back to top
View user's profile Send private message Visit poster's website
jasong



Joined: 26 Oct 2007
Posts: 49
Location: Texas

PostPosted: Mon Jan 28, 2008 10:33 pm    Post subject: searching a value by column name
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. Smile

Jason
Back to top
View user's profile Send private message
Support Team
Site Admin


Joined: 07 Jul 2006
Posts: 256

PostPosted: Tue Jan 29, 2008 2:18 pm    Post subject:
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: click into code to enlarge

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: click into code to enlarge

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
Back to top
View user's profile Send private message Visit poster's website
jasong



Joined: 26 Oct 2007
Posts: 49
Location: Texas

PostPosted: Wed Jan 30, 2008 6:24 pm    Post subject: row has no indexed child elements
Note that this control is a listview (ListView32) as I discovered through the spy.

I have the following code:


Code: click into code to enlarge

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
Back to top
View user's profile Send private message
jasong



Joined: 26 Oct 2007
Posts: 49
Location: Texas

PostPosted: Wed Jan 30, 2008 7:11 pm    Post subject: how can I select an item of a ListView by index and not name
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: click into code to enlarge

    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.
Back to top
View user's profile Send private message
Support Team
Site Admin


Joined: 07 Jul 2006
Posts: 256

PostPosted: Thu Jan 31, 2008 12:20 pm    Post subject:
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
Back to top
View user's profile Send private message Visit poster's website
jasong



Joined: 26 Oct 2007
Posts: 49
Location: Texas

PostPosted: Thu Jan 31, 2008 9:42 pm    Post subject: now I am cooking with gas
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Ranorex Forum Index -> RanorexPython All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum