I am using the pre-script method to loop through test data. I am also setting a validation set of data in the prescript file.
// fetching the test data
var usernames = pm.environment.get("usernames");
var valid_usernames = pm.environment.get("valid_usernames");
// Setting the test data
if(!usernames){
usernames = ['ww12345',' ',123,'www.google.com'];
}
// Setting the verification data
if(!valid_usernames){
valid_usernames = ['ww12345','testing'];
}
// Reseting the loop counter for clicking the 'send' button in top right corner
if(usernames.length === 0){
usernames = ['ww12345','',123,'www.google.com'];
}
// looping through the test data
var currentUsername = usernames.shift();
pm.environment.set("username", currentUsername);
pm.environment.set("usernames", usernames);
Then during the test execution, I am calling the current username, checking if it exists in the validation data, and then checking if the response was 200 or 400.
// Checking status codes for valid and invalid users
var valid_usernames = pm.environment.get("valid_usernames");
var username = pm.environment.get("username");
pm.test("Status Code for User", function () {
if(valid_usernames.includes(username)){
pm.response.to.have.status(200);
console.log("Status code 200 for : " + username);
}else{
pm.response.to.have.status(400);
console.log("Status code 400 for : " + username);
}
});
The only issue is that I am testing if an empty string returns a 400. However, I do not put an empty string into my validation set, but it returns true as if I did. I’m pretty sure this is because Postman is treating my validation set as a string, rather than an array. And a string always contains an empty string, but an array does not always contain an empty string. You can check for yourself here:
Array.includes()
String.includes()
I believe the behavior is pretty inconsistent. For looping over my data set, Postman operates as if it is an array, but for the validation set, Postman treats it as a string. From what I found, I cannot ‘typecast’ a variable as an array.
I am looking for a solution that can let me treat valid_usernames as an array rather than working with it as a string
I have the following:
if(username && valid_usernames.includes(username)){}
but in the case that i need to check the empty string, then it will be an extra if statement, and I’m trying to stay DRY. Thanks