API Automation Testing

What’s the postman code for validating and testing phone number ranges for example you want testers to only use numbers ranging from 0789999000 to 0789999999?

I used this but failed.
pm.test(“Verifying test number range limit”, function(){
pm.expect(response.APIStatus.subscriberid).to.be.above(256789999000);
})

Hi @aviation-astronaut-2

try something like below for range, create an if condition to validate the range in between, and go for the assertion,

here am checking the total is in within the range or not

The following is another option, but please note, you need to strip the zero from the beginning for it to work properly. It will drop the zero for most, but if the number starts 08 or 09, it will be treated differently which may cause undesired effects.

Javascript 0 in beginning of number - Stack Overflow

let telNumber = "0789999001";

pm.test("Verifying test number range limit", function () {
    pm.expect(parseInt(telNumber)).to.be.within(parseInt("0789999000"), parseInt("0789999999"));
});

As your numbers start with a 07, the 0 will get dropped when you parse it to an integer.

This is my request body:

{
    "customerId": "256789999077",
    "partnerId": "2560110006502",
    "appName": "eliadapi",
    "hostName": "ftp://10.156.x.x/Loans/PaulMarkFile/",
    "operationName": "setDataSharingFlag",
    "operationValue": 1
}

I’ve edited your code to have this below;

let customerId = "256789999001";

pm.test("Verifying test MSISDN range limits", function () {
    pm.expect(parseInt(customerId)).to.be.within(parseInt(256789999000), parseInt(256789999033));
});


It's still passing a success even with a number outside the range
![image|460x73](upload://roxSDGJHqAKSkV4I5fDFQKEaw0T.png)


What am I doing wrong?

@mdjones This is my request body;

{
    "customerId": "256789999077",
    "partnerId": "2560110006502",
    "appName": "eliadapi",
    "hostName": "ftp://10.156.x.x/Loans/MarkFile/",
    "operationName": "setDataSharingFlag",
    "operationValue": 1
}

I’ve edited your code to have this below;

let customerId = "256789999001";

pm.test("Verifying test MSISDN range limits", function () {
    pm.expect(parseInt(customerId)).to.be.within(parseInt(256789999000), parseInt(256789999033));
});

But I still received success whether within or outside the range.
image

Where did I go wrong?

What is passing that you think shouldn’t?

This fails for me.

let customerId = "256789998000";

pm.test("Verifying test MSISDN range limits", function () {
    pm.expect(parseInt(customerId)).to.be.within(parseInt(256789999000), parseInt(256789999033));
});

This number, 256789999077 shouldn’t pass because it’s outside 256789999000 and 256789999033.

That fails for me.

Here are some boundary tests in a loop.

let customerIds = ["256789998999","256789999000","256789999001", "256789999032","256789999033","256789999034","256789999077"];

customerIds.forEach(customerId => 
    pm.test(`Verifying test MSISDN range limits - boundary test ${customerId}`, () => {
        pm.expect(parseInt(customerId)).to.be.within(parseInt(256789999000), parseInt(256789999033));
    })
);

and the results

I assume that you are retrieving the customer ID from the response and not using the hard coded ID that I included in my example?

@mdjones my target is to limit anyone else from using a customerId [in the request body], that is outside the range.

That means I’ll be testing the ‘customerId’ on the side of ‘request’ and not 'response 'and if any user puts one that is outside, they receive an error.

I don’t want to specify the numbers in the test script/code but the condition should ensure that ‘any’ numbers used must be within range.

Not really sure what you are trying to achieve here.

You can run tests in the pre-request tab, but it won’t stop the actually request from sending.

It also sounds like you are not in control of your test data if you are worried about someone using a value outside of the agreed parameters.

This below has worked.

var validRange;  
pm.test("VERIFY CORRECT MSISDN - Range", function(){   

  var bdyraw = JSON.parse(pm.request.body.raw);
  var xyz = bdyraw.customerId;
  var regex = /^256(7[7-8]|4[1-9])[0-9]{7}$/;
  var check = xyz.match(regex);
  console.log(check);
  if(check==null){
    validRange= false;          
    pm.expect(validRange).to.eql(true)      
  }else{          
    validRange= true;          
    pm.expect(validRange).to.eql(true)      
  }

})

The IF statement should not be needed.

let customerIds = ["256789998999","256789999000","256789999001", "256789999032","256789999033","256789999034","256789999077"];
let regex = /^256(7[7-8]|4[1-9])[0-9]{7}$/;
customerIds.forEach(customerId => 
    pm.test(`VERIFY CORRECT MSISDN - Range ${customerId}`, function(){   
        pm.expect(customerId).to.match(regex)      
    })
);

Not exactly sure what your regex is supposed to be doing, but its not checking if the numbers are between 256789999000 and 256789999033.

All of the test data is passing.

^ Matches the beginning of a the string
match a 2 character
match a 5 character
match a 6 character
(match 7 + range 7-8) or (match 4 + range 1-9)
match range 0-9
Quantifier - match 7 of the preceding token (which was range 0-9)
$ matches the end of the string

Sorry, I’ve corrected the above.

What am trying to do is this. With this API am testing, testers have a strict range of allowed test numbers they’re to use for testing to limit them from committing fraud. When you use a wrong number, your api response body doesn’t have a success and the test results also fail.

This below is working. The test numbers must fulfil this: 1st four = 2567, next can either be a 7 or 8, next 4 digits are 9s and the last three range from 000 to 999.

//Allowed Test MSISDN Range

var validRange;  
pm.test("Allowed Test MSISDN Range", function(){   

  var bdyraw = JSON.parse(pm.request.body.raw);
  var xyz = bdyraw.customerId;
  var regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
  var check = xyz.match(regex);
  console.log(check);
  if(check.index!=0){
    validRange= false;          
    pm.expect(validRange).to.eql(true)      
  }else{          
    validRange= true;          
    pm.expect(validRange).to.eql(true)      
  }

})

I would recommend the same as any other test, that you boundary test the logic.

Make sure it fails where it should.

I’ve just included your updated regex and its not failing if the first four digits are 2566 or 2568. I haven’t even got around to testing the rest of the regex.

My recommendation when testing regex is to do it back to front. Test the last elements first and work your way to the beginning elements.

let customerIds = [
    "256779999000", // pass
    "256679999000", // fail
    "256879999000" // fail
];

let regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
customerIds.forEach(customerId => 
    pm.test(`VERIFY CORRECT MSISDN - Range ${customerId}`, function(){   
        pm.expect(customerId).to.match(regex)      
    })
);

image

The following tests the regex up to the first four characters which I think meets your criteria.

let customerIds = [
    "256779999000", // pass
    "256779999999", // pass
    "25677999900", // fail
    "25677999999", // fail
    "25677999999A", // fail
    "256779991000", // fail
    "256779919000", // fail
    "256779199000", // fail
    "256771999000", // fail
    "256789999000", // pass
    "256769999000", // fail
    "256799999000", // fail
    "256679999000", // fail
];

let regex = /^2567[7-8]9{4}\d{3}/
customerIds.forEach(customerId => 
    pm.test(`VERIFY CORRECT MSISDN - Range ${customerId}`, function(){   
        pm.expect(customerId).to.match(regex)      
    })
);

You can use websites like the following to check your regex.

RegExr: Learn, Build, & Test RegEx

/^2567[7-8]9{4}\d{3}/

Am not specifying any customerIds because I want testers be able to use any of the many available test numbers (about 2000 in existence) but can only fall in that range.
Hence, var regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;

The example customer ID’s I put in my code are just that. Examples to test that the regex is working correctly.

The same as any other automated test, you need to make the test fail to ensure you are not getting a false positive.

Based on the following statement that you posted earlier.

The test numbers must fulfil this: 1st four = 2567, next can either be a 7 or 8, next 4 digits are 9s and the last three range from 000 to 999.

If I use your regex, then they are all passing when they shouldn’t based on that logic.

let customerIds = [
    "256779999000", // pass
    "256779999999", // pass
    "25677999900", // fail
    "25677999999", // fail
    "25677999999A", // fail
    "256779991000", // fail
    "256779919000", // fail
    "256779199000", // fail
    "256771999000", // fail
    "256789999000", // pass
    "256769999000", // fail
    "256799999000", // fail
    "256679999000", // fail
];

let regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
customerIds.forEach(customerId => 
    pm.test(`VERIFY CORRECT MSISDN - Range ${customerId}`, function(){   
        pm.expect(customerId).to.match(regex)      
    })
);

Therefore on face value, your regex doesn’t appear to meet your criteria.

Do you happen to be on skype?

Sorry, I’m not willing to communicate outside of the forum.

The main advice I’m trying to give here, is that you test your regex and ensure it fails where it should.

The same advice goes for any test. Once you’ve got a particular test working, try and make it fail to prove that you are not getting a false positive.