Calling Functions / Modules dynamically

Class library usage, coding and language questions.
jainaakash
Posts: 48
Joined: Thu Jun 10, 2010 12:06 pm

Calling Functions / Modules dynamically

Post by jainaakash » Tue Aug 03, 2010 8:52 am

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.

User avatar
artur_gadomski
Posts: 207
Joined: Mon Jul 19, 2010 6:55 am
Location: Copenhagen, Denmark
Contact:

Re: Calling Functions / Modules dynamically

Post by artur_gadomski » Tue Aug 03, 2010 9:11 am

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.

jainaakash
Posts: 48
Joined: Thu Jun 10, 2010 12:06 pm

Re: Calling Functions / Modules dynamically

Post by jainaakash » Tue Aug 03, 2010 9:27 am

True, but have a very long list of tests and so, want to do it dynamically.
Thanks

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Calling Functions / Modules dynamically

Post by sdaly » Tue Aug 03, 2010 10:10 am

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

jainaakash
Posts: 48
Joined: Thu Jun 10, 2010 12:06 pm

Re: Calling Functions / Modules dynamically

Post by jainaakash » Tue Aug 03, 2010 10:39 am

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

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Calling Functions / Modules dynamically

Post by sdaly » Tue Aug 03, 2010 11:09 am

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!

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Calling Functions / Modules dynamically

Post by Support Team » Tue Aug 03, 2010 12:29 pm

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
.
Image

jainaakash
Posts: 48
Joined: Thu Jun 10, 2010 12:06 pm

Re: Calling Functions / Modules dynamically

Post by jainaakash » Wed Aug 04, 2010 11:52 am

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

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Calling Functions / Modules dynamically

Post by sdaly » Wed Aug 04, 2010 1:11 pm

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)

jainaakash
Posts: 48
Joined: Thu Jun 10, 2010 12:06 pm

Re: Calling Functions / Modules dynamically

Post by jainaakash » Wed Aug 04, 2010 2:10 pm

Thanks Buddy,
it worked perfectly

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Calling Functions / Modules dynamically

Post by sdaly » Wed Aug 04, 2010 4:15 pm

jainaakash, I take it you are doing keyword testing? If so take a look at - http://www.ranorex.com/forum/keyword-dr ... t1561.html

Liam
Posts: 16
Joined: Wed Mar 31, 2010 7:45 am

Re: Calling Functions / Modules dynamically

Post by Liam » Fri Aug 06, 2010 10:35 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Calling Functions / Modules dynamically

Post by Support Team » Fri Aug 06, 2010 10:55 am

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
.
Image

Liam
Posts: 16
Joined: Wed Mar 31, 2010 7:45 am

Re: Calling Functions / Modules dynamically

Post by Liam » Fri Aug 06, 2010 11:06 am

Thank you Alex, for your quick response. I will try that next week. Have a good weekend!

Liam