Help with Snippets

Hi all I have one snippets that check if array value completely matches with my response, but can I used other snippets which Pass test cases if only single value is matched with the array.
Please help me with this.

Have a look at the following examples.

The first test just compares the array in its entirety.

The second test loops through each item in array1 and checks to see if its in array two in the same index place.

It’s using a JavaScript feature called Template Literals (aka backticks) to interpolate variables and create a custom test case names.

The third bunch of tests loops also loops through the items in array1, but has a missing element, so will trigger a failure in the test (to prove that the tests are actually working).

The forth round of tests loops through each item in array1 and just checks to see if its in array 3. It doesn’t care about the position, just that it exists somewhere in the array. This should also fail on the same element as the previous loop.

let array1 = [0, 92, 93, 94];
let array2 = [0, 92, 93, 94];
let array3 = [1, 92, 93, 94];

pm.test("arrays match", () => {
    pm.expect(array1).to.eql(array2);
});

array1.forEach((element, i) => {
    pm.test(`loop1: ${element} is within array`, () => {
        pm.expect(element).to.eql(array2[i]);
    });
});

array1.forEach((element, i) => {
    pm.test(`loop2: ${element} is within array`, () => {
        pm.expect(element).to.eql(array3[i]);
    });
});

array1.forEach((element, i) => {
    pm.test(`loop3: ${element} is within array`, () => {
        pm.expect(array2).to.contain(element);
    });
});

image

@michaelderekjones but in above case we are just comparing 2 arrays, and in our case we have to compare given set of array with Response body of API. which is

I’m not sure I’m following.

I’m assuming that the readings element in the response body is also an array.

Your initial code snippet is showing.

abc.data.readings[a]

Which would appear to be an array with an index.

The “read” variable is also an array.

You haven’t included an example response body, or the rest of your code, so some assumptions needed to be made. (That you are working with two arrays).

The examples above has four tests.

  • The first one just compares two arrays for exact matches.
  • The second example loops through the array, checking each element, but the indexes must match. (Which is the same as your test in your initial post, but using a foreach loop instead of a for). The position in the arrays must be the same.
  • The third example is the same as the second but with mismatched elements to show a failure.
  • The fourth example is just checking that the current element in array1 appears somewhere in array2.

One of those methods should cater for your requirement. If it doesn’t, then you will need to explain your requirement in more detail.

So below are the Array and response which we have to compare, but in a such a way that if only single value from TEST array match Test case sould also get passed but in our using for loop all the values should get mathed.

So we want such method that if single value of array matches with response test case should get passed.

Please use the preformatted text option to post sample code.

I’m not retyping all of that into Postman.

Use the pre-formatted text option in the editor to prevent everything being aligned to the left.

The logic for this is provided above.

Readings is an array which you need to loop through (using whatever loop method you prefer).
The test assertion should be using the “contain” method.
Similar to your initial code but it would be that “read” contains value. With the value coming from the array. “readings[index].value”