Here, we will be discussing on handling the dynamic function call in TestComplete.
Consider a scenario where you perform the steps like
scenario 1 will have a flow like below:
1)Login
2)createPlan
3)addItem1ToPlan
4)savePlan
5)Logout
To handle this scenario, we will have created a function to perform each step and call them in the above order, which will ultimately create a scenario, as shown.
In most automation projects, when functionalities are grouped together, almost 60% of functions will have the same flow. To put it more clear,
scenario 2 will have a flow like below:
1)Login
2)createPlan
3)addItem 2ToPlan
4)savePlan
5)Logout
So, rather than calling the base functions again and again, you can call the expected functions dynamically, with the same flow,which avoids code repeatation and optimizes it.Below the strategy for the same
Generalized Function:
//here, get the expected function name from excel
//var FunctionName="addItem 2ToPlan"
1)Login
2)createPlan
3)eval(FunctionName)
4)savePlan
5)Logout
Problems with eval():
Passing object as parameter to function called by eval().....
Though, eval() in Javascript handles the problem of dynamically calling the expected functions.
When, the Function that has to be called contains Strings as parameters, eval() will work fine.
But, when the parameter for the function to be called contains objects, eval will not work. To overcome this, in TestComplete, we can make use of Project variables. The project variables can contain variables of any type(String,integer and object) that you will be needing throughout your project.(These, are similar to Environment variables in QTP).
So, add the required object that has to be passed as a function parameter to eval, in Project variables and receive the variable in the function definition , which will be a good work around.
ex:
/*here, get the expected function name from excel, if the param2 is an object,add it to project variables at the start*/
var FunctionName="addItem 2ToPlan"+"('"+param1+"');";
1)Login
2)createPlan
3)eval(FunctionName)
4)savePlan
5)Logout
In function definition:
function addItem 2ToPlan{
var ExcelObj;ExcelObj=Project.Variables.VariableByName("param2");
//now proceed further
}
Hope this helps :-)
Consider a scenario where you perform the steps like
scenario 1 will have a flow like below:
1)Login
2)createPlan
3)addItem1ToPlan
4)savePlan
5)Logout
To handle this scenario, we will have created a function to perform each step and call them in the above order, which will ultimately create a scenario, as shown.
In most automation projects, when functionalities are grouped together, almost 60% of functions will have the same flow. To put it more clear,
scenario 2 will have a flow like below:
1)Login
2)createPlan
3)addItem 2ToPlan
4)savePlan
5)Logout
So, rather than calling the base functions again and again, you can call the expected functions dynamically, with the same flow,which avoids code repeatation and optimizes it.Below the strategy for the same
Generalized Function:
//here, get the expected function name from excel
//var FunctionName="addItem 2ToPlan"
1)Login
2)createPlan
3)eval(FunctionName)
4)savePlan
5)Logout
Problems with eval():
Passing object as parameter to function called by eval().....
Though, eval() in Javascript handles the problem of dynamically calling the expected functions.
When, the Function that has to be called contains Strings as parameters, eval() will work fine.
But, when the parameter for the function to be called contains objects, eval will not work. To overcome this, in TestComplete, we can make use of Project variables. The project variables can contain variables of any type(String,integer and object) that you will be needing throughout your project.(These, are similar to Environment variables in QTP).
So, add the required object that has to be passed as a function parameter to eval, in Project variables and receive the variable in the function definition , which will be a good work around.
ex:
/*here, get the expected function name from excel, if the param2 is an object,add it to project variables at the start*/
var FunctionName="addItem 2ToPlan"+"('"+param1+"');";
Project.Variables.AddVariable("param2", "Object");
Project.Variables.param2=ExcelObj;//some excel object, that you have got.1)Login
2)createPlan
3)eval(FunctionName)
4)savePlan
5)Logout
In function definition:
function addItem 2ToPlan{
var ExcelObj;ExcelObj=Project.Variables.VariableByName("param2");
//now proceed further
}
Hope this helps :-)