{"info":{"_postman_id":"053f0ed4-f888-40fb-8f9b-b2801347f2a2","name":"Day 26: Parse HTML response","description":"## Instructions for Day 26: Parse HTML response\n\n1. **Get the challenge:** Fork [the parent collection `Day 26: Parse HTML response`](https://www.postman.com/postman/workspace/30-days-of-postman-for-developers/documentation/1559645-9f9419fe-5249-4b44-b07f-4cb3ef913c81) from the main workspace to your own public workspace.\n2. **Read the documentation:** In your fork, select the first folder. Expand the context bar on the right to follow the instructions in the collection documentation.\n3. **Submit your solution:** Select the second folder `Submit your solution` and follow the instructions in the documentation to validate your solution.\n\n## Concepts covered\n- [Variables](https://learning.postman.com/docs/sending-requests/variables/)\n- [Scripts](https://learning.postman.com/docs/writing-scripts/intro-to-scripts/)\n- [Using external libraries](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries)\n- [Writing tests](https://learning.postman.com/docs/writing-scripts/test-scripts/)\n\n## Additional resources\n- [Example](https://www.postman.com/postman/workspace/postman-answers/collection/9215231-cd114167-c5a1-495e-a50f-29d6dd5af4e0?ctx=documentation) in Postman Answers workspace\n- [Check for Broken Links on Your Website Using a Postman Collection](https://blog.postman.com/check-for-broken-links-on-your-website-using-a-postman-collection/) blog\n\n## Next challenge\n\n- [Scenario testing](https://www.postman.com/postman/workspace/30-days-of-postman-for-developers/documentation/1559645-e9968036-7ad8-4daa-97c3-331184213392)","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json"},"item":[{"name":"Parse HTML response","item":[{"name":"google","event":[{"listen":"test","script":{"type":"text/javascript","exec":["pm.test(\"Status code is 200\", function () {\r"," pm.response.to.have.status(200);\r","});\r","\r","var cheerio = require('cheerio');\r","const $ = cheerio.load(responseBody);\r","//console.log($)\r","\r","const linkObjects = $('.kCrYT a');\r"," // this is a mass object, not an array\r","\r"," // Collect the \"href\" of each link and add them to an array\r"," const clinks = [];\r"," linkObjects.each((i, link) => {\r"," clinks.push($(link).attr('href'), // get the href attribute\r"," \r"," );\r"," console.log(clinks);\r"," pm.collectionVariables.set(\"clinks\", clinks);\r"," \r"," });\r","console.log(clinks);\r","const links = [];\r","\r","_.forEach(clinks, function(clinks) {\r"," links.push(clinks.replace(/^(\\/url\\?q\\=)/,\"\"));\r"," console.log(clinks.replace(/^(\\/url\\?q\\=)/,\"\"));\r"," pm.collectionVariables.set(\"links\", JSON.stringify(links))\r","});\r","\r"," pm.test(\"Links variable is an array\", function () {\r"," pm.expect(JSON.parse(pm.collectionVariables.get(\"links\"))).to.be.an(\"array\");\r","});\r","\r","\r","\r","\r",""]}}],"id":"56c07efe-1871-4b12-a3d7-2a8d395dc00e","request":{"method":"GET","header":[],"url":{"raw":"https://www.google.com/search?q=pricilla","protocol":"https","host":["www","google","com"],"path":["search"],"query":[{"key":"q","value":"pricilla"}]}},"response":[]}],"id":"87bff2e4-dc07-4fc6-a769-a4cc52410b49","description":"It's time to work on your next challenge.\n\n1. **Add a request**: Add a request called `google` to the folder `Parse HTML response` with the following details:\n - `GET` request method\n - `https://www.google.com/search` request URL\n - Query parameter `q` with the value of any term, such as `postman`.\n\n Hit **Send**, and look at the HTML response in Pretty, Raw, or Preview mode to ensure that you are receiving some search results.\n1. **Scrape web links**: Under the **Tests** tab, write a script using [the built-in library cheerio](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries) to save all the links in the HTML response as an array. Try to only collect links that are displayed in the search results. And then store the array as a [collection variable](https://learning.postman.com/docs/sending-requests/variables/#variable-scopes) called `links`.\n\n Hit **Send** to ensure your collection variable contains an array of links. Then [persist the collection variable](https://learning.postman.com/docs/sending-requests/variables/#sharing-and-persisting-data) so that the array is saved with the collection under `INITIAL VALUE` as well as `CURRENT VALUE`.\n1. **Add two tests**: Write a test to ensure a successful response status code from google. Then write another test to validate the information being stored as a collection variable is formatted as an array. Send the request to make sure the tests pass appropriately, and save your changes. \n\nOnce you complete these steps, move on to the next folder in this collection to submit your solution. Follow the instructions in the request documentation.","event":[{"listen":"prerequest","script":{"type":"text/javascript","exec":[""],"id":"935e0c3f-5c3f-4695-89ff-3ac3fcdb0ca2"}},{"listen":"test","script":{"type":"text/javascript","exec":[""],"id":"2538678a-be4d-4391-a200-17ed9142bf8e"}}]},{"name":"Submit your solution","item":[{"name":"submit","event":[{"listen":"test","script":{"type":"text/javascript","exec":["// counter for passed tests\r","let pass = 0\r","let totalToPass = 6\r","\r","let collection = pm.response.json().collection\r","\r","pm.test(\"If you have any failures, review the failed test results or ask for support in the community forum. Remember to save your changes if you update the collection. When all of your tests pass, you are done with today's challenge.\", () => {\r"," pm.expect(true);\r"," pass += 1\r","});\r","\r","pm.test(\"Status code is 200\", () => {\r"," pm.response.to.have.status(200);\r"," pass += 1\r","});\r","\r","pm.test(\"Correct collection returned\", () => {\r"," pm.expect(collection.info.name).equals(\"Day 26: Parse HTML response\")\r"," pass += 1\r","})\r","\r","pm.test(\"Request added correctly\", () => {\r"," pm.expect(collection.item[0].item.length, 'check number of requests').equals(1)\r","\r"," let googleRequest = collection.item[0].item.find(req => { return req.name === \"google\"})\r"," pm.expect(googleRequest.name, 'check name').equals(\"google\")\r"," pm.expect(googleRequest.request.method, 'check method').equals(\"GET\")\r"," pm.expect(googleRequest.request.url.raw, 'check url').includes(\"https://www.google.com/search?q=\")\r","\r"," pass += 1\r","})\r","\r","pm.test(\"Script added correctly\", () => {\r"," let googleRequest = collection.item[0].item.find(req => { return req.name === \"google\"})\r","\r"," let test = googleRequest.event.find(event => {return event.listen === \"test\"})\r"," pm.expect(test.listen, 'check test script').equals(\"test\")\r"," pm.expect(test.script.exec.toString(), 'check cheerio').to.include(\"cheerio.load\")\r","\r"," let collVar = collection.variable.find(variable => { return variable.key === 'links'})\r"," pm.expect(collVar.key, 'check collection variable set').to.equal('links')\r"," pm.expect(JSON.parse(collVar.value), 'check collection variable array').to.be.an('array')\r","\r"," pass += 1\r","})\r","\r","pm.test(\"Tests added correctly\", () => {\r"," let googleRequest = collection.item[0].item.find(req => { return req.name === \"google\"})\r","\r"," let test = googleRequest.event.find(event => {return event.listen === \"test\"})\r","\r"," pm.expect(test.script.exec.toString(), 'check tests').to.include(\"pm.test\")\r","\r"," pass += 1\r","})\r","\r","// visualization for test results\r","let template\r","if (pass == totalToPass) {\r"," template = `🍪 passing!\r","
\r"," \r"," `\r","} else {\r"," template = `🙅 please try again\r","
\r"," \r"," `\r","}\r","pm.visualizer.set(template)"]}}],"id":"c3f29dc2-60c8-43b1-aa8b-73973c151b24","request":{"method":"GET","header":[{"key":"x-api-key","value":"{{postman_api_key}}","type":"text"}],"url":"https://api.getpostman.com/collections/{{collection_uid}}","description":"It's time to submit your solution.\n\n1. **Use a Postman environment:** Find the `Postman API env` environment from the previous challenge. Update the `collection_uid` with the current collection's ID.\n1. **Validate your solution**: Save your changes in the collection and environment. Hit **Send** and look under the **Tests** tab of the server response at the bottom to review your test results.\n\nIf you have any failures, review the failed test results or ask for support in the [community forum](https://community.postman.com/). When all of your tests pass, you are done with today's challenge. Remember to save your changes as you'll need to pass all the challenges to claim a **30 days of Postman** badge."},"response":[]}],"id":"cf76d9a1-adcb-47c4-be52-5227b8282217","description":"Follow the instructions in the request documentation."}],"event":[{"listen":"prerequest","script":{"id":"55614873-6db3-4253-a38c-a7119b388a04","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"97efd656-2b7b-45bd-a4bb-bc90602b07ca","type":"text/javascript","exec":[""]}}],"variable":[{"id":"a900816e-408d-4b48-80de-7e6c468530a0","key":"links","value":["https://www.instagram.com/pricillaklein/%3Fhl%3Den&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAAegQIBhAB&usg=AOvVaw2KOlxeI0C3oc2FyHcsNC-A","https://www.thebump.com/b/pricilla-baby-name&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAFegQIARAB&usg=AOvVaw2PlWS5oKnIgdFbJ1yAAiD0","https://www.babycenter.com/baby-names-pricilla-50345.htm&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAGegQICxAB&usg=AOvVaw2GVVC3RQrsURLSd_WSnf4v","https://au.linkedin.com/in/pricilla-lum-1b3b2ab4&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAHegQICBAB&usg=AOvVaw2YBG5mn6KYIgBqnqYhjsLs","https://www.urbandictionary.com/define.php%3Fterm%3DPricilla&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAIegQIBxAB&usg=AOvVaw1Qn28v30TQ7ivt0tDbV-fq","https://www.google.com/search?ie=UTF-8&ei=uAOaYP_OLt_ez7sP-7S1uAM&q=pricilla&tbm=isch&sa=X&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQsAR6BAgFEAE","https://www.google.com/search?ie=UTF-8&ei=uAOaYP_OLt_ez7sP-7S1uAM&q=pricilla&tbm=isch&sa=X&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQ7Al6BAgFEAI","https://www.galialahav.com/bridal/couture/la-dolce-vita/pricilla/&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAPegQIAhAB&usg=AOvVaw2Lh9lE0qOOq9SuXs4HmgB3","https://www.rit.edu/fa/controller/aboutus/directory/pricillaschiffhauer.html&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjAQegQIABAB&usg=AOvVaw3MUtlreDiuRu9DkWdtIIRh","https://babynames.com/name/pricilla&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjARegQIBBAB&usg=AOvVaw1QQWfRrc53FN01fcyr_njU","https://www.coloradomesa.edu/admissions/pricilla.html&sa=U&ved=2ahUKEwj_iqia4cDwAhVf73MBHXtaDTcQFjASegQICRAB&usg=AOvVaw0nDItRq6JCQzXaePshLeNo"]},{"id":"d30048f6-f920-4bed-9d9a-f52817e1cc32","key":"clinks","value":""}]}