Ryan
Ryan

Reputation: 1542

How to use OR operator in postman pm.test

I am attempting to create a test that checks a json response to contain either one property or another.

My example:

pm.expect(jsonData.data.owner).to.have.property('orgName') || 
pm.expect(jsonData.data.owner).to.have.property('firstName')

I can't seem to find any documentation on using or operators in the new pm.expect() syntax.

Upvotes: 4

Views: 13228

Answers (4)

eachhisownchimera
eachhisownchimera

Reputation: 111

pm.expect( booleanCondition1 || booleanCondition2 ).to.be.true

Upvotes: 1

Sandra Pavan
Sandra Pavan

Reputation: 264

use oneOf() method

pm.expect(jsonData.carrierType.type).to.be.oneOf(["mobile", "landline"]);

Upvotes: 7

Kacper Werema
Kacper Werema

Reputation: 448

I was looking for answer to implement OR in postman tests, but didn't found any. I've done just a simple if statement to determine which of the conditions should be checked. (Knowing the business logic). In my application I had to check if we support german language, but if we don't by default we set it to english. It was easy because I've took the value from my json and than manualy check which condition should be the valid one. This might help you and anyone else in further implementation of this. So for me it looked like this:

   pm.test("German language support.", function(){
   var jsonData = pm.response.json();
      if(jsonData.count() > 0){
          var langCode = jsonData[0].language.code;
          if(langCode === "en"){
          pm.expect(jsonData[0].language.code).to.eql("en") ;
          }else{
                 pm.expect(jsonData[0].language.code).to.eql("de") 
          }
      }
});

Upvotes: 2

A.Joly
A.Joly

Reputation: 2387

first you misuse pm.expect, you should do something like

pm.expect(jsonData.data.owner).to.have.property('property_name','property_value)

I'm afraid that it is not straightforward to implement or condition ... see examples here

Upvotes: 0

Related Questions