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.
Calling Functions / Modules dynamically
- artur_gadomski
- Posts: 207
- Joined: Mon Jul 19, 2010 6:55 am
- Location: Copenhagen, Denmark
- Contact:
Re: Calling Functions / Modules dynamically
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.
-
- Posts: 48
- Joined: Thu Jun 10, 2010 12:06 pm
Re: Calling Functions / Modules dynamically
True, but have a very long list of tests and so, want to do it dynamically.
Thanks
Thanks
Re: Calling Functions / Modules dynamically
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
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
-
- Posts: 48
- Joined: Thu Jun 10, 2010 12:06 pm
Re: Calling Functions / Modules dynamically
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
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
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!
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Calling Functions / Modules dynamically
Hi,
Regards,
Peter
Ranorex Team
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.jainaakash wrote:We can use this till we get a better solution
Regards,
Peter
Ranorex Team
-
- Posts: 48
- Joined: Thu Jun 10, 2010 12:06 pm
Re: Calling Functions / Modules dynamically
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
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
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)
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)
-
- Posts: 48
- Joined: Thu Jun 10, 2010 12:06 pm
Re: Calling Functions / Modules dynamically
Thanks Buddy,
it worked perfectly
it worked perfectly
Re: Calling Functions / Modules dynamically
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
Great sdaly also helped me out of a similar problem - http://www.ranorex.com/forum/how-to-loo ... t1462.html
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
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
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Calling Functions / Modules dynamically
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):
Alex
Ranorex Team
try { m.Invoke(g, null); } catch (TargetInvocationException ex) { throw ex.InnerException; }Regards,
Alex
Ranorex Team
Re: Calling Functions / Modules dynamically
Thank you Alex, for your quick response. I will try that next week. Have a good weekend!
Liam
Liam