Forum Discussion

davistark's avatar
davistark
New Contributor
14 years ago

how to run a Step Request for each file located in a folder

Hello! I'm a true novice with soapUI and Groovy Scripts. Now I have to do some task with this for my job but I'm don't really know how to do it.

First I will explain what the project does: Send a file (XML) to a server.
This was done with a "Step Request" (named "Publication of data"), and in his content there is the text XML which is sent to the server.

All this is done correctly, the problem is that every time I send a new file, I have to copy its contents (all file's text) and paste it into the Request Step (in the attached picture; I have to replace the text that is selected ).

My question is: how can I do to make soapUI read a folder where are all the XML files that I want send, and send them one by one?

I guess I have to create a "groovy script" to read the XML files located in a folder, copy its contents and put it in the Step Request (picture attached; replace the text that is selected by the one containing the XML file is been reading at the time).

I know, more or less, the script to read files in a folder, but I don't know how to make the Step Request (named "Publication of data") be executed for each XML file located in a folder with the contents of each XML.

Thanks in advance!
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    Hi,

    You can read the xml for the test step and populate its request with the xml read from your file like below:

    setXML = ""//xml read from the file in your folder
    def requestSet = testRunner.testCase.getTestStepByName( "Publication Of data" );
    requestSet.getProperty("request").setValue(setXML);

    Once your groovy script populates the request of test step "Publication Of data", you can call this step for execution from your groovy script like below:

    def validate1 = testRunner.runTestStepByName("Publication Of data") ;

    If you have any assertions in the test step which fail, your test step will fail and you can check the status like below:

    log.info "Validation Status = $validate1.status";

    Note that, once the test step has completed execution, the control will pass back to groovy script. So If you have to run multiple requests reading from multiple files, put a for loop in your groovy script.

    Let me know if this works or need anything else.

    Thanks,
    Deepesh Jain
  • davistark's avatar
    davistark
    New Contributor
    Thanks for your answer deepesh.jain!

    I think I understand the mechanics, but I'm not sure if I have to do something with the request step "Publication of data". That is, it assumes that the text that was selected in the attached image (in my previous post) has to be deleted and there will be the XML text (the content of the file that is been reading). Do I have to put in there "something" to indicate that in this space there is what the script groovy populates?

    The statement that you put before:
    <<requestSet.getProperty("request").setValue(setXML);>>
    I'm not sure what it does. Does this fill the entire contents of "Publication of data"? how indicates that this have to fill only part of the text I want to replace for each XML file content?

    Forgive me if I make basic questions, I would like to had more time to view tutorials and general info about the programa and Groovy Script, but I started yesterday with this program and I need to perform these tasks for my job

    PD: sorry for my poor english
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    Hi,

    The code that i mentioned would populate the complete request, not the highlighted portion. It would replace the complete xml in request. If you have to update specific nodes in the request xml. For that you will first have to load the request xml in a holder like below:

    def gutils = new GroovyUtils ( context )
    def holder = gutils.getXmlHolder("Publication Of Data#Request"); // This gets the request xml of your test step in a holder.

    Now you can set any node in this xml like below:

    holder.setNodeValue("//ns1:addClinicalDocument/ns1:recordTarget/ns1:patientRole",value1);
    holder.updateProperty();
    //Give the complete path of the node what you need to set in the quotes above.
    Let me know if this works.

    Thanks,
    Deepesh Jain
  • davistark's avatar
    davistark
    New Contributor
    Thanks for you quick answer!

    Ok, if I understood, what you recommend is fill the values of every node with the instruccion "holder.setNodeValue...". But first I have to get the values from the XML file (by xPath), right?

    Also I have to leave the highlighted portion in the Step Request "Publication of data", but deleting the values of every node cause the Groovy Script will populate them, right?
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    It depends what is there in your xml file.
    If there is a series of nodes and tree structure like below in your xml file you can just read the whole thing at the parent level from xml file and populate the xml structure at that node.

    <1>
    <2>
    <3>
    </3>
    </2>
    </1>

    If this is what is there in your xml file, then you can read the complete structure at xpath <1> and populate the same as a child node in between your original xml request.

    You don't need to delete the nodes if you want to keep the default values.