Need help with writing a test in postman

Hello everyone!
Need your help with solving the following task.
I need to create a TEST which performs a check that the number of characters “\” (backslashes) in the body of the response does not exceed (less or equal to) 700.
If there are no such characters in the response body, we should look for “/” (slashes)

I’ve already tried:

var stringsearch = "\\", str = pm.response.text();
for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
console.log(count);

if (count <= 700) {
    var stringsearch = "/", str = pm.response.text();
    for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
    console.log(count);
}

But! I need I can see the result of the test in Test Results but not in the console. Could you help to do this?

1 Like

Hey @juliakharlan :wave:

Do you have an example of the response body? It makes it easier for folks to try and create a solution from it.

Hey @danny-dainton :slightly_smiling_face:
Yes, that is it

Could you copy and paste a snippet of the raw response, so no one has to type that all out from the picture :sweat_smile:

What’s the significance of the slashes? Are you just looking to see if there are a specific number of URLs?

So, here is it :smiling_face_with_tear:
There is no significance, just need to count all the backslashes or slashes in the response, without any specific purpose :smiley:

{
    "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
    "id": "2309956",
    "self": "https://jira.a1qa.com/rest/api/2/issue/2309956",
    "key": "RFXIRFXBDQ-469",
    "fields": {
        "customfield_18474": null,
        "customfield_10071": null,
        "customfield_10470": "9223372036854775807",
        "customfield_10350": null,
        "fixVersions": [],
        "customfield_13500": null,
        "resolution": null,
        "customfield_10234": null,
        "lastViewed": null,
        "customfield_19670": null,
        "customfield_19671": null,
        "customfield_11670": null,
        "customfield_10221": null,
        "priority": {
            "self": "https://jira.a1qa.com/rest/api/2/priority/3",
            "iconUrl": "https://jira.a1qa.com/images/icons/priorities/major.svg",
            "name": "Major",
            "id": "3"
        }
    }
}

Could be something like:

let count = pm.response.text().split("\\").length - 1
console.log(count);

if (count <= 700) {
    let count = pm.response.text().split("/").length - 1
    console.log(count);
}

I’d need more context to provide a better answer.

Thank you for your help! :face_holding_back_tears:
But could it be written as for tests? I mean is it possible to view the result in “Test results” but not in the Console?
I need to get a text as a result something like “There are less than 700 backslashes”.
I suppose it shold be started as:

pm.test (“There are less than 700 backslashes”, function () {
let count = pm.response.text().split(" \\ ").length - 1

const backSlashCount = pm.response.text().split("\\").length - 1

if (backSlashCount <= 700) {
    pm.test('Less than 700 backslashes', () => {
        forwardSlashCount = pm.response.text().split("/").length - 1
        pm.expect(forwardSlashCount).to.be.below(backSlashCount);
    })
} else {
    pm.test('More than 700 backslashes', () => {
        pm.expect(backSlashCount).to.be.above(700);
    })
}

Thank you, Danny, so much for your help!!