Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
9 years ago

To pass DB connections details from xml file.

Hi

 

I have to pass DB connection details from xml file. Following is my xml file.

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
   <properties>
      <property name="DBDriver" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="ConnectionString" value="jdbc:oracle:thin:ercore/ercore@localhost:1521:XE"/>
   </properties>
</config>

 

Can you please suggest the way to do so.

 

Actually I want to call DBDriver and Connection String from xml file.

1 Reply

  • nmrao's avatar
    nmrao
    Community Hero

    vpachpute1,

     

    Not sure if you are still looking for it.

     

    Here is the groovy script: The idea is to fetch the values and store them at project level custom properties, so that you can use those values across the project.

     

    ​def xml ="""<?xml version="1.0" encoding="UTF-8"?>
    <config>
       <properties>
          <property name="DBDriver" value="oracle.jdbc.driver.OracleDriver"/>
          <property name="ConnectionString" value="jdbc:oracle:thin:ercore/ercore@localhost:1521:XE"/>
       </properties>
    </config>"""
    
    def parsedXml = new XmlSlurper().parseText(xml)
    def getData = { element, attributeValue ->
      parsedXml.'**'.find { it.name() == element && it.@name == attributeValue}.@value.text()
    }
    def driver = getData('property',  'DBDriver')
    def conStr =  getData('property',  'ConnectionString')
    context.testCase.testSuite.project('DRIVER_CLASS', driver)
    context.testCase.testSuite.project('CONNECTION_STRING', conStr)
    

    Use below property expansion where you need those values:

     

    for driver class -  ${#Project#DRIVER_CLASS}

    for connection string - ${#Project#CONNECTION_STRING}