Test if entire HTML response is equal to expected HTML

We have an API that returns HTML and want to write tests to validate that the HTML being returned is valid. We are not trying to parse the HTML, just do a direct comparison. Based on similar tests we created for JSON responses, we hoped something like this would work

const body = pm.response.text();
pm.test("check text body", function() {
pm.expect(body).to.eql(
{
<html>
<body>
My expected text here...
</body>
</html>
}
);
});

We get a syntax error: unexpected token ‘<’

@james.oleonard Welcome to the community :partying_face:

Here you can try to store the html as a variable and validate against it.

For example, store the expected html response in a global variable.

const body = pm.response.text();
pm.test("check text body", function() {
pm.expect(body).to.eql(pm.globals.get("html_resp"));
;
});
1 Like

Thanks but I need to set that global variable, which I tried doing in a pre-request script but then I run into the same problem there.

pm.globals.set("html_resp", "<html>
<body>
My expected text here...
</body>
</html>"
);

Error message is SyntaxError: Invalid or unexpected token

even i am also getting the error SyntaxError: Invalid or unexpected token
if anyone get the solution please post

@cryosat-geoscienti17

You have to put the body into a string (wrap it in quotes) as its not a valid JavaScript object hence the unexpected token.

You would then stringify the existing body, so you are comparing like for like. (String comparison for both elements\variables).

i found the solution, we can store html data in left quote
Example:
pm.response.to.have.body(`

My expected text here... `)