postman, tutorial,

Sharing - Testing4Everyone - Postman - PreRequest & PostResponse

Testing4Everyone Testing4Everyone Follow Nov 28, 2024 · 2 mins read
Sharing - Testing4Everyone - Postman - PreRequest & PostResponse
Share this

In this short post, I would like to share you the script that in a video of Testing4Everyone channel. It is related to PreRequest and PostResponse in Postman tool to generate

Here is the script of PreRequest

function generateRandomUserName(length) {
    const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

function generateRandomPassword(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const specialCharacters = '!@#$%^&*()_+{}:<>?';
    
    let result = '';

    // Ensure at least one special character
    result += specialCharacters.charAt(Math.floor(Math.random() * specialCharacters.length));
    
    // Fill the rest of the password
    const charactersLength = characters.length;
    for (let i = 1; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

    // Shuffle the result to mix the special character in
    result = result.split('').sort(() => 0.5 - Math.random()).join('');

    return result;
}

pm.environment.set("username", generateRandomUserName(7))
pm.environment.set("password", generateRandomPassword(10));



Here is the script of PostResponse

pm.test("Save FistName and LastName value to Environment", function () {
    var jsonData = pm.response.json();
    let firstname = pm.environment.set("firstname", jsonData.firstname)
    let lastname = pm.environment.set("lastname", jsonData.lastname)
    console.log(jsonData.firstname)
    console.log(jsonData.lastname)
});
Join Newsletter
Get the latest news right in your inbox. We never spam!
Testing4Everyone
Written by Testing4Everyone
Blogger account, Youtuber account, who Donald wrote on my morning coffee!