FAQ: Server Testing Stack - Testing HTML Responses

This community-built FAQ covers the “Testing HTML Responses” exercise from the lesson “Server Testing Stack”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Testing for Web Development

FAQs on the exercise Testing HTML Responses

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So what’s the difference between:

const parseTextFromHTML = (htmlAsString, selector) => {
  const selectedElement = jsdom(htmlAsString).querySelector(selector);
  if (selectedElement !== null) {
    return selectedElement.textContent;
  } else {
    throw new Error(`No element with selector ${selector} found in HTML string`);
  }
};

AND something like this (although when I try in the console I get: ReferenceError: browser is not defined , so I can’t test it directly) , but I did something similar in Cake O Clock Project and it worked the same.

const parseTextFromHTML = (selector){
  const selectedElement = browser.getText(selector)
  if(selectedElement !== null){
     return selectedElement
  }else{
     throw new Error(`No element with selector ${selector} found in HTML string`)
    }
  }
}

I think that would work as well! I digged around in the last lesson, which mentions:

The .getText method, from WebdriverI/O, gets the text content from the selected DOM element.

So, we would need to import WebdriverI/O to make .browser calls work.
I tried looking up in the last lesson’s file system here:

https://www.codecademy.com/paths/web-development/tracks/test-driven-development-javascript/modules/learn-tdd-feature-test/lessons/tdd-feature-test/exercises/feature-test-i-assert

But I can’t find a clear import statement for it. Seems like we need to build a wdio.conf.js file where it all happens. It appears PhantomJS needs to be running as well.

On top of that, this works on the feature-level where we simulate a user interacting with the UI and this lesson focuses on server-level tests, so that may be why they make us use a different technique.