harmans
11 years agoOccasional Contributor
Database connection in Setup script
Hi,
I am trying to create database connection one time in the Setup script (under TestSuite) instead of creating a new connection in all of the assertions / testcases.
So below is the groovy script in the Setup (at TestSuite Level)
import groovy.sql.Sql
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver")
def db = [url:'jdbc:sqlserver://127.0.0.1:1433;databaseName=Test123',user:'user',password:'password',driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
try{
def connection = Sql.newInstance(db.url, db.user, db.password, db.driver)
context.setProperty("dbconn",connection)
log.info "connected to the database"
}catch(Exception e){
log.error "Could not connect to the database"
}
And the script in my assertion is. But the below code does not work. How do i retrieve context property dbconn in the assertion ?
if(context.dbconn){
def sql = context.dbconn
sql.eachRow("select ua.[contact_name], ua.[contact_last_name] from [dbo].[USER_ACCOUNT] where [user_id] = 'abc' ")
{row ->
log.info("$row.contact_name")
}
}
I am trying to create database connection one time in the Setup script (under TestSuite) instead of creating a new connection in all of the assertions / testcases.
So below is the groovy script in the Setup (at TestSuite Level)
import groovy.sql.Sql
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver")
def db = [url:'jdbc:sqlserver://127.0.0.1:1433;databaseName=Test123',user:'user',password:'password',driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
try{
def connection = Sql.newInstance(db.url, db.user, db.password, db.driver)
context.setProperty("dbconn",connection)
log.info "connected to the database"
}catch(Exception e){
log.error "Could not connect to the database"
}
And the script in my assertion is. But the below code does not work. How do i retrieve context property dbconn in the assertion ?
if(context.dbconn){
def sql = context.dbconn
sql.eachRow("select ua.[contact_name], ua.[contact_last_name] from [dbo].[USER_ACCOUNT] where [user_id] = 'abc' ")
{row ->
log.info("$row.contact_name")
}
}