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 go.k6.io/k6/js/common.Bind.func1 (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
.html()
.find("input[name=csrftoken]")
.first()
.attr("value"); 

Let me know if that helps,
Cheers

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.