Using dynamic variable values in tests?

Hi There,

I’m relatively new to postman and API testing. I’m not able to get this solution to work, and I’m not sure why.

I’ve created a collection that does the following:

  1. Get bearer token
  2. Get a list widgets from the endpoint
  3. Create/POST a new widget using a random widget name
  4. Get a list widgets from the endpoint and test that the newly created widget’s name is returned.

In my collection’s ‘Pre-request Scripts’ tab, I’ve entered this:

uuid = require('uuid');
pm.globals.set('wname', 'widget' + uuid.v4());

Then in my get request (step 4 above), I have this set as one of my tests:

var wname = pm.globals.get('wname');
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include(wname);
});

This test always fails because I can see that it is looking for a name with a different random number than what was set in the ‘Pre-request Scripts’ above:

AssertionError: expected ‘[{“widget name”:“widgetaac95318-6272-4547-8e4d-974f1cc48616”}]’ to include ‘widgetb6a8ab70-33b9-448a-9a19-0638eee0882e’

Any idea what I may be doing wrong?

Thanks so much for your help and patience.

Ed