cancel
Showing results for 
Search instead for 
Did you mean: 

DataBase Connection in SetUp script and use the same in Groovy Test Step

Occasional Contributor

DataBase Connection in SetUp script and use the same in Groovy Test Step

Hi  All,

 

I am currently stuck in an issue where i want to configure JDBC connection either in SetUp Script or the one configured at project level 'DataBases' menu and could use the same sql instance in any of the teststep mostly groovy test step while hitting sql query in it.

 

Here i am trying , but not working:

import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils

def dbURL="jdbc:jtds:sqlserver://serverNme;databaseName=DBName;trusted_connection=yes "

def dbUsername = ""
def dbPassword = ""
def dbDriver = "net.sourceforge.jtds.jdbc.Driver"
def sql = Sql.newInstance(dbURL,dbDriver)
context.setProperty("DBCon", sql)

 

Using this sql instance in my teststep(Groovy) as:

import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils

def sqlObj=context.getProperty("DBCon")
def connection = sqlObj.firstRow("SELECT * FROM Table ")

log.info connection

 

 

Thanks!!

~Shivani

 

9 REPLIES
Community Leader

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Hi Shivani,

 

Are you currently able to create a SQL connection at all? 

 

 

Occasional Contributor

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Yes I am able to connect via  SQLConnection.

 

But the situation is i have written groovy code for connection as well as query in same groovy teststep.

And, there is option in Ready APi to make or configure JDBC connection at project level using Databses menu[Refer to attachment]. I want ot use either this configuration in my grrovy script or sql connection made in setup script.

 

~Shivani

 

Highlighted
Community Leader

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

There are two options. 

 

First option - Create a JDBC connection via "databases" and you can build your queries i.e select X from Z where A = 2 etc. Once you are happy with the query, the outcome gets stored in the custom properties automatically and to call that custom property all you have to do is ${DataSourceName#PropertyName}. This is simple and straight forward way for data-driving. 

Please find link below to help you.

https://support.smartbear.com/readyapi/docs/testing/data-driven/concepts.html

 

Second option - Create a JDBC connection via groovy script and store that connection in a custom property and just recall that custom property everytime you want to run the sql connection. You will then write your own custom query and store the query output manually into custom properties.

 

I personally prefer option one simply because it's easy and less time consuming, though if you have some very complex custom queries that "query builder" cannot support then you could use the second option I suppose. 

Occasional Contributor

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Thanks for such quick response.

 

But i am not sure about saving any sql connection object in custom properties so as to reuse it in my scripts in between test steps.

Can you guide me with any such sample example

 

Thanks

Contributor

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

As far as I can tell there is no way to preserve the sql instance or fetch it unfortunately. I have read through the groovy SQL documentation but I didn't see a way to do it.

 

you can however move your script into a script library I would create a script methods for both opening and for closing connections

 

Hope this was helpful,

Mo

If my answer helped you out, please consider leaving kudos and/or accepting as a solution.
Have an awesome day!
Occasional Contributor

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Yes , this could be solution if this script is having some logic which can be used anywhere in the project, but in case of DB connection I dont think would be of that Use.

 

Is there any way to use DB confiuration at Project level in Databases Menu in test scripts?

Community Leader

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Hi,

def dbURL="jdbc:jtds:sqlserver://serverNme;databaseName=DBName;trusted_connection=yes "

def dbUsername = ""
def dbPassword = ""

 

You can store individual components of the connection in custom props, i.e username, pw, db name etc. the whole connection cannot be stored in custom props as one. 

 

If you do want to go via groovy route then easier method would be script library as Mo mentioned. 

Contributor

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step


 Is there any way to use DB confiuration at Project level in Databases Menu in test scripts?


No, unfortunately not. Custom properties (the way to preserve values) only store strings. As I said in my previous reply it doesn't seem like there is any way to fetch an existing sql instance.

 

Instead you will need to open and close the sql instance each time you need to access the database (hence the script library would come in handy to not have to write out the connection parameters each time)

If my answer helped you out, please consider leaving kudos and/or accepting as a solution.
Have an awesome day!
Moderator

Re: DataBase Connection in SetUp script and use the same in Groovy Test Step

Hi all,

@ShivaniKamboj, have you found an answer to your question? If so, could you please accept the appropriate answer as a solution? This way, other users can easily find it if they face a similar issue in the future.

Or, do you have any updates or additional details here?

 

@jhanzeb1@MoUddin, thank you for your time looking into the case, it's great to have you both in the Community!

 

Have a nice day,

 

Olga Terentieva,
SmartBear Assistant Community Manager

Join us for SmartBear Community Wintertainment!

Share Your Stories
Read Latest News
Top Kudoed Authors