# Day 14 of 30 Days of Postman Challenge

**URL:** <https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494>\
**Category:** Help Hub\
**Tags:** tests, variables, collections, authentication, api\
**Created:** [October 19, 2023, 5:58pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494 "2023-10-19T17:58:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![altimetry-pilot-9153](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@altimetry-pilot-9153](https://community.postman.com/u/altimetry-pilot-9153)\
**Post date:** [October 19, 2023, 5:58pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/1 "2023-10-19T17:58:49Z")

</div>

Hello,  
So ive been working on this challenge and encountered an error

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/5/9590454369441e94dda28aa2a62f80a8773b6220.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/b/4bb4a171a9038117e1957e8b1adda73bc2a1f493.png)  
this is my tests tab in the get github request

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/7/f7484e06b85d6e52ca2ee80c2836546b53f6d874.png)  
this is my pre request script in the get github request  
everything was fine until i came to the submission point  
could someone help me out with this?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 20, 2023, 3:21pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/2 "2023-10-20T15:21:42Z")

</div>

This is the failing test.

```auto
pm.test("Scripts added correctly", () => {
    let githubRequest = collection.item[0].item.find(req => { return req.name === "github"})

    let test = githubRequest.event.find(event => {return event.listen === "test"})
    pm.expect(test.script.exec.toString(), 'check test').includes("pm.test")
    pm.expect(test.script.exec.toString(), 'check collection variable set').includes("pm.collectionVariables.set")

    let variable = collection.variable.find(variable => { return variable.key == "repoCount"})
    pm.expect(variable.key, 'check collection variable name').equals("repoCount")
    pm.expect(parseInt(variable.value), 'check collection variable value').to.be.at.least(0).and.at.most(30)

    pass += 1
})

```

Can you show the tests tab for your “github” request?

Your screenshot shows the pre-request scripts instead.

---

<div class="post-metadata">

**Author:** ![altimetry-pilot-9153](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@altimetry-pilot-9153](https://community.postman.com/u/altimetry-pilot-9153)\
**Post date:** [October 21, 2023, 4:34pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/3 "2023-10-21T16:34:48Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> pm.test("Scripts added correctly", () => {
> let githubRequest = collection.item[0].item.find(req => { return req.name === "github"})
> 
> let test = githubRequest.event.find(event => {return event.listen === "test"})
> pm.expect(test.script.exec.toString(), 'check test').includes("pm.test")
> pm.expect(test.script.exec.toString(), 'check collection variable set').includes("pm.collectionVariables.set")
> 
> let variable = collection.variable.find(variable => { return variable.key == "repoCount"})
> pm.expect(variable.key, 'check collection variable name').equals("repoCount")
> pm.expect(parseInt(variable.value), 'check collection variable value').to.be.at.least(0).and.at.most(30)
> 
> pass += 1
> })
> 
> ```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/a/1a2e916e1f383327870d80f2eaf73d815a719f14.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/8/08b4d9067e82abf47fb76280ea79556775fe58fb.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 22, 2023, 2:20pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/4 "2023-10-22T14:20:19Z")

</div>

That looks ok to me, the odd thing is that the failing test shows the actual result first, which should show the first line of the tests tab, but the text looks different to your test (although I’m not sure if your second screenshot is the top of your tests tab or not).

The test thinks the first line of your test tab is…

> // Test that the response status code

Can you share your collection?

Or alternatively, I would recommend duplicating the submit request, and then adding some console logs for the “githubRequest” and “test” variables just after they are defined.

This should return the test tab as a string.

```auto
let test = githubRequest.event.find(event => {return event.listen === "test"})
console.log(test.script.exec.toString();

```

The test is just checking that the text “pm.collectionVariables.set” exists somewhere in that string.

---

<div class="post-metadata">

**Author:** ![altimetry-pilot-9153](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@altimetry-pilot-9153](https://community.postman.com/u/altimetry-pilot-9153)\
**Post date:** [October 23, 2023, 2:11pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/5 "2023-10-23T14:11:36Z")

</div>

Thank you a lot  
I found the solution

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 23, 2023, 2:39pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/6 "2023-10-23T14:39:33Z")

</div>

@altimetry-pilot-9153

Glad its working.

Would you like to share the solution?

It might help someone else who is attempting this days challenge.

---

<div class="post-metadata">

**Author:** ![altimetry-pilot-9153](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@altimetry-pilot-9153](https://community.postman.com/u/altimetry-pilot-9153)\
**Post date:** [October 23, 2023, 5:20pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/7 "2023-10-23T17:20:57Z")

</div>

Yes sure  
I actually started from square 1 again for Day 14  
I can remember that one of my errors was that the repocount variable had nothing in it

pm.test(“Status code is 200 and collection variable is set”, function () {  
pm.response.to.have.status(200);  
pm.collectionVariables.set(“repoCount”, 10);  
}); This is the simple code i put for my tests tab

// Pre-request Script  
// Check if the pm.response object is undefined  
if (pm.response !== undefined) {  
// Check if the request is successful  
if (pm.response.to.be.ok) {  
// Get the response body  
var responseBody = pm.response.body;

// Check if the response body is undefined  
if (responseBody !== undefined) {  
// Convert the response body to JSON  
var jsonData = JSON.parse(responseBody);

// Get the number of repositories  
var repoCount = jsonData.length;

// Save the number of repositories as a collection variable  
pm.collectionVariables.set(“repoCount”, repoCount);  
} else {  
// Handle empty response body  
}  
} else {  
pm.collectionVariables.set(“repoCount”, 0);  
}  
} else {  
// Handle undefined pm.response object  
}

And this is for my prerequisites script tab there were tiny errors like the JSON body not being recognized (i parsed it in this case), etc

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [November 22, 2023, 5:21pm UTC](https://community.postman.com/t/day-14-of-30-days-of-postman-challenge/53494/8 "2023-11-22T17:21:31Z")

</div>

Closed on November 22, 2023, 5:21pm UTC

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