This is working for any number a tester can think of adding on the request body without having to add it in the test code.
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)
}
})
This below is my request body. Tester need to add a test number within a specified range.
The person testing doesn’t have to edit the test code… My test allows him/her edit CustomerId but must fall within range. I’ve now allow 2567[6-8] for first 5 digits.
If they add a number in the request body, outside the range, they get this error.
This now fails because of 6 which put the number out of range.
…
By using your code, the tester has to go back to the test code to add in the new available, unplanned for numbers for to allow it to pass.