cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing XML file with Groovy script

SOLVED
jkrier
Regular Contributor

Re: Parsing XML file with Groovy script

I made a change to the connName[Index] as you suggested in your first question

 

def env = ['test', 'sandbox']
def connName = ['wba', 'vb', 'cnx', 'rb', 'subinfo']
def xml = '''<connStrings><env name="test"><conn><connName>wba</connName><host>host01</host><port>1500</port><user>wbauser</user><password>wbapassword</password><sid>test</sid></conn><conn><connName>vb</connName><host>host02</host><port>1500</port><user>vbuser</user><password>vbpassword</password><sid>test</sid></conn><conn><connName>cnx</connName><host>host03</host><port>1500</port><user>cnxuser</user><password>cnxpassword</password><sid>test</sid></conn><conn><connName>rb</connName><host>host04</host><port>1500</port><user>rbuser</user><password>rbpassword</password><sid>test</sid></conn></env><env name="sandbox"><conn><connName>wba</connName><host>host01</host><port>1500</port><user>wbauser</user><password>wbapassword</password><sid>sandbox</sid></conn><conn><connName>vb</connName><host>host02</host><port>1500</port><user>vbuser</user><password>vbpassword</password><sid>sandbox</sid></conn><conn><connName>cnx</connName><host>host03</host><port>1500</port><user>cnxuser</user><password>cnxpassword</password><sid>sandbox</sid></conn><conn><connName>subinfo</connName><host>host03</host><port>1500</port><user>subinfouser</user><password>subinfopassword</password><sid>sandbox</sid></conn></env></connStrings>'''
def connStrings = new XmlSlurper().parseText(xml)
//Get details of test environment
def testConnectionDetails = getConnectionDetails(connStrings, env[0], connName[0])
showConnectionDetails(testConnectionDetails, env[0], connName[0])
//Get details of sandbox environment
def sandboxConnectionDetails = getConnectionDetails(connStrings, env [1], connName[1])
showConnectionDetails(sandboxConnectionDetails, env[1], connName[1])

Then I also changed the script to not use a list for connName as you asked in your second question

 

def env = ['test', 'sandbox']
def connName = 'wba'
//, 'vb', 'cnx', 'rb', 'subinfo']
def xml = '''<connStrings><env name="test"><conn><connName>wba</connName><host>host01</host><port>1500</port><user>wbauser</user><password>wbapassword</password><sid>test</sid></conn><conn><connName>vb</connName><host>host02</host><port>1500</port><user>vbuser</user><password>vbpassword</password><sid>test</sid></conn><conn><connName>cnx</connName><host>host03</host><port>1500</port><user>cnxuser</user><password>cnxpassword</password><sid>test</sid></conn><conn><connName>rb</connName><host>host04</host><port>1500</port><user>rbuser</user><password>rbpassword</password><sid>test</sid></conn></env><env name="sandbox"><conn><connName>wba</connName><host>host01</host><port>1500</port><user>wbauser</user><password>wbapassword</password><sid>sandbox</sid></conn><conn><connName>vb</connName><host>host02</host><port>1500</port><user>vbuser</user><password>vbpassword</password><sid>sandbox</sid></conn><conn><connName>cnx</connName><host>host03</host><port>1500</port><user>cnxuser</user><password>cnxpassword</password><sid>sandbox</sid></conn><conn><connName>subinfo</connName><host>host03</host><port>1500</port><user>subinfouser</user><password>subinfopassword</password><sid>sandbox</sid></conn></env></connStrings>'''
def connStrings = new XmlSlurper().parseText(xml)
//Get details of test environment
def testConnectionDetails = getConnectionDetails(connStrings, env[0], connName[0])
showConnectionDetails(testConnectionDetails, env[0], connName[0])
//Get details of sandbox environment
def sandboxConnectionDetails = getConnectionDetails(connStrings, env [1], connName[1])
showConnectionDetails(sandboxConnectionDetails, env[1], connName[1])

In either case I get the same error

 

groovy.lang.MissingMethodException: No signature of method: Script12.getConnectionDetails() is applicable for argument types: (groovy.util.slurpersupport.NodeChild, java.lang.String, java.lang.String) values: [wbahost011500wbauserwbapasswordtestvbhost021500vbuservbpasswordtestcnxhost031500cnxusercnxpasswordtestrbhost041500rbuserrbpasswordtestwbahost011500wbauserwbapasswordsandboxvbhost021500vbuservbpasswordsandboxcnxhost031500cnxusercnxpasswordsandboxsubinfohost031500subinfousersubinfopasswordsandbox, ...] error at line: 7

I added the list to connName because I have more than one in each environment. In your example your output is only getting the connection data for env=test and connName=wba but I need to grab all of the connections for each environment and I also would like to not have to update the script whenever I have to add a connection. My original idea was to get a count of connName and then iterate through the file in the env I want to grab all of the connections for that env and I would iterate through the file as many times as the count of connName returned.

nmrao
Community Hero

Re: Parsing XML file with Groovy script

First you need to provide your question as detailed as possible at the beginning.

I understand that one needs to test against some environment.
However, did not understand what you say. And repeating thru them all the file. Just repeating may not really the thing you wanted right? there must be something done when certain connection is selected which is not happening by just repeating.

First thing, do not just go by the image, just the script provided in text format and say if it worked for single value as it is.

Then you go to the next step. Do not get confused yourself.

 

@jkrier

Any way, if you want to loop thru all connections, try this script below:

 

https://github.com/nmrao/groovyScripts/blob/master/xml/ConnectionString.groovy

 

I would have appreciated if you have tried what is actually given to you, which is working script.



Regards,
Rao.
jkrier
Regular Contributor

Re: Parsing XML file with Groovy script

Rao,

 

The script at https://github.com/nmrao/groovyScripts/blob/master/xml/ConnectionString.groovy is throwing an error. I made no changes to it and ran it as is.

 

The output is

 

Environment : test, Connection name : wba
Details followed: 
Host : host01
Port : 1500
User : wbauser
Password: wbapassword
Sid  : test
Exception thrown

groovy.lang.MissingPropertyException: No such property: connectionDetails for class: ConsoleScript0

	at ConsoleScript0.run(ConsoleScript0:89)

 I ran this from the Groovy console from ReadyAPI, I also changed all of the println lines to log.info and ran from groovy script in SoapUI NG and got the same error

 

I made a change from

 

def connStr = getConnectionString(connectionDetails) 

 

to

 

def connStr = getConnectionString(connetionDetails)

 

Now when I run the script I see all of the connections being logged to the output but it still throws an error at the very end

 

Exception thrown

java.lang.NullPointerException: Cannot invoke method parent() on null object

	at ConsoleScript2.getConnectionDetails(ConsoleScript2:96)

	at ConsoleScript2$getConnectionDetails.callCurrent(Unknown Source)

	at ConsoleScript2.run(ConsoleScript2:87)

 

This is all very promising though, I added a few lines to pull out some data

 

def showConnectionDetails(def connexionDetails, String environmentName, String connectionName){
  log.info "Environment : ${environmentName}, Connection name : ${connectionName}"
  hostName = "${connectionName}"+"host" //new line
  testRunner.testCase.testSteps['InputValues'].setPropertyValue(hostName, "${connexionDetails.host}") //new line

And in my Properties step "InputValues" all of the hosts are being logged as I need. If it could just run without the error at the end I think it would be exactly what I want.

 

I apologize if I am being confusing to you. I am trying not to be and I very much appreciate your help and your time.

 

 

nmrao
Community Hero

Re: Parsing XML file with Groovy script

@jkrier

Sorry about it, that was actually done quickly (could not test) for the change that you requested ( to loop thru all environments, and connection names)

That was happening because, you provided all connection names, but some of them are not present in all environments.

Handled now, please take the updated script from github.



Regards,
Rao.

View solution in original post

jkrier
Regular Contributor

Re: Parsing XML file with Groovy script

Rao,

 

Thank you so much, this is what I needed. I cannot thank you enough for your help!

nmrao
Community Hero

Re: Parsing XML file with Groovy script

@jkrier,

 

Glad to know.
Not sure if you needed that creating final connection string (using combination of all the values, of course you can directly set that value in test case / test suite property to use property expansion in jdbc step), but that was done keeping in mind that it might be helpful, like a bonus?



Regards,
Rao.
jkrier
Regular Contributor

Re: Parsing XML file with Groovy script

Hi Rao,

 

I had some free time so I looked into using the connection string versus setting the individual connection values and I think it is the smarter way to go about it. Pretty good bonus, thanks for providing it.

usha1
Visitor

Re: Parsing XML file with Groovy script

Hi 

I have a xml response which one of the node contains encryoted code i need to decrypt the code and convert into xml file

<attachment>H4sIAAAAAA ...like this some data

</attachment>

manual process is copy the decryptedcode and paste in test.uue file 

decrypted code is encrypted by running base64 batch file

how to write script to convert that code into xml file

 

any help apreciated

 

regards

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors