Use Newman/Postman to Synthetically Monitor API Endpoints

Use Newman/Postman to Synthetically Monitor API Endpoints

Bitbar Monitoring provides a way to monitor mobile applications and APIs using real mobile devices in various location around the world with the option of using real carrier networks. The service supports running scripted mobile tests using eg. the Appium testing framework. Other supported frameworks are XCUITest for iOS and Espresso for Android applications.

Because the way the scripted tests are executed, other approaches for testing mobile applications or endpoints are supported. It is possible to monitor API endpoints using specialized tools that are already used by developers. This example shows how to use Newman to run Postman collection runs at Bitbar Monitoring.

Postman

Postman is a graphical tool for writing API requests to test REST services using the JavaScript programming language. The same language that most websites and many mobile applications are nowadays implemented. Postman allows the user to create requests and to store the resulting object for further testing of response values. The tester writes assertions against the receive response object evaluating to passing or failing test scenarios. Postman facilitates the creation of requests and evaluating the received responses.

Newman is the command line companion for running Postman collections. Collections can be viewed as test suites.

Example Monitoring Test

To use Postman and Newman tests, you need to create a scripted test check in Bitbar Monitoring. The scripted test run is a generic executor run, where a zip file is uploaded and unpacked upon test run execution. The unpacked content is looked for a `pom.xml` file to start a Maven execution (`mvn start`). If this file is not available then a shell file `run-test.sh` is looked for. If this is found then it is executed with `sh run-test.sh`.

Specifically this second approach allows the tester to freely start and do any kind of tests.

First, let’s install Newman by following their getting started guide. The following installs Newman and sets it to our current path.

```
npm install newman
export PATH=$PATH:$(pwd)/node_modules/.bin
```

Second, we can clone the test repository containing the Postman test suite that we are interested in executing. In this case the test suite is in Newman’s Github repository. Using git clone is an easy way of making sure that the test suite is always up to date. Optionally, we could also just add the test directory as part of the zip to be uploaded. This sample contains three different requests, but could of course contain many more. Monitoring runs have no limits to the number of API calls.

```
git clone https://github.com/postmanlabs/newman.git
```

Third, to get access to Newman’s test results, we want to create a Maven results directory.

```
mkdir -p target/surefire-reports
```

Finally, we need to call Newman to start executing the above cloned test suite.

```
newman run -k --reporters "cli,junit" --reporter-junit-exporter target/surefire-reports newman/examples/sample-collection.json;
```

The passed parameters are for formatting the test results in the JUnit test syntax and giving the location of the test suite (collection).

Below is a sample script that can be used as the `run-test.sh` script that should be packaged for upload to Monitoring check.

```
#!/bin/bash

# Install newman
npm config set proxy null
npm config set https-proxy null
npm install newman
export PATH=$PATH:$(pwd)/node_modules/.bin

echo "Updated path is: $PATH"

# clone the newman repo if it's not present (should be by default as
# part of upload)
if [ ! -d "newman" ]; then
echo "Cloning Newman repository"
git clone https://github.com/postmanlabs/newman.git
fi

# create Maven results directory if it does not exist
LOGS_DIR="target/surefire-reports"
if [ ! -d "$LOGS_DIR" ]; then
echo "Creating surefire-reports directory: $LOGS_DIR"
mkdir -p "$LOGS_DIR"
fi

# start sample test
export HTTP_PROXY=http://127.0.0.1:8888
export HTTPS_PROXY=http://127.0.0.1:8888
newman run -k --reporters "cli,junit" --reporter-junit-exporter "$LOGS_DIR" newman/examples/sample-collection.json;
```

Next step is to zip this test script into a package and create the Monitoring check. Running `zip -r newman-test.zip` in the directory with the test suite and the test script creates a nice package for upload.

```
.
├── newman
│ └── examples
│ ├── find-unique-urls-in-run.js
│ ├── parallel-collection-runs.js
│ ├── read-collection-from-file.js
│ ├── reports
│ │ ├── html-reporter-template-data.json
│ │ ├── sample-collection-report.html
│ │ ├── sample-collection-report.json
│ │ └── sample-collection-report.xml
│ ├── run-collections-in-directory.js
│ ├── sample-collection.json
│ └── v1.postman_collection.json
└── run-test.sh
```

Creating the Check

Let’s create the Monitoring check for running the Newman test. If you are new to mobile synthetic monitoring, you can check out how to get started with Bitbar Monitoring

Create a monitoring test in Bitbar Monitoring

It does not really matter which mobile environment is selected in the next step, as the test is not using any of the devices.

Select the locations and carriers where your APIs need to be monitored.

Lastly only the check run interval and actions to be taken in case of incidents need to be defined.

The API results become visible in the check run view inside of Monitoring service with all HTTP Archive file information about called endpoints, the call durations and the size of transferred data up or down.

 


Top Must-Test Global Devices for Better Mobile App Compatibility

Get a sheet of the most popular global smartphones and learn what devices every app developer should test against and verify the compatibility.

Download

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close