Home > SoapUI > Load Properties from ini file

Load Properties from ini file

The Example code below explains how do we get the ini file located in the system. Which get the properties and saves as global properties.
def loadProperties(path)
{
//Loads the file settings.ini
def properties = new java.util.Properties();
properties.load( new java.io.FileInputStream( “c:/settings.ini” ))
return properties
}
Place the above code in a test.groovy file and call using the below code
def test=new test();
prop=test.loadProperties();
ProjProps = context.getTestCase().getTestSuite().getProject();
//gets the host property and its value
def host = prop.getProperty(“URL”,”localhost”);
//settings the host property and value to global project properties.
context.getTestCase().getTestSuite().getProject().setPropertyValue(“host”,host);
//displays the host URL information in log file
log.info(host);
ProjProps.setPropertyValue(“databaseurl”, props.getProperty(“databaseurl”))
Example:
//Importing package SQL
import groovy.sql.Sql
//defining objects of type java util properties
def properties = new java.util.Properties();
//reading the settings.ini file
def file = new File(“c:/settings.ini”);
//verifying the file exists and can read the file.
if (file.canRead())
{
FileInputStream fis = new FileInputStream(file);
properties.load(fis);
fis.close();
}
//Establishing the DB connection
def sql = Sql.newInstance(“jdbc:oracle:thin:@” + properties.getProperty( “dburl” ) + “:” +
properties.getProperty( “dbport” ) + “:” + properties.getProperty( “db” ) , properties.getProperty(“dbuser”) ,
properties.getProperty( “dbpassword” ), “oracle.jdbc.OracleDriver”)
//defining groovy util object
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
//defining groovey holders for response xml
def holder = groovyUtils.getXmlHolder( “Admin#Response” )
//gettings node values
def items = holder.getNodeValues( “//ns2:Admin/orgUIDs”)
// To find the count/# of nodes (log.info( “Found ” + items.length + ” items..” ))
//Get the value of response into a variable
def Value=items[0]
log.info(Value)
//Gets the db value from DB into variable.
def Dbvalue=sql.firstRow(“Select UNIT_UID from Org where ORG_UID=’$Value'”)
log.info(Dbvalue.UNIT_UID)
//compares the value of response is in DB.
if (Value==Dbvalue.UNIT_UID)
{
log.info(“the value of response “+Value+ ” = “+Dbvalue.UNIT_UID)
}
else
{
log.warn(“the value of response “+Value+ ” != “+Dbvalue.UNIT_UID)
throw new Exception(“Test Failed as the value from response xml and DB are miss matching”)
}
Categories: SoapUI
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment