Iteration code through csv file with if else condition

Hi Team,
I am using csv file to iterate request multiple times with different sets of data but issue occurred as below:
During the 1st iteration, specific field have some values but during the 2nd iteration, that specific field does not have any value(its blank).

Step to reproduce:
csv file


1st Iteration fields-seniorCoachTeamscurrentCount, seniorCoachTeamstarget, seniorCoachTeamsavatarUrls blank, Doesn’t have any value.

2nd Iteration Fields: seniorCoachTeamscurrentCount, seniorCoachTeamstarget, seniorCoachTeamsavatarUrls have values.

Response:
1st iteration:
{
“orderingEntities”: [
{
“currentCount”: 45,
“target”: 10,
“avatarUrls”:
}
],
“seniorCoachTeams”: ,
}
2nd iteration response:
{
“orderingEntities”: [
{
“currentCount”: 0,
“target”: 5,
“avatarUrls”:
}
],
“seniorCoachTeams”: [
{
“currentCount”: 0,
“target”: 5,
“avatarUrls”:
}
],
}

Code:
1st solution -working fine, No error
pm.expect().to.include(pm.iterationData.get(“seniorCoachTeamscurrentCount”));

2nd solution -not working ,error as below snap

pm.test(“data rankidVerification”, function () {
var jsonData = pm.response.json();
var str = jsonData.seniorCoachTeams;
var regex = RegExp(’,’);
if (regex.test(str))
{
console.log(jsonData.seniorCoachTeams);
}else
{
pm.expect(jsonData.seniorCoachTeams[0].target).to.eql(data.seniorCoachTeamstarget);
}

P.S. Due to editor view, In the code you might be see square () but those are brackets “[” “]” .

What do you want it to do when there isn’t a value?

The code is breaking on this line:

pm.expect(jsonData.seniorCoachTeams[0].target).to.eql(data.seniorCoachTeamstarget);

Because there is no item at index 0 of seniorCoachTeams

This is the what I want with my code. When there is no value for seniorCoachTeams filed, I want to skip this line of code.

pm.test(“data rankidVerification”, function () {
var jsonData = pm.response.json();
var str = jsonData.seniorCoachTeams;
var regex = RegExp(’’);
if (regex.test(str))
{
console.log(jsonData.seniorCoachTeams);
}else
{
pm.expect(jsonData.seniorCoachTeams[0].target).to.eql(data.seniorCoachTeamstarget);
}
if there is any response for eniorCoachTeams then only else condition will run, correct?
What gonna be if else condition when need to skip any lines of code?

My console logs is clearly saying no values for senoirCoachTeams, and I want to use if else with below line of code only pm.expect(jsonData.seniorCoachTeams[0].target).to.eql(data.seniorCoachTeamstarget);.

Since seniorCoachTeams is an array, what you’re doing with matching on a regex probably won’t give you the definitive results you want. If I were writing this test, I would do it this way:

const jsonData = pm.response.json();
if(seniorCoachTeams.length > 0){
  pm.test('Does the first senior coach team match the data target?', function(){
    const seniorCoachTarget = jsonData.seniorCoachTeams[0].target;
    pm.expect(seniorCoachTarget).to.eql(data.seniorCoachTeamsTarget);
  });
}

Thanks for the suggestion. With the slight of code change, Got worked.

1 Like