k6

Postman global variables pm.globals

I am wondering how to work with global / environment variables in tests converted from postman to k6.

In this test, we are doing a Post, setting the pm.globals . We want that variable to be accessible in the address of the Delete request.

What I am looking for is we do not support this yet or a workaround / best practice

  group('post then delete', function() {
    
    postman[Request]({
      name: "Create (register) a Gateway",
      id: "4ef66998-e8c6-4848-9e09-2c8bfcdb80ae",
      method: "POST",
      address: `http://${pa_api_host}/api/gateways`,  // getting a value from a constant works
      data:
        '{\r\n  "gatewayId": "Created-This-With-Postman-({{$guid}})",\r\n  ...}',
      headers: {
        "Content-Type": "text/json"
      },
      post(response) {
        pm.test("Create new Gateway returns Status code 201", function() {
          pm.response.to.have.status(201);
        });
  
        pm.test("Response contains gatewayId", function() {
          pm
            .expect(pm.response.json().gatewayId)
            .to.be.an("string").that.is.not.empty;
  
          var gatewayIdFromResponse = pm.response.json().gatewayId;
          pm.globals.set("gatewayId", gatewayIdFromResponse); // pm.globals.set is not working
        });
      }
    });

    // pm.globals.set does not work, get does not seem to work either
    let gatewayId = pm.globals.get('gatewayId');

    console.log(JSON.stringify(pm.globals)); // empty object

    postman[Request]({
      name: "Utility: Delete a gateway from the Protocol Adatper",
      id: "969028fa-7401-4e9d-ac7f-12e5e207c08c",
      method: "DELETE",
      address: `http://${pa_api_host}/api/gateways/${gatewayId}`, // doesn't work
      data: {},
      headers: {
        "Content-Type": "application/json"
      },
      post(response) {
        pm.test("Delete Gateway used for Postman testing", function() {
          pm.expect(pm.response.code).to.eq(200);
        });
      }
    });
  


  });

This looks like a postman-to-k6 bug, can you file an issue there.

As a quick workaround you can have a variable in the scope of the function and set in the post and than just concatenate it to the end of the address in the Delete. You might need to urlencode it though, if there are any strange symbols in it .

1 Like

I tried to mimic what you’re doing @Murat and the following script works for me with latest version of postman-to-k6. It prints Yours Truly in the terminal as expected. If you you can’t get your script working with the latest version of the converter then please file an issue in github as suggested by @mstoykov.

import { group } from "k6";
import "./libs/shim/core.js";
import "./libs/shim/expect.js";

const Request = Symbol.for("request");

export default function() {
 group('Get slideshow author', function() {
    postman[Request]({
      name: "Get JSON",
      method: "GET",
      address: `https://httpbin.org/json`,
      post(response) {
        pm.test("Get JSON returns Status code 200", function() {
          pm.response.to.have.status(200);
        });
  
        pm.test("Response contains author", function() {
          pm
            .expect(pm.response.json().slideshow.author)
            .to.be.an("string").that.is.not.empty;
  
          var author = pm.response.json().slideshow.author;
          pm.globals.set("slideshowAuthor", author);
        });
      }
    });

    let slideshowAuthor = pm.globals.get('slideshowAuthor');
    console.log(slideshowAuthor);
  });
}
1 Like

Thanks all! Updating solved the problem.

1 Like