kroe761
10 years agoContributor
Call a function as a parameter from another function?
I am maintinaing a lot of legacy test code, and it uses the Runner.CallMethod() function to call methods from other files. One of the advantages here is that CallMethod uses a string as parameters, so calling a bunch of different functions in a for loop is easy:
function test() { functions = new Array("button_one", "button_two", "button_three") for (i = 0; i < functions.length; i++) { button = Runner.CallMethod("My_File." + functions[i]) button.Click() }
However, I am writing a bunch of new tests for a new application. I don't want to use deprecated codes, but I really like the functionality above. Is there any way I can do this using USEUNIT? Something like this:
//USEUNIT My_File function test() { functions = new Array(button_one, button_two, button_three) for (i = 0; i < functions.length; i++) { button = My_File.functions[i]() button.Click() } }
Or maybe this (actually what I would prefer)
//USEUNIT My_File function test(function_name) { button = My_File.function_name() button.Click() } test(button_one) test(button_two) test(button_three)
Thanks a lot!
Functions that reside in other scripts, and that are linked with USEUNIT, effectively become part of the current script.
You can call them "dynamically" as in your first example like so:
//USEUNIT My_File function test(){ var functions = ["button_one", "button_two", "button_three"] for (var i = 0; i < functions.length; i++){ var button = My_File[functions[i]](); button.Click() } }