Forum Discussion

okcjosh's avatar
okcjosh
Occasional Contributor
12 years ago

Passing Values from Groovy Script into Request

Due to an immediate change in my company we are loosing our SoapUi Pro licenses and I am tasked with taking a rather large testcase and porting it to the community version of SoapUI. Unfortunately, this project uses the 'DataGen' teststep rather heavily. I have successfully re-created what the 'DataGen' test step produced. It is pasted below. Everyone is welcome to use any of this as they see fit. But I need help immediately with the following: How do I use the created values from my GroovyScript in my Request? How do I pass these values directly into the XML call or into a properties transfer test step. For the life of me nothing is working. I pasted the results of the log as well. This proves my script is working as designed and giving me 12 random values that are used to create a customer profile. How do I go about putting these values into the XML request? In the pro version the datagen properties were mapped directly into the request and wallah! Please Help!!


def random = new Random()
def xxx = { String alphabet, int n ->
new Random().with {(1..n).collect
{ alphabet[ nextInt( alphabet.length() ) ] }.join() } }
def idstate =
["AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"]
def xstate = random.nextInt(idstate.size())
def addystate =
["AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"]
def ystate = random.nextInt(addystate.size())
def dobyear = (1960 + random.nextInt(31))
def xmonthx = random.nextInt(13)
def xdayx = random.nextInt(31)
def idissueyear = (2008 + random.nextInt(5))
def ymonthy = random.nextInt(13)
def ydayy = random.nextInt(31)
def idexpireyear = (2013 + random.nextInt(5))
def zmonthz = random.nextInt(13)
def zdayz = random.nextInt(31)
xmonthx = ( xmonthx < 10 ) ? "0" + xmonthx : "" + xmonthx
xdayx = ( xdayx < 10 ) ? "0" + xdayx : "" + xdayx
ymonthy = ( ymonthy < 10 ) ? "0" + ymonthy : "" + ymonthy
ydayy = ( ydayy < 10 ) ? "0" + ydayy : "" + ydayy
zmonthz = ( zmonthz < 10 ) ? "0" + zmonthz : "" + zmonthz
zdayz = ( zdayz < 10 ) ? "0" + zdayz : "" + zdayz
email = xxx ( (('A'..'Z')+('0'..'9')).join(), 12 )+ '@42.okcjosh.com'
firstname = (xxx ( (('A'..'Z')).join(), 8 ))
lastname = (xxx ( (('A'..'Z')).join(), 8 ))
dob = (xmonthx + '-' + xdayx + '-'+ dobyear)
identid = xxx ( (('A'..'Z')+('0'..'9')).join(), 9 )+ 'ASAPDL'
identissue = (ymonthy + '-' + ydayy + '-'+ idissueyear)
identex = (zmonthz + '-' + zdayz + '-'+ idexpireyear)
address = xxx ( (('A'..'Z')+('0'..'9')).join(), 9 )+ 'XASAPADDRESSX'
city = xxx ( (('A'..'Z')).join(), 7 ) + 'XASAPCITYX'
zip = xxx ( (('0'..'9')).join(), 5 )
log.info"EmailAddress $email"
log.info"FirstName $firstname"
log.info"LastName $lastname"
log.info"DateofBirth $dob"
log.info"IdentificationNumber $identid"
log.info"IdentificationState ${idstate [xstate]}"
log.info"IdentificationIssueDate $identissue"
log.info"IdentificationExpirationDate $identex"
log.info"AddressLine1 $address"
log.info"City $city"
log.info"AddressState ${addystate [ystate]}"
log.info"Zip $zip"


LOG RESULTS

Mon Jun 17 00:04:18 CDT 2013:INFO:EmailAddress DGKOOFL9B795@42.okcjosh.com
Mon Jun 17 00:04:18 CDT 2013:INFO:FirstName JEIRWEWY
Mon Jun 17 00:04:18 CDT 2013:INFO:LastName VVCIWRQQ
Mon Jun 17 00:04:18 CDT 2013:INFO:DateofBirth 03-28-1975
Mon Jun 17 00:04:18 CDT 2013:INFO:IdentificationNumber 7KM6VD5HLASAPDL
Mon Jun 17 00:04:18 CDT 2013:INFO:IdentificationState GA
Mon Jun 17 00:04:18 CDT 2013:INFO:IdentificationIssueDate 00-13-2011
Mon Jun 17 00:04:18 CDT 2013:INFO:IdentificationExpirationDate 09-27-2015
Mon Jun 17 00:04:18 CDT 2013:INFO:AddressLine1 0WN1U0JSNXASAPADDRESSX
Mon Jun 17 00:04:18 CDT 2013:INFO:City GFAEDPNXASAPCITYX
Mon Jun 17 00:04:18 CDT 2013:INFO:AddressState NM
Mon Jun 17 00:04:18 CDT 2013:INFO:Zip 70200

XML REQUEST


<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<AddCustomerRequest ReasonCd="M62" UserId="XXXASAPTESTINGXXX" Version="13.1.2" xmlns="urn:hertz.com:crm:v1">
<Source Channel="COUNTER" SystemId="USOKDCR1501298" SystemType="ASAP"/>
<Customer ChangeAction="ADD" CustomerType="IND" LastModifiedBy="">
<Profile OrgCd="HRAC" ChangeAction="ADD" StatusCd="A">
<Person BirthDate="${HELP!}" FirstName="{HELP!}" LastName="${HELP!}" ChangeAction="ADD"/>
<Identification CountryCd="US" ExpirationDate="${HELP!}" IdentId="${HELP!}" IdentType="DL" StateProvCd="${HELP!}"IssueDate="${HELP!}"ChangeAction="ADD"/>
<Addresses>
<StreetAddress PrimaryInd="true" UsageType="HOME" ChangeAction="ADD"/>
<AddressLine1>"${HELP!}"</AddressLine1>
<City>"${HELP!}"</City>
<PostalCode>"${HELP!}"</PostalCode>
<StateProvCd>"${HELP!}"</StateProvCd>
<CountryCd>US</CountryCd>
</StreetAddress>
<Email Address="" PrimaryInd="true" UsageType="HOME" ChangeAction="ADD"/>
</Addresses><LoyaltyMembership MemberNum="" ProgramCd="N1" ServiceStatusCd="p" MemberStatusCd="A"><Tier TierCd="RG"/></LoyaltyMembership>
</Profile>
</Customer>
</AddCustomerRequest>
</S:Body>
</S:Envelope>

5 Replies

  • okcjosh's avatar
    okcjosh
    Occasional Contributor
    Thank You! So much for your help! Please walk me through this. I am new to groovy.

    Lets start with my existing script as it is. What specifically do I need to do in my script to make these values available as property values in my xml request? I read through the tutorial links you have sent me many times and need a little help getting started.

    For instance you told me to use : ${#TestCase#Property}

    Where does that go? In the XML request or is that a method I am supposed to use in my groovy script.

    Can we start with my groovy script? What do I need to code in the script to prepare those values to be sent out?
    Please walk me through this. I cannot thank you enough!
  • nmrao's avatar
    nmrao
    Icon for Champion Level 1 rankChampion Level 1
    Not knowing is not a problem , you may learn it as needed.
    In the groovy script add line
    testRunner.testCase.setPropertyValue( "FIRST_NAME", firstname )

    In the request have value ${#TestCase#FIRST_NAME}

    Hope this helps
  • okcjosh's avatar
    okcjosh
    Occasional Contributor
    Rao,

    You're a lifesaver! Everything works with one exception. The value of STATE. I am getting errors when I inserting the testRunner method. Please advise. The updated groovy script as well as the error are attached. Please see section of "Identification State"


    // This Groovy Script was designed to be used in SoapUI to automatically generate random customer profiles

    def random = new Random()
    def xxx = { String alphabet, int n ->
    new Random().with {(1..n).collect
    { alphabet[ nextInt( alphabet.length() ) ] }.join() } }
    def state =
    ["AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID",
    "IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS",
    "MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR",
    "PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"]

    // This creates a random Email address
    email = xxx ( (('A'..'Z')+('0'..'9')).join(), 12 )+ '@42.okcjosh.com'
    testRunner.testCase.setPropertyValue( "EMAIL", email )
    log.info"EmailAddress $email"

    // This creates a random First Name
    firstname = (xxx ( (('A'..'Z')).join(), 8 ))
    testRunner.testCase.setPropertyValue( "FIRST_NAME", firstname )
    log.info"FirstName $firstname"

    // This creates a random Last Name
    lastname = (xxx ( (('A'..'Z')).join(), 8 ))
    testRunner.testCase.setPropertyValue( "LAST_NAME", lastname )
    log.info"LastName $lastname"

    // This creates a random Date of Birth
    def dobyear = (1960 + random.nextInt(31))
    def xmonthx = random.nextInt(13)
    def xdayx = random.nextInt(31)
    xmonthx = ( xmonthx < 10 ) ? "0" + xmonthx : "" + xmonthx
    xdayx = ( xdayx < 10 ) ? "0" + xdayx : "" + xdayx
    dob = (xmonthx + '-' + xdayx + '-'+ dobyear)
    testRunner.testCase.setPropertyValue( "DOB", dob )
    log.info"DateofBirth $dob"

    // This creates a random Identification Number
    identid = xxx ( (('A'..'Z')+('0'..'9')).join(), 9 )+ 'ASAPDL'
    testRunner.testCase.setPropertyValue( "IDENT_ID", identid )
    log.info"IdentificationNumber $identid"

    // This creates a random Identification State
    def idstate = random.nextInt(state.size())
    testRunner.testCase.setPropertyValue( "ID_STATE", idstate )
    log.info (state [idstate])

    // This creates a random Identification Issue Date
    def idissueyear = (2008 + random.nextInt(5))
    def ymonthy = random.nextInt(13)
    def ydayy = random.nextInt(31)
    ydayy = ( ydayy < 10 ) ? "0" + ydayy : "" + ydayy
    ymonthy = ( ymonthy < 10 ) ? "0" + ymonthy : "" + ymonthy
    identissue = (ymonthy + '-' + ydayy + '-'+ idissueyear)
    testRunner.testCase.setPropertyValue( "IDENT_ISSUE", identissue )
    log.info "IdentificationIssueDate $identissue"

    // This creates a random Identification Expiration Date
    def idexpireyear = (2013 + random.nextInt(5))
    def zmonthz = random.nextInt(13)
    def zdayz = random.nextInt(31)
    zmonthz = ( zmonthz < 10 ) ? "0" + zmonthz : "" + zmonthz
    zdayz = ( zdayz < 10 ) ? "0" + zdayz : "" + zdayz
    identex = (zmonthz + '-' + zdayz + '-'+ idexpireyear)
    testRunner.testCase.setPropertyValue( "IDENT_EX", identex )
    log.info "IdentificationExpirationDate $identex"


    If I comment out the testRunner method everything works fine, when I try to use it, with many variations, I get the following error.


    groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase.setPropertyValue() is applicable for argument types: (java.lang.String, java.lang.Integer) values: [ID_STATE, 44] Possible solutions: setPropertyValue(java.lang.String, java.lang.String), getPropertyValue(java.lang.String) error at line: 49
  • okcjosh's avatar
    okcjosh
    Occasional Contributor
    Rao,

    I think I just figured it out. I changed the groovy script to

    // This creates a random Identification State
    idstate = random.nextInt(state.size())
    testRunner.testCase.setPropertyValue( "ID_STATE", state [idstate] )
    log.info (state [idstate])