30 Days Developer Postman - Day 26

Guys,

I might be missing something silly here. Can anyone help me here please? Collection link is https://www.getpostman.com/collections/a82ed90c5373e9f09125

One test case is failing,

I am storing the links as strings.

pm.collectionVariables.set("links", JSON.stringify(links))

When I test under google request, it’s passing:

pm.test("Links variable is an array", function () {
pm.expect(JSON.parse(pm.collectionVariables.get("links"))).to.be.an("array");

});

Of course I can edit the tests under “submit” request to make it pass. Please any explain me what I am doing wrong.

2 Likes

Hi @bpricilla,

When I import your collection, the test under google request is also failing for the same reason:

This seems to be because your links collection variable is currently being saved as a comma separated string - i.e. https://www.instagram.com/etc,https://www.instagram.com/foo,...

The tests are expecting that you should save your collection variable formatted as an array - i.e. ["https://www.instagram.com/etc","https://www.instagram.com/foo",...]

@neilstudd Thanks for the response :slightly_smiling_face:

But I see that google request Tests are passing for me :exploding_head:

And I see the submit request is also passing now :stuck_out_tongue:

Sorry my bad, “Persist All” :stuck_out_tongue: it’s working now.

Anyway thanks a ton for replying :partying_face:

2 Likes

Hi i have done the Day 26 when i am using the Bing its returns null value for the array but when i am using the Google its returns the links here is my code for referance
pm.test(“Status code is 200”, function () {

pm.response.to.have.status(200);

});

var cheerio = require(‘cheerio’);

const $ = cheerio.load(responseBody);

const linkObjects = $(’.kCrYT a’);

const clinks =;

linkObjects.each((i, link) => {

  clinks.push($(link).attr('href'), // get the href attribute

      );



});

console.log(clinks);

pm.collectionVariables.set(“clinks”, clinks);

const links = ;

_.forEach(clinks, function(clinks) {

links.push(clinks.replace(/^(\/url\?q\=)/,""));

console.log(clinks.replace(/^(\/url\?q\=)/,""));

});

pm.collectionVariables.set(“links”,JSON.stringify(links))

console.log(links);

pm.test(“Links variable is an array”, function () {

pm.expect(JSON.parse(pm.collectionVariables.get(“links”))).to.be.an(“array”);

});
its returns current value=

but if am using the google current value has links .

so whether i can use the google for this script
can anyone help me …

Thank you very much

Hi @kkalpana55

Yes we can use Google also instead Bing, i too tried with Bing and its not working

Thanks !

Hi @sseenivasan89 , thank you very much.

1 Like

Hi guys.

I am working on the Day 26 test. I managed to make it work with bing. I scrap all the links from my search results and store them in the collection variable.

However, the “Script added correctly” test of “submit” is failing with a “JSONError: Unexpected token ‘h’ at 1:1”

The related line of the submit test is this one:

pm.expect(JSON.parse(collVar.value), 'check collection variable array').to.be.an('array')

I don’t understand why is trying to read it as JSON, there isn’t any mention in the documentation of the test that the array must be saved as JSON.

Should I save the list of links as a JSON or the submit test is wrong?

Thank you!

Edit: to add my code:

var cheerio = require("cheerio");

const $ = cheerio.load(pm.response.text());

var linkss = [];

$("li.b_algo h2 a").each(function () {

    var currentLink = $(this).attr("href"); 

    linkss.push(currentLink)

});

pm.collectionVariables.set("links", linkss);

pm.test("Status code is 200", function () {

    pm.response.to.have.status(200);

});

pm.test("Collection Variable is an Array", function() {

    pm.expect(pm.collectionVariables.get("links")).to.be.an("array");

})
1 Like

same thing happened to me, thanks for pointing out that haha

1 Like

I have the same issue. I have asked ChatGPT to perform this task and it did very. I had to edit second line of code and to add pm.response.text()

var cheerio = require('cheerio')

let htmlResponse = pm.response.text()
 
let linkArray = []
 
const $ = cheerio.load(htmlResponse)
 
$('a').each(function (i, elem) { 
    linkArray.push($(this).attr('href'))
})

What do symbols li.b_algo h2 mean?
What about tests, I must say that the test Script added correctly fails for me too, even thought I have persisted data in variables.
Did you fix it somehow?

I’m dealing with this same error (JSONError: Unexpected token ‘h’ at 1:1). Was there a resolution?

This helped me:

pm.collectionVariables.set('links', JSON.stringify(links)); 
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.