Page 1 of 1

Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 8:52 am
by jainaakash
Hi Team,

I read the test script name to be executed from an excel sheet in a variable and then want to call the same script at runtime. Can you please guide me on how can I achieve this?

Earlier, I used to do it via Eval(str) but seems this is not available in vb.net.

eg:
str = "TestCases.TestCase1()"
Eval(str)

I tried another way to do this as below:
Dim sc As New MSScriptControl.ScriptControlClass()
sc.Language = "VBScript"
Dim str As String = "TestCases.TestCase1()"
sc.Eval (str)

Still, no luck and got the below error:
----
Unexpected exception occured: System.Runtime.InteropServices.COMException (0x800A01A8): Object required: 'TestCases'
at MSScriptControl.ScriptControlClass.Eval(String Expression)
----

What is the easy and best way to address this issue? Please guide.

Thanks.

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 9:11 am
by artur_gadomski
Maybe you can use a switch statement?
String testName = GetTestNameFromFile();
switch (testName) {
    case "Test1" : 
        Test1();
    case "Test2" :
        Test2();
    default:
        Report.Error("No test by the name: " + testName);
}
That would be easy way I guess.

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 9:27 am
by jainaakash
True, but have a very long list of tests and so, want to do it dynamically.
Thanks

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 10:10 am
by sdaly
Try this, I know there is an easier way of doing it but this is the only way I could get it to work. This is how I do it...

Private Sub startMethod(methodName As String)
Dim a As system.Reflection.Assembly = system.reflection.Assembly.GetExecutingAssembly
For Each t as system.Type In a.GetTypes
For Each m As system.Reflection.MethodInfo In t.GetMethods
If m.Name = methodName Then
Dim o As New Object
m.Invoke(o,Nothing)
End if
Next
Next
End Sub

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 10:39 am
by jainaakash
Thanks.
Yes, even this works fine. As you rightly said, there must be an easier way to do this. Here, the number of elements it has to verify is too much. We can use this till we get a better solution :)

Thanks

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 11:09 am
by sdaly
Instead of iterating through getTypes and getMethods, you should be able to use getType("moduleOrClassName") and getMethod("methodName")....I just can't get it to work! If you can post the solution!

Re: Calling Functions / Modules dynamically

Posted: Tue Aug 03, 2010 12:29 pm
by Support Team
Hi,
jainaakash wrote:We can use this till we get a better solution
You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. Therefore the solution with the reflection from sdaly is definitely the best solution to solve this problem.

Regards,
Peter
Ranorex Team

Re: Calling Functions / Modules dynamically

Posted: Wed Aug 04, 2010 11:52 am
by jainaakash
Thanks Peter.

I could not get that to work dynamically.. probably needs some coding expert :) If you can provide me the VB.NET code to implement, that would be great.

I have another issue here, in this case the method called does not have any parameters. If I want to invoke a method which takes some input parameters, the suggested code does not work. What changes do I need to make to the code to get it working?

Thanks and Regards,
Aakash

Re: Calling Functions / Modules dynamically

Posted: Wed Aug 04, 2010 1:11 pm
by sdaly
Try this...

Public Sub startMethod(methodName As String, parameters as object)
Dim a As system.Reflection.Assembly = system.reflection.Assembly.GetExecutingAssembly
For Each t as system.Type In a.GetTypes
For Each m As system.Reflection.MethodInfo In t.GetMethods
If m.Name = methodName Then
Dim o As New Object
m.Invoke(o,parameters)
End if
Next
Next
End Sub



Example use below passing a string and an int -

Dim parameters(1) As Object
parameters(0) = "test"
parameters(1) = 1
startMethod("Dev2Test",parameters)

Re: Calling Functions / Modules dynamically

Posted: Wed Aug 04, 2010 2:10 pm
by jainaakash
Thanks Buddy,
it worked perfectly

Re: Calling Functions / Modules dynamically

Posted: Wed Aug 04, 2010 4:15 pm
by sdaly
jainaakash, I take it you are doing keyword testing? If so take a look at - http://www.ranorex.com/forum/keyword-dr ... t1561.html

Re: Calling Functions / Modules dynamically

Posted: Fri Aug 06, 2010 10:35 am
by Liam
Great sdaly also helped me out of a similar problem - http://www.ranorex.com/forum/how-to-loo ... t1462.html :D

I've converted the code to c# and now there seems to be a problem with this line: m.Invoke(g,null);

Invoke will catch all exceptions thrown by the target method, wrap them in a TargetInvocationException, and then rethrow that. So I will never get an RanorexException to the report if there is one...

Any idea for this?

Thanks,
Liam

Re: Calling Functions / Modules dynamically

Posted: Fri Aug 06, 2010 10:55 am
by Support Team
You can get the actual exception that happend in the invoke from the TargetInvocationException.InnerException property (see MSDN documentation). You can, for example, catch the TargetInvocationException and re-throw it's inner exception (the actual RanorexException):
try
{
    m.Invoke(g, null);
}
catch (TargetInvocationException ex)
{
    throw ex.InnerException;
}
Regards,
Alex
Ranorex Team

Re: Calling Functions / Modules dynamically

Posted: Fri Aug 06, 2010 11:06 am
by Liam
Thank you Alex, for your quick response. I will try that next week. Have a good weekend!

Liam