Novari-QA
8 years agoFrequent Contributor
Call function from string value JavaScript
I was hoping that TestComplete offered a method in which we can dynamically call specific functions based off of a string variable.
For example: This is how I would do it in C#. Unfortunately I am not sure if JavaScript has something similar, or even if TestComplete offers it.
using System; using System.Reflection; /// <summary> /// Will invoke the method passed in. Then, it will return the generic type used in the function /// Example: InvokeMethod<int>("methodName", "param"); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="methodName"></param> /// <param name="stringParam"></param> /// <returns></returns> public static T InvokeMethod<T>(string methodName, string stringParam) { return (T)typeof(T).InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new Object[] { stringParam }); }
Thoughts?
There are a few ways.
1) Square bracket notation:
var method = "Message";
Log[method]("test");2) $call:
Log.$call("Message", "test");
aqObject.CallMethod(Log, "Message", "test");