Exception Problem

Bug reports.
CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Exception Problem

Post by CookieMonster » Wed Dec 27, 2006 9:26 pm

Hi I tried handle Exceptions with the Ranorex Pro Version, but I have a few problems.
If I activate Application.ErrorAsException = true; it work fine for the first 3 controls but on the 4th control ranorex throws an Exception, but if deactivate Application.ErrorAsException = true; everthing works fine.
The 4th control will be found without problems.

Best Regards Dan

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Thu Dec 28, 2006 12:56 am

CookieMonster wrote:but on the 4th control ranorex throws an Exception
Please post your sample code, i would like to know which function throws an exeption.

Jenö Herget
Ranorex Team

CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Post by CookieMonster » Thu Dec 28, 2006 6:54 pm

Hi Jenö,


Here is the Method which throws the exception, the follwing Line trows the exception: ctrl = this.frmToAutomate.FindControlName(strElementName);
I think I know where the problem is, during the coding I had never the Exceptions activatet and for that I didn't reconize this problem. I also

Regards Dan
The Method which I used during coding:

Code: Select all

 public Ranorex.Element GetElement(String strElementName)
        {
            Element element = null;
            Control ctrl = null;            
            try
            {
                this.frmToAutomate.GetFocus();
                this.frmToAutomate.GetControls();
                if (this.frmToAutomate != null)
                {
                    ctrl = this.frmToAutomate.FindControlName(strElementName);
                    if (ctrl != null)
                    {
                        element = ctrl.Element;                       
                    }
                    else
                    {
                        foreach (Control ctrlElement in this.frmToAutomate.Controls)
                        {
                            if(ctrlElement.Element.Name.Equals(strElementName))
                            {
                                element = ctrlElement.Element;
                                break;
                            }
                        }
                    }                   
                }                
            }
            catch (RanorexException e)
            {
                throw e;
            }
            return element;
        }
I changed the to the following code, but its still not working:

Code: Select all

 public Ranorex.Element GetElement(String strElementName)
        {
            Element element = null;
            Control ctrl = null;
            try
            {
                this.frmToAutomate.GetFocus();
                this.frmToAutomate.GetControls();
                if (this.frmToAutomate != null)
                {
                    ctrl = this.frmToAutomate.FindControlName(strElementName);
                    element = ctrl.Element;                    
                }
            }
            catch (RanorexException e)
            {
                throw e;
            }
            finally
            {
                foreach (Control ctrlElement in this.frmToAutomate.Controls)
                {
                    if (ctrlElement.Element.Name.Equals(strElementName))
                    {
                        element = ctrlElement.Element;
                        break;
                    }
                }
            }
            
            return element;
        }
What I'm doing wrong, its not an Bug ??

Best Regards Dan

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Thu Dec 28, 2006 8:24 pm

I think the problem is, that you try to find a control with an element name.
If you use the Form.FindControlName function, than you should call the function with the "Control name" property.
Use RanoreySpy to get the control names. In the sample application VS2005Application.exe the control name of the first radio button is "radioButton1" but the element name is "radio1".
You can find the control with Form.FindControlName("radioButton1"), but Form.FindControlName("radio1") throws an exeption.
If you want to find an element in the control, than you should use control.Element.FindChild(role, elementName).

The following code (from the sample application RanorexVS2005Sample3) searches the checked list box control with the control name "checkedListBox1" in the form and then the element "Item2" in the control.

Code: Select all

Control checkedListBox = form.FindControlName("checkedListBox1");

Element element = checkedListBox.Element.FindChild(Role.CheckButton,"Item2",null);
if (element != null)
{
  element.Select(Selection.TakeFocus|Selection.TakeSelection);
}
Please try it out and inform me about the results.

Jenö Herget
Ranorex Team