cancel
Showing results for 
Search instead for 
Did you mean: 

ReadyAPI- download a textfile from a URL with groovyscript

SOLVED
Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

Hi everyone,

@richie @msiadak @HimanshuTayal 

 

apparently I'm not getting anywhere with this.

I can't use the XmlSlurper as it is deprecated, not be able to work with html pages that starts with "DOCTYPE!"

 

The use of an HTTP Request passed, I get the response from the webpage, but as said here before I can't extract it or go through it with xml.

 

In the documentation of SmartBear I saw that if you check the "Download Resources" and add a resource download assertion, the api should download all files and links from the webpage. In my case it doesn't. Already filled in the "Is this usefull" textfield on that page. I've done the test with the smartbear url and that works. So it seems that either this is because of the VPN we are using, or the way the webpage was made (very simple, except for that f*ck*ng doctype thing)

 

The page is visible in ReadyAPI respons as :

  • Xml
  • HTML
  • RawResponse
  • Outline
  • Overview

So I tried to get the RawResponse like seen in a previous question and tried this:

 

import groovy.json.JsonSlurper

//cfr. https://community.smartbear.com/t5/SoapUI-Pro/ReadyAPI-get-RawResponse-with-groovyscript/m-p/205378#M46921
def fetchParam = context.testCase.getPropertyValue('fsbTransID')
def jsonSlurper
//def response = context.expand( '${GetFileFromPalDBMed#Response}' )
testRunner.testCase.getTestStepByName("GetFileFromPalDBMed").testRequest.response.responseHeaders.each { 
	if (it.key == "<tr><td><a href=")
		jsonSlurper = new JsonSlurper().parseText(it.value)
}
def fileF =  jsonSlurper.a

assert fileF != "" : "a href is blank"

testRunner.testCase.setPropertyValue("ahref",fileF.toString())
log.info a

 

 

But that doesn't help neither as Groovy doesn't find "a" in the flat text. The "a" from the tag "a href".

Trying to install SeleniumSE and all it's packages just to do this one job is overkill to me and it's a maze to. I even don't know if I will need it in the future (depending on the requirements of future clients...) and katalon is still a pain in the ass as it's still stumbling over the Chrome version. Other open source test recording tools doesn't do the job neither. (tried 2-3 of  them).

So maybe ....could someone throw me another bone here please?  😄

Or adapt this code in one way or another?

@Anastasia  ? your colleagues? 

 

Thanks in advance.

AboveAndBeyond

 

Highlighted
Community Hero

Re: ReadyAPI- download a textfile from a URL with groovyscript

Didn't follow entire thread.

 

Possible to show the screen shot of file to be downloaded?Were you able to download the file? Is it xml file?

 

If still need to download the file, follow@msiadak suggestion, i.e., use HTTP Request and provide full url of the file in test step.

If you get the file content as string from http request, then you can directly use that in following groovy step to parse.

Otherwise, there are scripts available in the forum which can download the attachment to file and save to disc automatically.

Then use the file in the following groovy script step to achieve your requirement.

 

Yes, you are right groovy.util.XmlSlurper is deprecated in groovy 3.x. Use groovy.xml.XmlSlurper (note the package difference)



Regards,
Rao.
Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

Hello @nmrao 

thanks for your answer.

Point of actions are:

Got to website with HTTP request in ReadyAPI (succeeded) you will not be able to access as it is password and VPN protected (government)

AboveAndBeyond_0-1596624760243.png

 

As explained in the documentation I've checked this:

AboveAndBeyond_1-1596624827916.png

 

and added assertion

AboveAndBeyond_2-1596624849089.png

 

but no files were downloaded

AboveAndBeyond_3-1596624888112.png

 

The website is simple:

AboveAndBeyond_4-1596624960807.png

 

these are textfiles.

in your comment:  " Then use the file in the following groovy script step to achieve your requirement. " which step do you mean? the one I have?

Concerning xmlSlurper: it also gave me an error message.

I've tried to do it this way:

 

 

import groovy.util.XmlSlurper
def response = context.expand( '${GetFileFromPalDBMed#Response}' )
def rootNode = new XmlSlurper().parseText (response)
assert rootNode.name() == 'html'
rootNode.children().each { assert it.name() in ['a', 'href']}

testRunner.testCase.setPropertyValue("ahref",rootNode.toString()) 

 

 

according this webpage:

https://docs.groovy-lang.org/2.5.8/html/gapi/groovy/util/XmlSlurper.html

 

it gives me this:

 

AboveAndBeyond_0-1596625897268.png

 

 

I'm not a developer, I just try to put snippets together to make it work. Understand highlevel what the code is doing but I can't fix it. As you see, also here, DOCTYPE is not allowed, so either I'm using the Slurper wrong, or I forgot to put something in the code or....don't know...

 

Thanks for reaching out! 😉 

AboveAndBeyond.

Highlighted
Community Leader

Re: ReadyAPI- download a textfile from a URL with groovyscript

@AboveAndBeyond : Could you please show what your response is printing from line number 18 from the groovy script.

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

Re: ReadyAPI- download a textfile from a URL with groovyscript

Could not see images as they are small and not zooming.

So not able to understand the issue.
Anotherway is you can download the file in groovy itself.
https://stackoverflow.com/questions/14474973/how-to-download-a-file-groovy

 

Downloading multiple files

 

http://www.lavezzo.com/blog/2010/04/url-encoding-for-grails/

 

Saving attachments script

https://community.smartbear.com/t5/SoapUI-Open-Source/Saving-attachments-automatically/td-p/880



Regards,
Rao.
Highlighted
Community Hero

Re: ReadyAPI- download a textfile from a URL with groovyscript

Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

ReadyAPIResponselogInfo.png

Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

@nmrao    FYI

ReadyAPIResponseOutline.pngReadyAPIResponseRAW.png

 

ReadyAPIResponselogInfo.png

 

ReadyAPIResponselogInfo2.png

Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

Hi all,

@nmrao @HimanshuTayal @richie @msiadak 

 

Well, I'm not yet done. I have tried everything one has given me but the Katalon recorder isn't recording anything, the seleniumID neither, HttpBuilder is not accepted by ReadyAPI (throws me error message), and yet I feel I'm so close.... 😞 

What I have till now: (see attached screenshots hoping that you have a clearer view)

Send a Rest Request to create an fsbTransactionID   = passed

Create groovy script to get the ID's searched for  = passed

Send a HTTP Request to get the webpage   = passed

Create a groovy script to get the file  = failed

 

A colleague of mine proposed to collect the a href's in a list and search the ID that way. But I have no clue how to do that. Anybody an idea please?

 

Thanks in advance,

AboveAndBeyond

Highlighted
Frequent Contributor

Re: ReadyAPI- download a textfile from a URL with groovyscript

Hi @nmrao 

In this link    https://community.smartbear.com/t5/SoapUI-Open-Source/Saving-attachments-automatically/td-p/880

I don't see any imports so I imported  "import java.io.FileOutputStream"

is that correct? is this enough? Because groovy throws me this:

"groovy.lang.MissingPropertyException: No such property: messageExchange for class: script 21 blabla...."

 

I've missed (once again  😞  I'm so sorry @nmrao) the fact of changing the package 'XmlSlurper'. I've adapted it meanwhile but nevertheless, groovy throws me error messages. I've tried 3 different approaches:

 

First try:

 

import groovy.xml.XmlSlurper

def getID = testRunner.testCase.getPropertyValue("fsbTransId")
log.info getID
def response = context.expand( '${GetFileFromPalDBMed#Response}' )
log.info response

def rootNode = new XmlSlurper().parseText (response)
assert rootNode.name() == 'html'
rootNode.children().each { assert it.name() in ['a href']}

testRunner.testCase.setPropertyValue("ahref",rootNode.toString())
//error message: Assertion failed: assert it.name() in ['a href'] | | | | 'head' false FSB TA PAL NG V3 debug interface error at line: 12

 

 

Second try:

 

import groovy.xml.XmlSlurper
import java.io.FileOutputStream

def response = messageExchange.response
assert null != response, "response is null"
def outFile = new FileOutputStream(new File(System.getProperty('java.io.tmpdir')+'/test.zip'))
def ins = messageExchange.responseAttachments[0].inputStream
if (ins) {
   com.eviware.soapui.support.Tools.writeAll( outFile,  ins )
}
ins.close()
outFile.close()

//error message: no such property messageExchange

 

 

3th try:

 

import groovy.xml.XmlSlurper
import java.io.FileOutputStream

def getID = testRunner.testCase.getPropertyValue("fsbTransId")
log.info getID
def response = context.expand( '${GetFileFromPalDBMed#Response}' )
log.info response


def outFile = new java.io.FileOutputStream( "C:/Documents/responses_ReadyAPI")
def input = response.attachments[0].inputStream

com.eviware.soapui.support.Tools.writeAll( outFile,  input )

//error message: (The system cannot find the path specified) error at line: 33

 

 

I'm out of lines here....any idea please?

 

thanks

AboveAndBeyond

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Filtering data retrieved from a DataSource

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors