Files
bruno/packages/bruno-tests/collection/scripting/inbuilt modules/cheerio/cheerio.bru
Andreas Wirth 9a21eec1b9 Bugfix: Add cheerio and xml2js modules to post-response scripts (#4516)
* Bugfix: Add cheerio and xml2js modules to post-response scripts
* chore: improved cheerio and xml2js test
---------

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
2025-04-17 01:31:53 +05:30

60 lines
1.5 KiB
Plaintext

meta {
name: cheerio
type: http
seq: 1
}
post {
url: https://echo.usebruno.com
body: text
auth: none
}
body:text {
<h2 class="title">Hello Bruno!</h2>
}
script:pre-request {
const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello pre-request!');
$('h2').addClass('welcome');
bru.setVar("cheerio-test-pre-request", $.html());
}
script:post-response {
const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello post-response!');
$('h2').addClass('welcome');
bru.setVar("cheerio-test-post-response", $.html());
}
tests {
const cheerio = require('cheerio');
test("cheerio html - from pre request script", function() {
const expected = '<html><head></head><body><h2 class="title welcome">Hello pre-request!</h2></body></html>';
const html = bru.getVar('cheerio-test-pre-request');
expect(html).to.eql(expected);
});
test("cheerio html - from post response script", function() {
const expected = '<html><head></head><body><h2 class="title welcome">Hello post-response!</h2></body></html>';
const html = bru.getVar('cheerio-test-post-response');
expect(html).to.eql(expected);
});
test("cheerio html - from tests", function() {
const expected = '<html><head></head><body><h2 class="title">Hello Bruno!</h2></body></html>';
const $ = cheerio.load(res.body);
expect($.html()).to.eql(expected);
});
}