Extract comments from HTML response

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 :wink:

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
}

thanks, appreciate the help

Similar issue

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’));

How do i convert the Response object to a string?

Here is a sample

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);
    tmp = the_response.substring(the_response.indexOf('HTMLId'), the_response.indexOf('.ftl'));
    console.log('DBG ' + tmp);  
  }

here is a result

Thanks for the code sample!

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:

import { findBetween } from "https://jslib.k6.io/k6-utils/1.4.0/index.js";

export const options = {}

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>
  `;
  const comment = findBetween(the_response, "<!-- $HTMLId: ", " $ -->");
  console.log(comment);
}

Most of the time you will be operating on Response objects, and those have a Response.body property so you would use that instead:

import http from 'k6/http';
import { findBetween } from "https://jslib.k6.io/k6-utils/1.4.0/index.js";

export const options = {}

export default function () {
  const res = http.get("https://httpbin.test.k6.io/html");
  const p = findBetween(res.body, "<p>", "</p>");
  console.log(p);
}

1 Like