Last active
May 18, 2023 09:03
-
-
Save knbknb/b062a3c5bc6f591e0d9cb929f9f33f1a to your computer and use it in GitHub Desktop.
JS: Postman snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// postman snippet | |
// https://stackoverflow.com/questions/72098479/how-to-use-faker-variables-in-a-pre-request-script-in-postman | |
// You can't use the {{}} syntax in the pre-request and test tabs. | |
// The following will do: | |
var firstName = pm.variables.replaceIn("{{$randomFirstName}}"); // | |
// pre-request script to load faker library from CDN | |
// sets global/folder -level var "window" to the faker object | |
window = {}; | |
function loadFaker3(locale = "en"){ | |
const url = "https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js"; | |
pm.sendRequest(url, (error, response) => { | |
if (error || response.code !== 200) { | |
pm.expect.fail('Could not load external library'); | |
} | |
eval(response.text()); | |
// This is where you can set the locale. See faker.js docs for all available locales. | |
window.faker.locale = locale; | |
setCollVars() | |
}); | |
} | |
loadFaker3("fr") | |
function setCollVars(){ | |
pm.collectionVariables.set("contact_name", window.faker.name.firstName() + " " + window.faker.name.lastName()); | |
pm.collectionVariables.set("contact_phoneNumber",window.faker.phone.phoneNumber()); | |
pm.collectionVariables.set("contact_streetAddress",window.faker.address.streetAddress()); | |
} | |
// window var was set on folder level | |
function constructFakeData(name = "(formname)", n = 2){ | |
let fakeDataArr = []; | |
const fakeDataObj = {}; | |
// Loop n times to generate n records of fake data | |
for (let i = 0; i < n; i++) { | |
// Generate a new fake data object | |
let acr = window.faker.hacker.abbreviation(); | |
const name = pm.variables.replaceIn(`{{$randomFirstName}} ${acr}`) | |
acr = name.substr(0, 2) + acr; | |
const data = { | |
program_name: name, | |
program_acronym: acr, | |
comment: `${acr}: ${window.faker.lorem.sentence()}`, | |
}; | |
fakeDataArr.push(data); | |
} | |
fakeDataObj[name] = fakeDataArr; | |
return fakeDataObj; | |
} | |
const fakeData = constructFakeData(name = "program", n = pm.collectionVariables.get("fake_n_programs")); | |
const req_body = JSON.stringify(fakeData.program[0]); | |
pm.collectionVariables.set("req_body", req_body) | |
console.info(JSON.stringify(fakeData)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// postman snippet | |
// test for the presence of a Querystring Pramater in response | |
let qsparam = 'filter[id]' | |
pm.test(`query string request-param '${qsparam}' appears in response`, function () { | |
var jsonData = pm.response.json(); | |
let query = {}; | |
pm.request.url.query.all().forEach((param) => { query[param.key] = param.value}); | |
pm.expect(jsonData.items[0].id).to.eql(Number.parseInt(query[qsparam])); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment