Forum Discussion

nakshatra's avatar
nakshatra
Contributor
6 years ago

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 = Conn...
  • RUDOLF_BOTHMA's avatar
    RUDOLF_BOTHMA
    6 years ago

    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