Forum Discussion

nakshatra's avatar
nakshatra
Contributor
5 years ago
Solved

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

     

8 Replies

  • What Type is your Variable you are trying to set ?  You may need to create your variable as a Type of Object.  You can send this variable to another script, but your other script will need to know how to interpret it.  In other words, it needs to know what the data is going to look like.  And of course be able to handle if the variable value is null or undefined

    • nakshatra's avatar
      nakshatra
      Contributor

      Thanks for the reply.

       

      In my code return type is var. i am returning dbObj as a result. Now how to store the result?

      Can you explain me little bit detail how to store and how to use in Keyword test.

       

      function query (sqlQuery,Connectionstring) { 

      var dbObj = ADO.CreateADOQuery(); 

      dbObj.ConnectionString =  Connectionstring; 

      dbObj.SQL = "Select top 5 col1, col2, col3,col4,col5 from table";  

      dbObj.Open(); 

      return dbObj ; 

      } 

      • RUDOLF_BOTHMA's avatar
        RUDOLF_BOTHMA
        Community Hero

        Good start in Support documentation:

         

        Working With Project and Project Suite Variables in Keyword Tests

        and

        Working With Project and Project Suite Variables in Scripts

         

        Quick overview:

         

        Create the required scripts.  One returns a variable, one accepts a variable and does something with it

         

         

        In your KWT, add the Run Script routine function

         

         

         

        Set your local variable to the last result

         

         

        Add the ConsumeObject Test Script and run the KWT:

         

         

        Et voila !

         

         

        There is nothing stopping you from using the local variable in code expressions in the KWT itself.  It isn't limited to only scripts.  You just need to make sure you know what type/structure your variable is.  This is all just a psuedo example though.  In a scenario like this, I wouldn't use the KWT to get a value from a script and pass it to another script.  I would just create a single script, which could in turn call the second script directly and cut out the KWT as middleman