Forum Discussion

mkhan031486's avatar
mkhan031486
Contributor
8 years ago
Solved

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!

  • The issue got fixed after performing the below steps:-

    1. Downloaded the GPS test app the link which was given by one of the SmartBear representatives.

    Link - https://play.google.com/store/apps/details?id=com.chartcross.gpstest 

    2. Check the In View and In Use count.

    3. If the count is 0 then killing the adb instance. So that count becomes greater than 0.

    4. Then executing the script.

    5. Created a separate script to disable the mock location rather than disabling in the same script.

    6. Disabling the mock location in the same script was just setting the location first and then disabling it and on UI you couldn't see any change.

    7. So in short, killing the adb instance and checking for the fix in GPS test app before executing the script fixed the issue.

     

    Code snippet:-

    function MockGPSLocation()
    {
    var Longt, Lat, Alt, Acc, Gps;

    // Enable GPS
    Mobile.Device().GPS.GPSEnabled = true;

    Longt = Mobile.Device().GPS.Location.Longitude;
    Lat = Mobile.Device().GPS.Location.Latitude;
    Log.Message("Original Longitude: "+Longt);
    Log.Message("Original Latitude: "+Lat);

    // Enable the "Allow mock locations" property
    Mobile.Device().GPS.AllowMockLocations = true;

    Longt = 174.8367068133983;
    Lat = -36.87015422083155;
    Alt = Mobile.Device().GPS.Location.Altitude;
    Acc = Mobile.Device().GPS.Location.Accuracy;

    // Specify a mock location
    Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

    // Output the location data
    Longt = Mobile.Device().GPS.Location.Longitude;
    Lat = Mobile.Device().GPS.Location.Latitude;
    Alt = Mobile.Device().GPS.Location.Altitude;
    Acc = Mobile.Device().GPS.Location.Accuracy;
    Gps = Mobile.Device().GPS.GPSEnabled;
    Log.Message("Longitude: "+Longt);
    Log.Message("Latitude: "+Lat);
    Log.Message("Altitude: "+Alt);
    Log.Message("Accuracy: "+Acc);
    Log.Message("GPSEnabled: "+Gps);

    }

     

    function DisableMockLocation()
    {
    Mobile.Device().GPS.AllowMockLocations = false;
    }

3 Replies

  • nmrao's avatar
    nmrao
    Icon for Champion Level 1 rankChampion Level 1

    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
        Icon for Champion Level 1 rankChampion Level 1

        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>