Forum Discussion

kamahade's avatar
Regular Contributor
14 years ago

Using Setup Script

I have following steps in my Single Testcase.

    Data source
    Create operation
    DB Checker
    Get operation
    Datasource Loop

I'm using Data source to fectch values from DB and feeding to my Create operation

And then doing Groovy script step to verify values fetched from DB

fetching of one of my response value from Create operation and feeding to get operation

Looping above 3 steps through Datasource loop step

Now, I want to create a setup script where I want to intialize DB connection. Since I do not want to open and close DB connection for every of my testcase. I tried placing following piece of code in setup script tab. However, while running my DB_Checker step, UI complains that it could NOT find property sql which is defined in setup script. ANY help will be useful

import groovy.sql.Sql;
def MA_UUID;
def groovyUtils = new context );

def sql = Sql.newInstance("jdbc:db2://IP_ADDRESS:60000/DB_NAME", "USERNAME","PASWORD", "");

2 Replies

  • kamahade's avatar
    Regular Contributor
    Thanks .. I followed that link and I could able to do it..

    I'm storing values of DB connection in a file and reading that file to create my DB connection string.

    please tell me if any more efficient step to do this.

     import groovy.sql.Sql;

    def DB_prp= new Object[4]; //array to store the db properties
    def results;
    def DB_connection;
    //Groovy open file for reading DB related properties
    def target ="D:\\soapui-release-B\\properties\\DB_connection.csv"
    def i=0;
    def FILE_NOT_EXISTS="$target file NOT present";
    File wf= new File(target);
    assert wf.exists():FILE_NOT_EXISTS;
    results = line.tokenize("\t");
    // results[1];
    //for (item in DB_prp) {
    // item;
    try {
    DB_connection = Sql.newInstance(DB_prp[0], DB_prp[1],DB_prp[2],DB_prp[3]);
    context.setProperty("dbConn", DB_connection);

    } catch (Exception e) {
    log.error "Could not establish connection to the database."

    in the teardown script I'm closing the connection

    //Close db connection
    if (context.dbConn)
    context.dbConn.close(); "Closed Database Connection."