Definition of Element.Valid

Class library usage, coding and language questions.
Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Definition of Element.Valid

Post by Marianne Jacobsen » Fri Nov 27, 2009 2:43 pm

The documentation for Element.Valid reads "Returns true if the element is believed to be valid." what exactly is the definition of Valid here?

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

Re: Definition of Element.Valid

Post by Support Team » Fri Nov 27, 2009 5:24 pm

The value of Adapter/Element.Valid indicates whether the actual UI element, which the Adapter or Element instance corresponds to, still exists. E.g. if for a Form adapter's Valid property is false, then the form is no longer there (most probable because the form has been closed).
Consequently, all data retrieved from the Adapter/Element instance are not valid any more and will return default values; invoking an action on the adapter may cause an exception.

Regards,
Alex
Ranorex Support Team

Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Re: Definition of Element.Valid

Post by Marianne Jacobsen » Mon Nov 30, 2009 12:46 pm

Okay that was I was hoping for. But unfortunately I have some problems with it.

I have a control on which I poll for the value of a property (using GetPropertyValue). After a while the method call throws an exception with

Code: Select all

[Ranorex.ActionFailedException] = {"Action 'getpropertyvalue' failed on element '{Unknown:ProgramButton0}'."} 
and inner exception

Code: Select all

InnerException = {"The control does no longer exist."}
If I add a check for control.Valid right before calling GetPropertyValue - it returns true. Like:

Code: Select all

if(control.Valid) //returns true
   object value = control.GetPropertyValue("SomeProperty"); //throws exception
What am I doing wrong here?

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

Re: Definition of Element.Valid

Post by Support Team » Mon Nov 30, 2009 5:10 pm

Hmmm, it seems like the communication to that control fails, i.e. if the control does not react to the window message Ranorex sends to it. Usually, if Valid is true, the Control.GetPropertyValue method should work as well (provided that the specified property exists).
  • Is that exception thrown everytime you access the control and for every property?
  • Are you working on a 64 bit operating system? If so, please make sure that the automating and the automated process have the same bit width (see http://www.ranorex.com/support/user-gui ... forms.html)!
  • That could be a security problem as well. Do you start the automated/automating process from a network share, from an encrypted folder or from somewhere inside the "Users"/"Documents and Settings" folder? Try starting the applications in another folder!
Regards,
Alex
Ranorex Support Team

Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Re: Definition of Element.Valid

Post by Marianne Jacobsen » Tue Dec 01, 2009 9:46 am

The exception is thrown after awhile - I'm uncertain whether the control actually does cease to exist. I'm not running 64bit, network share or encrypted drive.

The control in question is a custom one (but it inherits from UserControl) could this be the reason why it's not responding to the messages that Ranorex send?

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

Re: Definition of Element.Valid

Post by Support Team » Tue Dec 01, 2009 11:30 am

Marianne Jacobsen wrote:The exception is thrown after awhile - I'm uncertain whether the control actually does cease to exist.
I assume that is the case, but I don't know why the Valid property is still true :?
Marianne Jacobsen wrote:The control in question is a custom one (but it inherits from UserControl) could this be the reason why it's not responding to the messages that Ranorex send?
No, that should not be a problem. The Control adapter (and the Control.InvokeRemotely method in particular) are designed to work with custom .NET Windows Forms controls.

What version of Ranorex do you use?

Regards,
Alex
Ranorex Support Team

Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Re: Definition of Element.Valid

Post by Marianne Jacobsen » Tue Dec 01, 2009 1:37 pm

We are using version 2.2 - which I assume is the newest one.

So far I've created a workaround for it - but it would be nice to have control.Valid return false.

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

Re: Definition of Element.Valid

Post by Support Team » Wed Dec 02, 2009 10:18 am

Actually, I could not reproduce a case where Valid is false for a .NET Windows Forms control and the control cannot be found.
  • What kind of control does that happen with?
  • Are there certain steps that have to be taken to cause that behavior?
  • It would be nice to know, whether the control is visibly there or not; i.e. if you can still interact with it, after that problem occurs.
  • In that custom UserControl, do you know whether the control does something special with its corresponding window handle or in its Dispose method?
It could be a timing issue as well, e.g. when Valid is evaluated, the control is still there. But when you try to get a property value from it, that fails because the control disappeared right between the call to Valid and the GetPropertyValue call.

Regards,
Alex
Ranorex Support Team

Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Re: Definition of Element.Valid

Post by Marianne Jacobsen » Wed Dec 02, 2009 2:01 pm

I'll give a little background information the things I'm trying to achieve. Note that until recently our code was running off Ranorex 1.5.1 in which the scenario worked.

I've created a class the retrieves controls/elements for specific testing purposes. In reality this is just a wrap around "element = someXpathExpression" which in turn ensures that the control is visible, enabled, set timeouts and so forth.

Then when executing test I sometimes need to wait for control to reach a specific state before continuing with the test. The application-under-test consists of all .Net custom controls (they inherit from Control or UserControl). Lets call this control "customButton" - now this customButton has a public property called "customText" (and sometimes are more complex stucture for which I use invokeRemotely. e.g. it has a property which returns an non-serializable custom object).

I then want to wait for customButton.customText to change from "blabla" to "albalb". I retrieve the control from the xpathexpression and return it. Then I start to poll the customButton.customText using some reasonable interval and GetPropertyValue. When the text becomes "albalb" I break and continue to click for instance.

Usually the exception is throw the first time I ask for customText after it has changed from "blabla" to "albalb". So all the time when it has the text is unchanged from when I queried it from Ranorex I have no problems. This might be where the control deleted and created again.

I do not actually know whether the custom control does something with its window handle or dispose. But I expect that they tear the control down behind the scenes and create it for reasons unknown to me.
I hope that cleared things up a little bit.

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

Re: Definition of Element.Valid

Post by Support Team » Thu Dec 03, 2009 5:14 pm

Marianne Jacobsen wrote:I'll give a little background information the things I'm trying to achieve. Note that until recently our code was running off Ranorex 1.5.1 in which the scenario worked.
Actually, the core code for providing access to .NET Windows Forms control properties in Ranorex V2.X is nearly the same as in V1.5.1. So, if this scenario worked with V1.5.1, it should also with V2.X.
Marianne Jacobsen wrote:Usually the exception is throw the first time I ask for customText after it has changed from "blabla" to "albalb". So all the time when it has the text is unchanged from when I queried it from Ranorex I have no problems. This might be where the control deleted and created again.
Does the method fail on following calls, too? So, if GetPropertyValue fails on the first time, does it work again (on the same control) after waiting for some time (e.g. 1 second)? What happens if you observe the control with Ranorex Spy while changing the text; does it change its handle ('Handle' attribute)?
The reason I ask that is: Ranorex tracks changes of the control's window handle that is used as identifier for communicating with the control. It might be that the handle of the control is re-created when you set the text and that Ranorex did not yet get informed of that change.

Could you provide us a sample application with that control, so we could analyze that behavior?

Regards,
Alex
Ranorex Support Team