Switch Statements not running

I have this test where I can’t get the switch statement to run. What am I missing ? I can get the commented out if statement to run ok

// Get the value of TestName from the csv file
var testName = pm.iterationData.get("TestName");


/*if (testName == "Missing Firstname") {
    pm.test("TEST: " + testName + ". Error message check.", function () {
        pm.expect(pm.response.json().errors.FirstName[0]).to.eql("FirstName is required");
    });
}*/

//console.log("Testname is " + testName);
switch (testName) {
    case "Missing FirstName" :
        pm.test("TEST: " + testName + ". Error message check.", function () {
            pm.expect(pm.response.json().errors.FirstName[0]).to.eql("FirstName is required");
        });
    break;
    case "Missing LastName" :
        pm.test("TEST: " + testName + ". Error message check.", function () {
            pm.expect(pm.response.json().errors.LastName[0]).to.eql("LastName is required");
        });
    break;
}

It would appear that you need to include the default option.

I thought this was optional (but highly recommended to include).

switch(testName) {
  case "Missing Firstname":
    console.log("missing firstname");
    break;
  case "Missing Lastname":
    console.log("missing lastname");
    break;
  default:
    console.log("something else");
}

If I remove the “default” option from the above, then it doesn’t trigger any of the cases. If I include it, then it works as expected.

You could just add a console log here, or I’m guessing it should fail a test if you ever hit the default option.

Thanks, I just tried that and it is hitting the default clause now. Must be something to do with datatypes ?

I tested this by hardcoding the testName.

// var testName = "Missing Firstname";
var testName = "Missing LastName";


switch(testName) {
  case "Missing Firstname":
    console.log("missing firstname");
    break;
  case "Missing Lastname":
    console.log("missing lastname");
    break;
  default:
    console.log("something else");
}

If its reading it from a CSV file, then it should be a string.

Console log the testName straight after you define it.

1 Like

I’m a clown :rofl: Logged the value to console and Just realised the issue.

I have the possible values in the csv of FirstName, LastName, etc (upper case N) but the case statement is looking for a match on Firstname, Lastname (lower case n)

thanks for the help

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.