Forum Discussion

rakshitbachary's avatar
rakshitbachary
New Contributor
13 years ago

Setting properties at test case level from request

Dear Members,

I am newbie to groovy scripting. As part of my assignment, I have to set test case level properties. I have below request in hand for which I have to set properties at test case level.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:webs="http://webs">
<soapenv:Header/>
<soapenv:Body>
<webs:SaveFilePropertiesRequest>
<webs:File>
<webs:FileName>fileNameValue#1</webs:FileName>
<webs:FilePath>filePathValue#1</webs:FilePath>
<webs:FileType>fileTypeValue#1</webs:FileType>
<webs:FileSize>fileSizeValue#1</webs:FileSize>
</webs:File>
<webs:File>
<webs:FileName>fileNameValue#2</webs:FileName>
<webs:FilePath>filePathValue#2</webs:FilePath>
<webs:FileType>fileTypeValue#2</webs:FileType>
<webs:FileSize/>
</webs:File>
</webs:SaveFilePropertiesRequest>
</soapenv:Body>
</soapenv:Envelope>


Properties that will be set for the above request will be as below

FileName#1
FilePath#1
FileType#1
FileSize#1
FileName#2
FilePath#2
FileType#2
FileSize#2


I am having lot more iterations for the files. Doing this manually is getting much troubled. Please help me with a groovy code so that these properties can be automatically set at test case level.

Thanks in advance

Regards
Rakshit

4 Replies

  • EmBer's avatar
    EmBer
    New Contributor
    This doesn't have your file path names because i don't know how many you will have but this will create testSuite level properties based on how long the loop runs

    for(i=0; i<10;i++){
    def temp = "cloud" + i.toString()
    testRunner.testCase.testSuite.addProperty(temp)
    testRunner.testCase.testSuite.getProperty(temp).setValue(i.toString())
    }

    This creates 10 properties that are all "cloud0, cloud1...." with values "0,1....". You can do the same kind of thing for your File properties.

    I hope that helps
  • Thanks EmBear

    But my requirement is somewhat different. We cannot assume

    My request can be as below also

     
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:webs="http://webs">
    <soapenv:Header/>
    <soapenv:Body>
    <webs:SaveFilePropertiesRequest>
    <webs:SystemUserCredentials>
    <webs:UserID>userIDValue</webs:UserID>
    <webs:UserPassword>userPasswordValue</webs:UserPassword>
    <webs:SystemDomain>systemDomainValue</webs:SystemDomain>
    </webs:SystemUserCredentials>
    <webs:PropertiesToBeSaved>
    <webs:File>
    <webs:FileName>fileNameValue#1</webs:FileName>
    <webs:FilePath>filePathValue#1</webs:FilePath>
    <webs:FileType>fileTypeValue#1</webs:FileType>
    <webs:FileSize>fileSizeValue#1</webs:FileSize>
    </webs:File>
    <webs:File>
    <webs:FileName>fileNameValue#2</webs:FileName>
    <webs:FilePath>filePathValue#2</webs:FilePath>
    <webs:FileType>fileTypeValue#2</webs:FileType>
    <webs:FileSize/>
    </webs:File>
    </webs:PropertiesToBeSaved>
    </webs:SaveFilePropertiesRequest>
    </soapenv:Body>
    </soapenv:Envelope>


    For this request I need to save properties as below:


    UserID
    UserPassword
    SystemDomain
    FileName#1
    FilePath#1
    FileType#1
    FileSize#1
    FileName#2
    FilePath#2
    FileType#2
    FileSize#2


    I expect to not set any manual value like "cloud" in the code(except #1....#n. This can be adjudged by #i). Code should automatically extract these values and set them as a property.

  • def REQUESTHOLDER = testRunner.testCase.testSuite.project.testSuites['yourtestsuitename'].testCases['yourtestcasename'].testSteps['yourrequestname'].getProperty('Request').getValue();
    def TESTCASE = testRunner.testCase.testSuite.project.testSuites['yourtestsuitename'].testCases['yourtestcasename'];

    def xmlfile = new XmlParser().parseText(REQUESTHOLDER);
    def SystemUserCredentials = xmlfile.'soapenv:Body'.'webs:SaveFilePropertiesRequest'.'webs:SystemUserCredentials'
    def WebFile = xmlfile.'soapenv:Body'.'webs:SaveFilePropertiesRequest'.'webs:PropertiesToBeSaved'.'webs:File'

    TESTCASE.setPropertyValue('UserID', SystemUserCredentials.'webs:UserID'.text())
    TESTCASE.setPropertyValue('UserPassword', SystemUserCredentials.'webs:UserPassword'.text())
    TESTCASE.setPropertyValue('SystemDomain', SystemUserCredentials.'webs:SystemDomain'.text())


    def count = 1;

    WebFile.each {
    it.each {
    def name = it.name().toString()[it.name().toString().lastIndexOf('}') + 1 .. -1] + '#' + count.toString()
    def value = it.text()

    TESTCASE.setPropertyValue(name, value)
    }
    count++;
    }


    If you want to get the values from the response, change
    def REQUESTHOLDER = testRunner.testCase.testSuite.project.testSuites['yourtestsuitename'].testCases['yourtestcasename'].testSteps['yourrequestname'].getProperty('Request').getValue();


    to

    def REQUESTHOLDER = testRunner.testCase.testSuite.project.testSuites['yourtestsuitename'].testCases['yourtestcasename'].testSteps['yourrequestname'].getProperty('Response').getValue();