I’d like to run 2 requests in the pre-request script, both are to generate an admin csrf token, I don’t want to duplicate them in every single test folder within several collections so I set up both request in the Collection and then call them in a “Create User” endpoint that requires admin rights, so far I’ve tried adding a setTimeout for both like this:
const logInPage = pm.collectionVariables.get("logInPage")
const adminLogInAction = pm.collectionVariables.get("adminLogInAction")
setTimeout(loginPageReq, 3000);
setTimeout(adminRequest, 5000);
function loginPageReq() {
pm.sendRequest(logInPage, function(err, response){
const $ = cheerio.load(response.text());
let csrfToken = $('[name=_csrf]').attr('value');
pm.globals.set("csrfToken", csrfToken);
})
};
function adminRequest() {
pm.sendRequest(adminLogInAction, function(err, response){
const $ = cheerio.load(response.text());
if ($('[csrf-header-name="X-CSRF-TOKEN"]').attr('csrf-token')){
const csrfToken = $('[csrf-header-name="X-CSRF-TOKEN"]').attr('csrf-token');
pm.globals.set("csrfToken", csrfToken);
} else {
const csrfToken = $('[name="_csrf"]').val();
pm.globals.set("csrfToken", csrfToken);
}
})
};
I’d like to run loginPageReq, wait 3 seconds and then run adminRequest, before running the CreateUser request.
Actual result: it waits 5 seconds, then executes both