Using k6 converter :"ReferenceError: cheerio is not defined"

Hello ,
In postman I am using cheerio to parse some html response . After conversion I get “ReferenceError: cheerio is not defined” :

ERRO[0003] ReferenceError: cheerio is not defined
running at post (file:///C:/prj/vbc/k6/k6-rest-api.js:82:27(11))
default at executePostrequest (file:///C:/prj/vbc/k6/libs/shim/core.js:1250:11(23))
        at file:///C:/prj/vbc/k6/libs/shim/core.js:315:30(27)
        at file:///C:/prj/vbc/k6/k6-rest-api.js:57:21(18)
        at (native)
        at file:///C:/prj/vbc/k6/k6-rest-api.js:56:17(13)  executor=per-vu-iterations scenario=default source=stacktrace

Manualy adding the following extra imports sort of makes it work :

  import  cheerio from "./libs/cheerio.js";
  import URI  from "./libs/urijs.js";

The problem is now that it takes 3 minutes with laptop fans maxed out to run the simple 1 VU 1 iteration with a few requests test.

I sense that I am importing cheerio that was already imported somewhere else.
Shouldn’t cheerio already be present in the pm converted tests?

looks like import cheerio from “./libs/cheerio.js”; is the culprit . But I need to to parse html to get antiforgery token… and regex feels bad

Hi @ovidiu.buligan!

Welcome to the community forums :wave:

If you’re looking only for a token value, I believe using the functionality of k6/html is possible.

There is an example of parsing HTML.

Also, there is an example of the scenario where a csrftoken is parsing on our test site.

vars["csrftoken"] = res

Let me know if that helps,

Hello , I ended up using regex to extract the csrftoken . with something similar to :

const requestVerificationToken = /<input\s+name=\"__RequestVerificationToken\"\s+type=\"hidden\"\s+value="([\w-]+)/.exec(body)[1]

I think it is faster than parsing html and works since we also control the html page that emmits csrftoken . Probably also consume less memory.

If efficiency is a concern, I would recommend using findBetween as it uses only a combination of indexOf and substring operations.

If you are using any script file and getting "Uncaught ReferenceError: x is not defined " which means ‘x’ is either a variable or a method which you are trying to use before declaring it using var keyword. This means that there is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope otherwise , it will endup throwing this ‘x’ is not defined error . This usually indicates that your library is not loaded and JavaScript does not recognize the ‘x’.

To solve this error: Load your library at the beginning of all your scripts.

There can be multiple other reasons for this issue:

  • Conflict with Other Libraries
  • Path to your library included is not correct
  • Llibrary file is corrupted
  • Working offline (when you use CDN)