Skip to content

Latest commit

 

History

History
113 lines (79 loc) · 6.13 KB

part2-FinalCountdown.md

File metadata and controls

113 lines (79 loc) · 6.13 KB

Final Countdown

< Previous section: Prepare for launch

Now that we've tried out collection runs, variables, and test snippets, let's explore how to pass data between requests and change the order of execution.

1. Import some tests

  • In the Postman app, click New+, open the Templates, and find "Intro to writing tests - with examples".
  • Import the Intro to writing tests collection using the Run in Postman button.
  • This collection has a variety of test types organized into folders. It's a great place to explore for future learning around tests.

Open the collection web documentation for future reference.

2. Use variables to pass data between requests

Test scripts let you pass data around in Postman, so you can chain tests together with shared data.

  • Create an environment to use for your tests. In the top right click the eye button—the environment quick look. In the Environments section, click Add. Give your environment the name workshop, click Add, and close the Manage Environments modal.
  • Use the dropdown at the top right to select the workshop environment you just created. With this selected, any requests we run will reference variables in this environment.
  • In the collection Integration tests folder > Using variables sub-folder, send the request Bitcoin exchange rate and inspect the response.
  • Check out the JavaScript code under the request Tests tab—it demonstrates how to get and set variables from a script using the .set method. It's saving a var to the environment. Open the environment quick look—it should be in there now because we ran the request.
  • Open the request Echo the exchange rate. Notice that the URL references the bitcoinRate variable set in the previous request tests. Send the request to see the response. The request used the value returned from the previous one.

3. Change the request execution

Your scripts can alter the flow of request execution when you use the collection runner.

  • Import another collection from API Network, search "learn" again and select the learning profile from before, this time importing "API Learner".
  • Open the Runner and select the API Learner collection. Run the collection and notice the flow of execution. All four requests should run.
  • Open the first request GET data and Send it. The response includes a field message and a text string value.
  • Select the Tests tab. Enter some JavaScript to test the length of the message (the conditional test should return true):
let jsonData = pm.response.json();
let responseField = jsonData.message;
//check the length of the text
if (responseField.length < 10) {
  //note - setnextrequest only works when you use collection runner
  postman.setNextRequest(null);
}
  • Make sure you Save the request so that your code will execute when you run the collection.
  • Open the Runner again and select the API Learner collection. Run the collection—it should run the GET request followed by the PUT request, skipping the requests in-between.
  • Alter the code to stop execution after the first request by passing null to the setNextRequest method:
if (responseField.length > 10) {
  postman.setNextRequest(null);
}
  • Save the request. Open the Runner and select the API Learner collection again. Start the run—it should stop after one request.
  • Optional: try changing the conditional test to return false and running the collection again (e.g. change > to <)—all requests should run.

4. Share data between collections

You can pass data between requests even if they are not in the same collection. Let's try that using environments.

  • Open the Intro to Writing Tests collection > Integration Tests > Bitcoin exchange rate request again. In the Tests remember that it sets the bitcoinRate environment variable. Make sure the workshop environment is selected.
  • Open the API Learner collection > POST data request.
  • In the Body of the request, add a new field, with the value referencing the environment variable:

{% highlight javascript%} {% raw %} { "name": "sue", "value": "{{bitcoinRate}}" } {% endraw %} {% endhighlight %}

  • Send the request—you will see the variable value set by the request in the other collection was sent to this request via the environment.

Variable scopes determine where variable values are available within Postman, as well as which one takes precedence if you different vars with the same name.

winter solstice art

Recap

In this section we tried out:

  • used the collection runner
  • chained requests
  • altered execution order
  • passed data between requests
  • set variables from scripts
  • accessed variables in the request body

Kahoot

Go to kahoot.it and enter the pin shown on the screen—answer the questions as quick as you can to win an Amazon gift card!

Additional resources

Check out the docs:

And more templates you can try out in Postman:

Next steps

Next up: Liftoff