Hi, I am wondering if there is a good way to search if a particular string exists in response body. I am talking about like a global search in the entire parsed response body without knowing where it might be exactly. I looked at Selection and couldn’t find a way.
Javascript search() or in operator seems to be not working in k6.
Please let me know. Thanks!
Hi @yli , you can use includes() function in the body part like this:
export default function () {
const response1 = http.get('http://myapp:5000/', {tags: {name: '/'}});
check(response1, {
'verify homepage text': (r) => r.body.includes('Python is fun'),
"status is 200": (r) => r.status === 200
});
sleep(0.2)
};
Another way to search a value in a body part that i know is this:
const cheerio = require('cheerio');
function validateElement() {
const res = http.get('https://loadimpact.com/');
const $ = cheerio.load(res.body);
const title = $('head title').text();
check(title, {
'has correct title': () => title == 'Load Impact is now k6',
});
console.log(title);
return title;
}
I share you this info i hope it helps:
Greetings
1 Like
They both work perfectly. You are awesome.
The includes() method was actually documented here (Checks) but it’s not searchable and I missed it.
Thanks again!
1 Like
This might be because includes is actually part of JavaScript
1 Like