How can I retrieve console log information in POSTMAN when a test fails?

Having an issue with this test. I am trying to trigger a console.log when the installment value in my JSON is less than 100 (resulting in a test FAIL). However, I am receiving both PASS and FAIL results in the test, but no information is showing up in the console.log. I'm unsure why my code isn't functioning as expected. I believed using else if would provide the desired outcome, but it seems there are still issues.

var jsonData = JSON.parse(responseBody)

for (var i = 0; i < jsonData.length; i++) {
  for(var j = 0; j < jsonData[i].products.length; j++) {

    pm.test("Installment Check", function() {
      if (pm.expect(jsonData[i].products[j].installment).to.be.above(100))
      {
        // Successful case...
      }
      else if (pm.expect(jsonData[i].products[j].installment).to.be.below(100))
      {
        // Failure case...
        console.log(jsonData[i].id)
      }
    });
    
  }
}

Answer №1

When using pm.expect, it does not return a value but throws an Error instead. This means you cannot use it in an if condition that expects a boolean.

You can try either of the following:

var jsonData = JSON.parse(responseBody)

for (var x = 0; x <= jsonData.length - 1; x++) {
    {

        for (var y = 0; y <= jsonData[x].products.length - 1; y++)

            pm.test("Installment > 100", function () {
                if (jsonData[x].products[y].installment < 100 && jsonData[x].products[y].installment < 100) console.log(jsonData[x].id)
                pm.expect(jsonData[x].products[y].installment).to.be.above(100)
                pm.expect(jsonData[x].products[y].installment).to.be.below(100)
            });
    }
}

Alternatively:

var jsonData = JSON.parse(responseBody)

for (var x = 0; x <= jsonData.length - 1; x++) {
    {

        for (var y = 0; y <= jsonData[x].products.length - 1; y++)

            pm.test("Installment > 100", function () {

                try {
                    pm.expect(jsonData[x].products[y].installment).to.be.above(100)
                } catch (e) {
                    pm.expect(jsonData[x].products[y].installment).to.be.below(100)
                    console.log(jsonData[x].id)
                }

            })
    }
}

Answer №2

Here is my method for displaying information in both the console and test window:

pm.test(pm.info.requestName + " - Auth Successful", ...);
console.info(pm.info.requestName + " - Auth Successful");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A step-by-step guide on setting up flow types for @material-ui/core version 4

Is there a way to install flow types for material-ui/core version 4.x.x? It seems like the last update was for version 1.x.x here. The documentation on this topic is quite limited here. I'm unsure if there is still support for this, especially since t ...

Execute Javascript Code Once Directive Has Finished Rendering the DOM for ShareThis

Within my Angular (1.3) application, I have a list of records being displayed using ng-repeat. The template includes a directive that contains ShareThis controls from ShareThis, which are activated once the DOM is loaded. Upon the initial load of the app, ...

Combining Array Attributes to Create a New Property as a 'JSON String'

I'm attempting to combine the attributes of an array into a JSON-like string as a new attribute. For example: [{ { "orderNo":"1", "description":"Item 1", "note": "Note 1" }, { "orderNo":"2", "description":"Item 2", ...

The stylesheet is linked, but no changes are taking effect

I've linked my template.php to style.css, and when I make changes in template.php, they show up fine. However, if I make changes in the stylesheet, nothing happens. What should I do? My website is not live; I'm working on it using a WAMP server. ...

Activate response upon verifying website link

I am adding functionality to my HTML page where I want certain sections to be visible when the user clicks on a link. Initially, these sections are hidden using the CSS property display: none;. My aim is to make these sections visible when the user clicks ...

"Utilizing Bootstrap Tour for Easy Navigation: Automatically Scroll Back to the Top with

I have a Bootstrap tour set up with code that is meant to capture the user pressing the end tour button and scroll them back to the top of the screen. However, currently the code runs when a new popover loads instead of when the end tour button is clicke ...

Adjusting the font color when hovering over multiline text

I am currently in the process of coding a website for my job, and I am working on changing the text color when it is hovered over. However, there seems to be a break in my code causing the text not to highlight all at once. Any guidance or suggestions on h ...

Steps to modify a JSON file's content and store it for future use

There is a json file stored in res/raw which contains numerous objects. I am parsing this file to display the objects in a list on my user interface. Whenever the user finishes interacting with this list and exits the activity, any modifications made by ...

Troubleshooting Rails 4: Handling a 404 Not Found Error When Making an AJAX Call to a

After spending about an hour trying to figure this out, I am still stuck... The action in my oferts_controller.rb file looks like this: def update_categories @categories = Category.children_of(Category.find(params[:categories])) respond_to ...

Guide on converting XML and JSON data in Swift 3

As a newcomer to IOS, I am looking to convert some mixed data (combination of XML and JSON) received from a SOAP web service into an array using Swift 3. This data is being received as a string variable in the parser method. func connection(_ connection: ...

The CSS Bootstrap 4 class 'input-group-append' is not functioning properly. Just recently, the styles were displaying correctly

My web application using AngularJS 1.7.8, jQuery 3.3.1, Bootstrap 4.3.1, and various other CSS and JS libraries worked seamlessly last week. However, today I noticed an issue with the button visualization. To Replicate: Visit openskymap.org to see my d ...

Creating sophisticated TypeScript AngularJS directive

Recently, I came across a directive for selecting objects from checkboxes which can be found at this link: The issue I'm facing is that we are using TypeScript and I am unsure of how to implement the directive in TypeScript. From what I understand, ...

Using Array.Map() to retrieve the child array within a React component

Here is the data I have retrieved from an API: [ { "id": 1, "appName": "string", "defaultAction": "string", "defaultMessage": "string", "rules": [ { "id": 1, "version": "string", "brand": "string", ...

Guide on how to send files to an API using a form in React with MUI

I am struggling with sending an image through the front-end to my back-end application that is set up to accept data. Here is the code I have: import { useState } from 'react'; import axios from '../../config'; import { useNavigate } fr ...

Tips for linking attributes to a function using ES6

I'm currently working on a stateless React.js component called myView. I want to keep the syntax as concise as possible, but I'm having trouble understanding how to export a default function with an attached object. Here's an example in myVi ...

The Ajax Auto Complete function encounters an issue when attempting to assign a value to the TextBox

I have a Textbox that utilizes an Ajax Autocomplete function. The function is functioning correctly and retrieves values from the database, populating them in the TextBox as well. However, there is a button on the page that triggers a query, fetching som ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

Generate a Table Using JSON Data with AngularJS and ng-repeat

I have some JSON data that looks like this: { "Workout1": { "Name": "First", "Rounds": [ { "Exercises": [ { "Name": "Exercise1", "Repeat": 10 }, { "Name": "Exercise2 ...

Utilizing Slim PHP to generate JSON responses

I have a situation where my backbone application is making an ajax GET call to my slim php app, expecting JSON dataType in return. $.ajax({ url: './myroute', type: 'GET', dataType: "json", data: { userna ...

Creating dynamic ng-options in AngularJS

Below is an array: $scope.age = 2; $scope.people = [{name:"Sam",age:2},{name:"Pam",age:3},{name:"Ham",age:4}] The requirement is to make the ng-options dynamic. When age is 2, display all people objects in ng-options. If age is 1, show only the object wi ...