Forum Discussion

mkhan031486's avatar
mkhan031486
Contributor
8 years ago

Displaying last 3 digits of a order number

Hi, I'm trying to test a test case where I have to pass the last three digits of an order number that I have created from another groovy script. 

 

Below is my original script that creates the order number

 

 

int a = 9
nr = "N"
for(i = 0; i < 7; i++)
{
 random = new Random()
 randomInteger= random.nextInt(a)
 nr = nr + randomInteger
}
//return nr

testRunner.testCase.setPropertyValue("OrderNumber",nr)

 

It creates an order Number for example N0382254.

Now in another script I want get the last three digits of that order number and save it into a property.

How can I call the original script and then get the last 3 digits?

Any help will be highly appreciated. Thanks!

  • nmrao's avatar
    nmrao
    8 years ago

    Not sure why are you using like that while it can be done easily by:

     

    Note that if you need to use that extracted value in a request, no addition groovy script is required. Instead use in-line script in the request itself as shown below:

     

    <id>${=  def id = testRunner.testCase.getPropertyValue("OrderNumber"); id.substring(5, id.size())}</id>

  • nmrao's avatar
    nmrao
    Champion Level 3

    Use "substring" method to get the required value.

     

    For example:

     

    def s = 'N0382254'
    log.info s.substring(5, s.size())

    • mkhan031486's avatar
      mkhan031486
      Contributor

      I'm using the below script.. 

       

      def s = context.expand( '${CreateOrderNumber#result}' )
      log.info s.substring(3, s.size());

      I'm getting the below error

      java.lang.StringIndexOutOfBoundsException: String index out of range: -3

      error at line: 2

      • nmrao's avatar
        nmrao
        Champion Level 3

        Not sure why are you using like that while it can be done easily by:

         

        Note that if you need to use that extracted value in a request, no addition groovy script is required. Instead use in-line script in the request itself as shown below:

         

        <id>${=  def id = testRunner.testCase.getPropertyValue("OrderNumber"); id.substring(5, id.size())}</id>