RyanSchlomer.com

Sr QA Consultant

Automation Testing with Postman

Posted by:

|

On:

|

Automation Testing with Postman

In an earlier post, I created a budgeting API that will be used as a reference. I created some tests in Postman in the previous post that were mostly manual tests. Today, I will show how Postman can be used to automate some of the testing.

Postman’s Environment Variables

Environment variables can be used to pass data between the tests. For example, let’s say your Create Budget API creates a budget and returns the new budget id. You can then pass this budget id to your next test, a Get Budget request, to verify the budget was created.

Postman uses JavaScript, which is not really what I am familiar with, but there are ChatGPT and other online sites to help with coding.

Setting Environment Variables

You can set an environment variable in the Tests tab of a request using JavaScript code. For example, to save a newBudgetId from the API response:

var jsonData = pm.response.json();
pm.environment.set("newBudgetId", jsonData.id);  // Save the new ID

Using Environment Variables

Once set, these variables can be used in various places:

  • In the Endpoint URL: You can include the variable in the URL using double curly braces {{ }}.
    • Example: https://localhost:7232/api/Budget/ByBudgetId/{{newBudgetId}}
  • In the Tests Tab: You can retrieve the value of the environment variable using pm.environment.get() within your test scripts.
    • Example:
pm.test("Returned budget has correct ID", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql(pm.environment.get("newBudgetId"));
});
  • In the Request Body: You can also use environment variables in the request body.
    • Example:
{
  "id": {{newBudgetId}},
  "userId": {{userId}},
  "name": "{{name}}",
  "month": {{month}},
  "year": {{year}}
}

Benefits of Environment Variables

  • Reusability: Once set, the variable can be used across multiple requests and tests.
  • Dynamic Testing: Allows you to create tests that adapt based on previous responses.
  • Maintainability: Easier to update tests as you only need to change the variable’s value in one place.

Even if you’re not familiar with JavaScript, Postman’s scripting is relatively straightforward. As I mentioned above, there are resources like ChatGPT and other online sites to help you out.

Automated Response Validation with Postman

Postman excels in scenarios where you can validate API responses directly, without the need for external checks like SQL queries. This makes it ideal for automating tests that expect specific error messages.

Consider the test case named “Get Budget By Id — Invalid BudgetId -1,” where you expect a “Budget not found” message. You can automate this validation as follows:

pm.test("Budget not found.", function () {
    var textData = pm.response.text();
    pm.expect(textData).to.eql("Budget not found.");
});

Most of the negative tests could be automated in this way. That’s pretty simple.

Advanced Automation Techniques with Postman

While Postman excels at API testing, it lacks the capability to execute direct SQL queries for database validation. This limitation can make it challenging to automate tests that require database checks.

To overcome this, you can create custom API operations that interact with the database or read/write files. For example, I created an API operation named ExecuteWhitelistedQuery that allows me to run a set of predefined SQL queries. This approach adds a layer of security by only allowing whitelisted queries to be executed.

In my “Create Budget” test, I use this API operation to verify the number of budgets in the database.


//This calls ExecuteWhitelistedQuery api and returns the number of budgets in the database.
function getBudgetCount() {
    return new Promise((resolve, reject) => {
        pm.sendRequest("https://localhost:7232/api/Testing/ExecuteWhitelistedQuery/NumberOfBudgets", function (err, res) {
            if (err) {
                reject("Request failed: " + err);
            } else {
                var responseJson = res.json();
                var budgetCount = responseJson[0][0];
                    resolve(budgetCount);
              
            }
        });
    });
}

const jsonData = pm.response.json();

// Asynchronous test
pm.test("Number of budgets are correct", async function () {
    try {
        const externalBudgetCount = await getBudgetCount();
        
        const internalBudgetCount = Array.isArray(jsonData) ? jsonData.length : jsonData.budgets.length;

        pm.expect(externalBudgetCount).to.eql(internalBudgetCount, "Budget counts should match");
    } catch (error) {
        pm.expect.fail("Test failed due to error: " + error);
    }
});


pm.test("Every budget has necessary fields", function () {
    jsonData.forEach((item, index) => {
        ["id", "userId", "name", "month", "year"].forEach((property) => {
            if (!item.hasOwnProperty(property)) {
                console.log(`Item at index ${index} is missing property ${property}`);
            }
        });
    });
});

Your API could be further extended to accept string parameters for SQL queries or to perform file operations, enhancing your testing capabilities.

Conclusion

From using environment variables for dynamic testing to employing advanced techniques for database validation, Postman provides a versatile toolkit for testers. Even if you’re not well-versed in JavaScript, the platform’s user-friendly interface and the availability of online resources make it accessible for testers of all skill levels. By integrating custom API operations, you can even extend Postman’s capabilities to suit your specific testing needs