# API Automation Testing

**URL:** <https://community.postman.com/t/api-automation-testing/46747>\
**Category:** Help Hub\
**Tags:** tests, scripts\
**Created:** [April 25, 2023, 2:07pm UTC](https://community.postman.com/t/api-automation-testing/46747 "2023-04-25T14:07:43Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![aviation-astronaut-2](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/aviation-astronaut-2/32/15643_2.png) [@aviation-astronaut-2](https://community.postman.com/u/aviation-astronaut-2)\
**Post date:** [May 10, 2023, 10:04am UTC](https://community.postman.com/t/api-automation-testing/46747/21 "2023-05-10T10:04:30Z")

</div>

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.

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/9/c/9cb481a1a5a0c101fc6b478ac6bd59e95b515e2c.png)

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

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/3/c3701501701f4ca6346182250268720f83e24774.png)

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](https://us1.discourse-cdn.com/getpostman/original/3X/2/8/28318c02a690341d76f3903950efcff9c2810102.png)  
This now fails because of 6 which put the number out of range.  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/c/cc1342a46dfbe9acc30220265e7fb84f195f69bd.png)

…  
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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 10, 2023, 11:09am UTC](https://community.postman.com/t/api-automation-testing/46747/22 "2023-05-10T11:09:48Z")

</div>

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.

```auto
// 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](https://us1.discourse-cdn.com/getpostman/original/3X/1/6/16275a3a19266f1dcd486fc7990691c8d806aa75.png)

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.

```auto
// 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)      
    })
);

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/5/a53539ff72d285f89d4a7ac6ab5ac954eeecc428.png)

Just take this as a basic example.

```auto
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.

```auto
  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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 18, 2023, 4:26pm UTC](https://community.postman.com/t/api-automation-testing/46747/23 "2023-09-18T16:26:57Z")

</div>



[Previous page](https://community.postman.com/t/api-automation-testing/46747.md?page=1)
