API Automation Testing

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)      
  }

})

image

This below is my request body. Tester need to add a test number within a specified range.

image

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.
image
This now fails because of 6 which put the number out of range.
image


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.

I’m really not following you here.

My code is example code.

I don’t have access to your response, so I can only provide example code.

I put the test and test data in a loop to prove that the regex was working.

Your code would not need the loop or test data to be hardcoded. You would just use the element from the response.

I’ve put your latest regex in my loop and its still passing all of the test data which surely cannot be correct.

// test data including whether it should pass or fail.
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)      
    })
);

image

I’m still going on your original logic which was…

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 the regex I previously supplied, then it fails where I would expect it to.

// let regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
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)      
    })
);

Just take this as a basic example.

var xyz = "256789969022";
var regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
pm.test(`VERIFY CORRECT MSISDN`, function(){   
    pm.expect(xyz).to.match(regex)      
})

It’s passing when it shouldn’t.

Really not sure what you are trying to do with this.

  var regex = /^256(7[7-8])[9-9]{4}|[0-9]{3}$/;
  var check = xyz.match(regex);
  console.log(check);
  if(check.index!=0){

But not sure its doing what you think its doing.