Using if else statement condition for login test

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:

Details (like screenshots):
I am using if else statement condition for login test and try to success value should be in true and should be match msg for all different condition in body value

let jsonData1 = pm.response.json(),

msg = ["Login Successfull", 

       "Incorrect Password",

       "Please register first." ]

if (jsonData1.success === "false") {

pm.test("password is wrong"${jsonData1.msg}, () => {

    pm.expect(jsonData1.msg).to.be.oneOf("Incorrect Password")

})

else {

pm.test("Login Successfull" $ {jsonData1.msg}, () => {

    pm.expect(jsonData1.msg).to.equal("Login Successfull")

}

}

}`

How I found the problem:
There was an error in evaluating the test script: SyntaxError: Unexpected token ‘else’

I’ve already tried:
Postman 31-07-2021 08_10_46 (2)

Hi @science-astronaut-50,

I got rid of the red line errors you were seeing on the code. I’ve also cleaned it up with missing semi-colons and indentations. Hopefully this helps :slight_smile:

msg = ["Login Successfull",
       "Incorrect Password",
       "Please register first." ];

if (jsonData1.success === "false") {
    // use backticks to when adding a object in a string
    pm.test(`password is wrong ${jsonData1.msg}`, () => {
        pm.expect(jsonData1.msg).to.be.oneOf("Incorrect Password");
    })

//missing closing bracket
} else {

    // use backticks to when adding a object in a string
    pm.test(`Login Successfull ${jsonData1.msg}`, () => {
        pm.expect(jsonData1.msg).to.equal("Login Successfull");
    
    //missing closing bracket on the closing test
    })
}