Focus()/HasFocus doesn't work

Ask general questions here.
John Silver, Esq.
Posts: 5
Joined: Tue Mar 15, 2011 6:28 pm

Focus()/HasFocus doesn't work

Post by John Silver, Esq. » Mon Apr 04, 2011 1:14 pm

Hi,

I have the application, which shows the dialog window to let user to edit item's properties.
It is important to be sure that predefined control on this dialog is focused.

The following code is used to find the dialog window:

Code: Select all


Container dlg=null;
Element field=null;
string path="/form/element[@class='_MY_WINDOW_CLASS_']/container[@accessiblename~'^MyPrefix.*']";
if (Host.Local.TryFindSingle(path,10000,out dlg))
{
  if (dlg.TryFindSingle("bla-bla-bla",10000,out field)
  {
    EnsureFocused(field); <--see below
  }
}
It is impossible to use repository, because the "_MY_WINDOW_CLASS_" and "MyPrefix" are not constants, they can be dynamically changed during testing (they are taken from external database).

When I get the dialog's container and the field instances (I've got them always, that's not a problem), I perform checking of the focus:

Code: Select all

void EnsureFocused(Element item)
{
  while(Item.HasFocus==false)
  {
    item.Focus();
    Delay.Milliseconds(100);
  }
}
So, the problem in that while loop is works only for the first opened dialog. After performing of an other tests, it is closed and re-created again. After this, the item.HasFocus always return "false", even I set the focus to the item manually.

May I ask You, please, to help me to solve this problem?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Focus()/HasFocus doesn't work

Post by Support Team » Mon Apr 04, 2011 5:57 pm

John Silver, Esq. wrote:So, the problem in that while loop is works only for the first opened dialog. After performing of an other tests, it is closed and re-created again. After this, the item.HasFocus always return "false", even I set the focus to the item manually.
Did you try tracking the item with Ranorex Spy when the dialog opens for the second time? What's the value of HasFocus in Ranorex Spy then?
You can see the value of HasFocus in the "Detail" tab page under category "Layout". To get the correct result (Ranorex Spy must not have focus, but the actual item), click on the "HasFocus" field in the property grid before tracking and then use instant tracking (hit Ctrl+LWin keys) on the item while Ranorex Spy is visible.

If Ranorex Spy does not show the expected value, could you then provide us a sample application with the problematic dialog?

Regards,
Alex
Ranorex Team

John Silver, Esq.
Posts: 5
Joined: Tue Mar 15, 2011 6:28 pm

Re: Focus()/HasFocus doesn't work

Post by John Silver, Esq. » Tue Apr 05, 2011 8:26 am

Did you try tracking the item with Ranorex Spy when the dialog opens for the second time? What's the value of HasFocus in Ranorex Spy then?
Yes, I did it. The HasFocus property (in the spy details view) is "true". By the way, very often I've got the situation when spy does see any changes, but the test application doesn't.
May be, I need to set/change some caching discipline for the test application?
If Ranorex Spy does not show the expected value, could you then provide us a sample application with the problematic dialog?
Unfortunately I can't provide this application - it is very heavy and uses very special database to work.

One more thing - in the tested application I've added logging for the set/lost focus events, so, the WM_SETFOCUS (as a result of .Focus() call) is coming to the my control only for the first instance of the dialog window, after this it doesn't.

P.S. Sorry for my English ;), my native languages are Ukrainian/Russian.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Focus()/HasFocus doesn't work

Post by Support Team » Tue Apr 05, 2011 5:16 pm

John Silver, Esq. wrote:Yes, I did it. The HasFocus property (in the spy details view) is "true". By the way, very often I've got the situation when spy does see any changes, but the test application doesn't.
If Ranorex Spy shows the correct value, the elements in your code should also work correctly (Ranorex Spy does not do anything special). I got the feeling that you still use the same element instance for retrieving information from the second dialog. Is the "Valid" property of the element still returning true?

When the second dialog opens, you have to search for the "field" element again before you can retrieve information from it, e.g. the value of the "HasFocus" property. Do you always execute all of the code you posted above or just set the "field" element variable once and from then on use the "field" variable?
John Silver, Esq. wrote:It is impossible to use repository, because the "_MY_WINDOW_CLASS_" and "MyPrefix" are not constants, they can be dynamically changed during testing (they are taken from external database).
With Ranorex 3.X that can also very easily be done with variables in your repository. Have a look at the Ranorex User Guide:
http://www.ranorex.com/support/user-gui ... html#c2970

Regards,
Alex
Ranorex Team

John Silver, Esq.
Posts: 5
Joined: Tue Mar 15, 2011 6:28 pm

Re: Focus()/HasFocus doesn't work

Post by John Silver, Esq. » Fri Apr 08, 2011 9:26 am

Support Team wrote: I got the feeling that you still use the same element instance for retrieving information from the second dialog.
That's impossible, because my workflow is:
1) do the Host.Local.TryFindSingle(path,10000,out dlg) (as I understand, it always should reflect the current state?")
2) do the dlg.TryFindSingle("bla-bla-bla",10000,out field)
3) call the EnsureFocused(field)

So, from my point of view, in my code there is no "caching" mechanism, and I can't got the "old" instance of dlg or field. It seems, that testing application uses some internal cache to execute the Host.Local.TryFindSingle(path,10000,out dlg).
Support Team wrote:Is the "Valid" property of the element still returning true?
Which one?
Support Team wrote:Do you always execute all of the code you posted above or just set the "field" element variable once and from then on use the "field" variable?
Yes, see above.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Focus()/HasFocus doesn't work

Post by Support Team » Mon Apr 11, 2011 5:56 pm

John Silver, Esq. wrote:It seems, that testing application uses some internal cache to execute the Host.Local.TryFindSingle(path,10000,out dlg).
Unless there is a CacheSessionContext used anywhere in your code, then Ranorex does not cache any values. Basically, if you always search the item anew, the value of HasFocus should be up-to-date.

Please, check whether the Valid property is true for the item:
bool isValid = item.Valid;
And I just glimpsed a weird thing in your code snippet, where you use two different variables called "item" and "Item" (capital "i"). Could that be a variable pointing to a different element?

Regards,
Alex
Ranorex Team

John Silver, Esq.
Posts: 5
Joined: Tue Mar 15, 2011 6:28 pm

Re: Focus()/HasFocus doesn't work

Post by John Silver, Esq. » Mon Jun 06, 2011 11:10 am

Hi,

sorry for the late answer.
So, the problem disappeared with new version of the Ranorex (now I'm using 3.0.2.12718).

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Focus()/HasFocus doesn't work

Post by Support Team » Mon Jun 06, 2011 11:13 am

John Silver, Esq. wrote:the problem disappeared with new version of the Ranorex (now I'm using 3.0.2.12718)
Good to hear that :D

Regards,
Alex
Ranorex Team