Page 1 of 1

How can i implement a process like this

Posted: Mon Apr 27, 2009 8:48 am
by marcushe
Working Environment:win7
What I want Ranorex to do is when encounter a exception , i deal with exception in Catch block then return to the line where i meet the exception and go on the next line.
As we know ,each line recorded maybe meet an exception during replay, but it 's a mess to surround each line with Try..catch
.....
Try
{
do something1
do something2
}
Catch()
{
solve error
go back to do something2
}

Posted: Mon Apr 27, 2009 4:30 pm
by Ciege
Try breaking your code down more into reusable functions. Within the functions surround with try/catch blocks and return results to the caller (i.e. 1 if it passes or 0 if it fails). Then in your calling blocks deal with the potential errors and continue...

psudo code:

Code: Select all

main
  call func1
  if func1 = 1 
    call func 2
end main

func1
 try
  do sommething
 catch
end func1

func2
 try
  do sommething
 catch
end func2

Not so easy to deal with Try..catch

Posted: Tue Apr 28, 2009 3:50 am
by marcushe
Consider your action like below:
1. Activate Application form
2. click button1,popup a dialog
3. click ok
When you replay, you may encounter
1. Application form not exist
2.button1 not exist
3.dialog not exist
4.ok not exist
Then ,how do you orgonize your script?
what would you do if your script contain 20 forms and buttons, your Try...Catch block will overwhelm you

Posted: Wed Apr 29, 2009 2:40 pm
by Support Team
If you want to response to an error, there`s no other solution than catching the error.
In most cases, for every element which throws an ElementNotFoundException you have do react different to solve a problem.

But if you want to go to the next step when an exception occurs and don`t want to have to much try...catch blocks you can use a delegate list to call your automation functions.
e.g.

Code: Select all


public delegate void ActionDelegate();
//...
//...

//class Program
System.Collections.Generic.IList<ActionDelegate> list = new System.Collections.Generic.List<ActionDelegate>();
            	
list.Add(Recording1.Start);
list.Add(Recording2.Start);
list.Add(lklklewrwqerqeRepository.Instance.FormBin.TitleBarBin.Click);
                
foreach(ActionDelegate action in list)
{
  try
  {
     // call function
     action();
   }
   catch(ElementNotFoundException)
   {
      // handle error
                		
      continue;
   }
}
Regards,
Christian
Ranorex Support Team

Re: Not so easy to deal with Try..catch

Posted: Wed Apr 29, 2009 4:37 pm
by Ciege
marcushe wrote:Consider your action like below:
1. Activate Application form
2. click button1,popup a dialog
3. click ok
When you replay, you may encounter
1. Application form not exist
2.button1 not exist
3.dialog not exist
4.ok not exist
Then ,how do you orgonize your script?
what would you do if your script contain 20 forms and buttons, your Try...Catch block will overwhelm you
What I would do (and have done) is functionalize your scripts to create a reusable framework. Create an application launch function that returns a true/false if the app was launched. Same thing for searching for a clicking a button & checking for dialogs.

As you can see from my example code below, you need to create reusable functions or methods to do all your work for you by passing in different variables. That way the try/catch blocks are already done for you when you need to do a specific thing in your code.

You should be able to see how you can expand all the methods and create your own reusable framework that will work across all of you different possible automation tests (i.e. enter text into a text box, select a combo item in a combo box, click a hyper link in IE, etc...). Save this framework off as a DLL or some other includable code so that if you need to make a modification to it you only do that in one place, one time and all of your scripts that reference it are updated automatically.


I.e. - Pseudo Code

Code: Select all

Main
  //Launch AUT
  intResult = LaunchAUT
  if intResult = FALSE
    { failure}

  //Click Button1 on Form1
  intResult = ClickButton(("DialogButtonExistsOnName", "button1")
  if intResult = FALSE
    { failure}

  //Find Form1
  intResult = FindDialog("DialogName")
  if intResult = FALSE
    { failure}

  //Click OK on FormX
  intResult = ClickButton(("DialogButtonExistsOnName", "OK")
  if intResult = FALSE
    { failure}
END Main

Function LaunchAUT
{
  try
    { verify AUT path exists
       launch aut
       return true
    }
  catch
    {
        return false
    }
}

Function ClickButton("DialogButtonExistsOnName", "buttonName")
{
  try
    { intResult = FindDialog("DialogName")
       verify button exists
       click button
       return true
    }
  catch
    {
        return false
    }
}

Function FindDialog
{
  try
    { verify dialog exists
       return true
    }
  catch
    {
        return false
    }
}