how to use returned result from Script in Keyword test
Hi All,
I wrote one script for getting result from DB. Below is my code:
function query (sqlQuery,Connectionstring) {
var dbObj = ADO.CreateADOQuery();
dbObj.ConnectionString = Connectionstring
dbObj.SQL = sqlQuery;
dbObj.Open();
queryResult = dbObj;
return queryResult;
}
Query Result:
col 1 | col 2 | col 3 | col 4 | col 5 |
data 1 | data 2 | data 3 | data 4 | data 5 |
Now I need to call this method in keyword test and need to use the values to pass like parameter in another method.
I tried to store by using set variable value but it is not working.
Can anyone help me to slove this.
Thnaks in advance.
Regards,
saritha.
If i want to run application in Environment 1 to environment 2, in one place i can change the connection
Consider storing your connection string as a variable on the Project level, so your KWT won't always have to send the connection string as a parameter each time you call the function. Note the RunQuery looks different. It's a rough translation of what I would do with ADO. There is also a second parameter that can be sent with an array, so you can parametrise your queries
function RunQuery(sqlQuery, params) { ADOConnection = OpenConnection(); //Function that does work to open an return a connection with some checking if a connection is already open etc. Excercise for the reader
if(ADOConnection==null)
throw("ADO Connection Null");
ADOCommand = ADO.CreateCommand();
ADOCommand.ActiveConnection = ADOConnection; // Connection
ADOCommand.CommandType = CmdText;
ADOCommand.CommandText = sqlQuery;
if(params!= null && params.length>0)
{
for(var i=0;i<params.length;i++)
{
ADOCommand.Parameters.Item(i) = params[i];
}
}
var returnValue = ADOCommand.Execute();
ADOConnection.Close();
return returnValue; }You can then maintain the value of the project variable in a convenient, central place. You may find that your SQL queries get too long/complex, so perhaps seperate them into different functions in a DBQueries script and call that from your KWT:
function GetAllUsers(){ return RunQuery("Select * from [Users]"); } function GetOneUser(userID){ return RunQuery("Select * from [Users] where userID = ?", [userid]); }
The syntax and usages are approximate. You will have to tinker till it works for you