Each and every page of application has an “identifier” in HTML code
The “identifier” comes in a form of HTML Comment
This comment located before
Is there an “easy”/“proper” way to extract this comment.
Currently, i am parsing Response as a String
let tmp = the_response.substring(the_response.indexOf(‘HTMLId’), the_response.indexOf(‘.ftl’));
Hi there! Have a look at findBetween - although if you look at the code it uses, it’s not too dissimilar to your current approach
export function findBetween(content, left, right, repeat = false) {
const extracted = [];
let doSearch = true;
let start, end = 0;
while (doSearch) {
start = content.indexOf(left);
if (start == -1) {
break; // no more matches
}
start += left.length;
end = content.indexOf(right, start);
if (end == -1) {
break; // no more matches
}
let extractedContent = content.substring(start, end);
// stop here if only extracting one match (default behavior)
if (!repeat) {
return extractedContent;
}
// otherwise, add it to the array
extracted.push(extractedContent);
// update the "cursor" position to the end of the previous match
content = content.substring(end + right.length);
}
return extracted.length ? extracted : null; // return all matches as an array or null
}
This is not working for me because i do not have left, right parameters .
I need to find those.
However, i am not able to due to the the_response is an object, and my code attempting to use String function. let tmp = the_response.substring(the_response.indexOf(‘HTMLId’), the_response.indexOf(‘.ftl’));
The parseHTML function returns a Selection object, which rightly does not have a indexOf function or even substring - you’ll want to use the html() function to convert the HTML back to a string:
import http from "k6/http";
import { parseHTML } from 'k6/html';
export const options = {
vus: 1,
iterations: 1,
httpDebug: 'full',
}
//
export default function () {
const the_response = `<html lang="en" data-kantu="1">
<!-- $HTMLId: templates/v2/logon.ftl $ --><head>
<meta charset="UTF-8">
<title>Part of the site</title>
</head><body></body</html>
`;
extractComment(the_response);
}
export function extractComment(the_response) {
the_response = parseHTML(the_response).html(); // <----
var tmp = the_response.substring(the_response.indexOf('HTMLId'), the_response.indexOf('.ftl'));
console.log('DBG ' + tmp);
}
Of course, if you’re already dealing with a string, you can skip the parsing to HTML and back altogether: