Is there a way to refresh a repo item?

Class library usage, coding and language questions.
Posts: 3
Joined: Wed Jan 20, 2016 11:04 pm

Is there a way to refresh a repo item?

Post by STPJmassey » Thu Jan 21, 2016 1:00 am

I am trying to look at the checkbox of an item in a table that is taller than the container it is in, so there is a scrollbar that you can scroll up and down. I would attach a snapshot of the program, but it exceeds the max upload size. I'll attach an image, however, so you can at least see the type of thing I'm talking about - items in this grid that are not currently visible just aren't detected.

If I try to access / act on a repo item or create a new element variable from path for one of the checkboxes not on screen when the page loads, it fails (ElementNotFoundException).
I can work around this by trying to re-create the element repeatedly while scrolling down a 'page' at a time.
public static bool GetCheckBoxElement(string HoldType, out CheckBox elem)
    string CheckBoxPath = repo.OrderEntry.Holds.GENERICHoldCheckBoxInfo.Path.ToString().Replace("HOLDTYPE", HoldType);
    ScrollBar sb = repo.OrderEntry.Holds.HoldsScrollBar;
    while(sb.Value != 0){sb.Click(Location.UpperCenter);}
    elem = null;
    while (elem == null & sb.Value < sb.MaxValue)
            elem = CheckBox.FromPath(CheckBoxPath);
            elem = null;            
    if (elem == null){return false;}
    else{return true;}

(The GENERICHoldCheckBox repo item lets me find checkboxes based on their following Hold Code - see screenshot).

However...If I scroll past the element at least once (which, as I say, I must do in order to access / act on / create a new variable from path of it in the first place), it seems that 'Visible' is set to 'True', and so it stays - even if it has since scrolled back out of view.

So, if there is any possibility that the checkbox may have scrolled off screen since it was assigned to a variable, I have to re-do that process (assigning it, including scrolling around until it's on screen if that fails) to make sure it's still visible, because if I try to just scroll until it's visible, it doesn't scroll - like I say, 'Visible' remains true, even though it's not actually on screen. If I try to tell it to click or check the checkbox, it will act on whatever checkbox is nearest to the location the element was at when it was assigned to a variable.

I can work around this just by re-doing the whole process of assigning it to a variable and scrolling if that fails (call the above code again), and then immediately (before it may scroll away again) checking or changing the state, but it would be nice for this and other similar sorts of cases I've run into if there were just a simple way to force ranorex to refresh a repo item to reset properties like existence, visibility, the location on screen, etc. It doesn't always seem to do this on its own when parts of the UI change, and it can be very frustrating when it claims something is visible when it's clearly not, or can't find a repo item because it wasn't visible when the screen first loaded, but is now.

Is there some built-in way of doing that (getting Ranorex to completely refresh / retry) that I just haven't noticed before? If not, have any of you found a nicer way of working around it? The code above does get the job done, but I kinda think it smells. Any help would be appreciated; I've run into similar issues to this a fair few times, now.
HoldsGrid.png (8.34 KiB) Viewed 3121 times

User avatar
Ranorex Guru
Ranorex Guru
Posts: 7062
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Is there a way to refresh a repo item?

Post by odklizec » Thu Jan 21, 2016 8:42 am


I'm not quite sure about refreshing repo item. In my opinion, if the element is out of the visible area, its visibility flag should automatically be switched to false. If it does not, then it's probably a bug in your app? Another possibility could be enabled folder caching in Ranorex properties?

Just go to the repository and examine the properties of parent folder of given checkbox. If the Use Cache option is True, switch it to False...
UseCache.png (6.33 KiB) Viewed 3109 times
BTW, have you tried using EnsureVisible method instead of clicking the scrollbar? While it's maybe not exactly user-based action, it works quite nice and brings the items outside the visible area back to visible area, without the need to use mouse-related actions. So in your code, I would suggest to use CheckBox.EnsureVisible(); instead of sb.Click(Location.LowerCenter);

As for the snasphot size, you can reduce its size by unchecking the Ranorex option "Let snapshot contain complete ancestor subtree"...
RxSettings.png (88.53 KiB) Viewed 3109 times
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Posts: 3
Joined: Wed Jan 20, 2016 11:04 pm

Re: Is there a way to refresh a repo item?

Post by STPJmassey » Thu Jan 21, 2016 7:11 pm

Hi, thanks for the reply!

- There isn't a parent folder in the repo that these items are under that the cache setting is available on. Is there somewhere else that I can set this, either in repo / repo item settings or in code?

- EnsureVisible causes ElementNotFound exception - like I said, until it's been scrolled into viewability at least once, as far as Ranorex seems to know, it doesn't exist, at all.
- If I scroll down to it within the test, or if I just manually scroll down so it's visible before starting the test run, EnsureVisible seems to work, but it's sort of pointless because it's already on screen.
- For some reason, today it is not doing the thing of clicking where it used to be if it's scrolled off screen, so that's a partial win, at least. EnsureVisible (or anything else, like Click) still causes an error 'Failed to find item' if it's not on screen, though:
// This works, if I arrange for the checkbox to be on screen before running this code

// Scroll the checkbox off screen...
ScrollBar sb = repo.OrderEntry.Holds.HoldsScrollBar;
while(sb.Value != sb.MaxValue){sb.Click(Location.LowerCenter);}

// ...And now this causes an error (Failed to find item)
I'm not sure what is different today, where yesterday EnsureVisible did nothing once it had come on screen, then left again, but it's at least an improvement - I can work with the exception, but the silent failure was killing me. Possibly it's because I'm working directly on a repo item, not an element I created from a path, so that's useful to be aware of, at least.

- That complete ancestor subtree option does not seem to have made the snapshot smaller.
- I tried getting a snapshot of just the container to make it small enough, but it seems to break the UI and the capture is wrong.

I'm attaching another image, though, that may help show what I'm talking about better, with the container open and one of the rows selected in Spy. You can see that there are far more rows in the grid (by the position of scrollbar / size of the thumb) than there are elements in the container in Spy. It's not that they're listed but invisible, they're just not there at all. Scroll until one is visible and you can do whatever with it, but once it scrolls out of view, it's like it doesn't exist anymore.

Thanks again for the help; with a night's rest and fresh eyes, I think this is less problematic than I thought, but I'd still like to get it fully nailed down, and especially try to find a less ugly way of working around this.
Grid02.png (152.97 KiB) Viewed 3097 times

Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Is there a way to refresh a repo item?

Post by krstcs » Thu Jan 21, 2016 7:47 pm

The element not existing is due to something called 'Lazy Loading'. Many frameworks do this on list-type UI items so they don't have to put the full object definition and layout in memory until the list/table is scrolled. This usually means that the only really objects are the ones that are visible plus maybe 2-3 on either top or bottom of the current ones.

You may need to add a scroll loop to your module that tries to find the element on the visible list, then scrolls the list down a page (send the page-down keyboard shortcut) and try again until the one you want is there.

Make sure your loop is a while-loop and use the <RepoItemInfo>.Exists() method inside the conditional statement, something like this:

Code: Select all

while (!myElementInfo.Exists() && <check for "last" list element here so you don't loop indefinitely>) {
  //scroll here

In addition, you might want to massage the XPath so that it uses indexes. We usually say to avoid them, but in this case you might want to add another XPath that points to indexed versions of the list items in order to facilitate the scrolling. Even with lazy loading, you should be able to to get the number of elements in the list's model and then use that to make sure you don't overrun the list or loop indefinitely.
Shortcuts usually aren't...