Введение в postman

Troubleshooting common test errors

When you encounter errors or unexpected behavior in your test scripts, the Postman Console can help you to identify the source. By combining debug statements with your test assertions, you can examine the content of the HTTP requests and responses, as well as Postman data items such as variables.

Click Console at the bottom left of Postman to open it.

You can log the value of a variable or response property:

You can log the type of a variable or response property:

You can generally use console logs to mark code execution, sometimes known as «trace statements»:

Assertion deep equality error

You may encounter the . For example, this would arise with the following code:

This happens because the test is comparing a number to a string value. The test will only return true if both the type and value are equal.

JSON not defined error

You may encounter the issue. This typically happens when you are attempting to reference a JSON object that has not been declared or is outside the scope of your test code.

Make sure that any code setting your response data to a variable is accessible to all test code, for example in this case moving to before the first would make it available to both test functions.

Assertion undefined error

You may encounter the issue. Typically this happens when you are referring to a property that does not exist or is out of scope.

In the above example, if you see , this indicates that the property is not defined in the object.

Test not failing

There may be occasions where you expect a test to fail and it doesn’t.

Make sure your test code is syntactically correct and try sending your request again.

Network information

Postman will display network data when your API returns a response.

Hover over the globe icon to see the local and remote IP addresses for the request you sent.

When you make requests, the Network information will display a padlock and will include detail of any certificate verification.

If you have SSL verification enabled in the global settings and verification fails, Postman will indicate the error in the response area. Click the link to disable verification globally and immediately run the request again. If SSL is disabled globally but enabled for your request, you will see the error and a link to open the console.

If you have SSL verification turned off and your request returns a certificate verification error, you will see the detail in the Network information pop-up.

For requests that return data successfully but with a certificate verification failure, the console will display a warning.

Parsing response body data

In order to carry out assertions on your responses, you will first need to parse the data into a JavaScript object that your assertions can use.

To parse JSON data, use the following syntax:

To parse XML, use the following:

To parse CSV, use the CSV parse utility:

To parse HTML, you can use cheerio:

Handling responses that don’t parse

If you cannot parse the response body to JavaScript because it’s not formatted as JSON, XML, HTML, CSV, or any other parsable data format, you can still make assertions on the data.

You can test if the response body contains a string:

This does not tell you where the string was encountered because it carries out the test on the whole response body. You can also test if a response matches a string (which will typically only be effective with short responses):

Viewing responses

The Postman Body tab gives you several tools to help you understand the response quickly. You can view the body in one of three views — pretty, raw, and preview.

Pretty

The pretty mode formats JSON or XML responses so they are easier to view. Nobody wants to scroll through a minified single line JSON response looking for that elusive string! Links inside the pretty mode are highlighted and clicking on them can load a GET request in Postman with the link URL. For navigating large responses, click on the down-pointing triangles (▼) on the left to collapse large sections of the response.

For Postman to automatically format the body, make sure the appropriate Content-Type header is returned. If not, you can force formatting through JSON or XML. You can force JSON formatting under the General tab in the SETTINGS modal. To do so. select «JSON» from the «Language detection» dropdown.

Finding items in responses: You can use CMD/CTRL + F to open the search bar, and CMD/CTRL + G to scroll through results.

The raw view is a large text area with the response body. It can indicate whether your response is minified.

Preview

The preview tab renders the response in a sandboxed iframe. Some web frameworks by default return HTML errors and the preview mode is especially helpful. Due to iframe sandbox restrictions, JavaScript and images are disabled in the iframe.

You can maximize the body to occupy the whole Postman window. Use this view if you plan to work with the response for an extended period of time.

If your API endpoint returns an image, Postman will detect and render it automatically. For binary response types, you should select “Send and download” which will let you save the response to your hard disk. You can then view it using the appropriate viewer. This gives you the flexibility to test audio files, PDFs, zip files, or anything that the API throws at you.

Getting started with tests

To try writing a test script for the first time, open a request in your Postman app and open the Tests tab. Enter the following JavaScript code:

This code uses the library to run the method. The text string will appear in the test output. The function inside the test represents an assertion. Postman tests can use Chai Assertion Library BDD syntax, which provides options to optimize how readable your tests are to you and your collaborators. In this case, the code uses BDD chains to express the assertion.

This test checks the response code returned by the API. If the response code is , the test will pass, otherwise it will fail. Click Send and check the Test Results output in the response area.

Try changing the status code in the assertion code and running again to see how test results appear differently when they pass or fail.

You can structure your test assertions in a variety of ways to suit your logic and preference in terms of how you want the results to output. The following code is an alternative way of achieving the same test as the one above using the syntax:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock
detector