The problem I have is that the HTML Iām looking for is generated by JavaScript.
I found how to visualize the page in the visualize tab by doing this:
var response = pm.response.text();
var base = ' < base href = "https://www.mywebsite.com/" > ';
var template = response.replace('<head>', '<head>' + base);
pm.visualizer.set(template);
However, I didnāt succeed in storing the āgenerated HTMLā by the javascript in a variable and testing it.
Postman Scripts provides you access to the Cheerio library which allows you parse and manipulate HTML pages. You can use this library to check for the presence of specific HTML elements, properties, or strings.
Hereās an example showing how you can check if a title text is valid.
const $ = cheerio.load(pm.response.text());
//output the html for testing
console.log($.html());
// Input field you want to test for
const targetElementText = $('section > div.title').text();
const savedTitle = pm.collectionVariables.get('title')
pm.test("title is valid", () => {
pm.expect(targetElementText).to.be("savedTitle");
});
Also, can you explain your workflow? Where is the html being generated? Is that being generated in the test scripts using the visualizer object or it is generated and then returned as an API response?
Itāll be helpful if you explain your workflow so I could better understand what you want to achieve.
Someone from Postman will probably need to confirm, but Iām pretty sure you canāt execute the JavaScript that is returned in the response within the Sandbox (probably due to security and many other reasons).
Is Postman the most appropriate tool to perform UI Testing?
I would love you to keep you within the platform but I feel this type of testing is better suited in a dedicated tool/framework, that allows you to create these UI tests.
The use of cheerio to parse the response HTML to check for certain elements is limited and will only get you to a certain point. Once you want to expand on that, youāre moving into the capabilities of those more targeted tools.