Postman scripting: why it is not working?












0















I have a couple of requests as part of my collection and wanted to set an environment variable with a value from the json response output. This variable is not defined in Manage Environments section. Is it mandatory that I would have to first declare the environment variable under Manage Environments section? If I do declare it in the Manage section, then it is working.



const jsonData = pm.response.json();
pm.environment.set('sessionId', jsonData.sessionId);


My other question is I'm at loss on why this is passing? I have the following code snippet in one of my requests.



{
"sessionid": "15c3f72e-788f-423f-b1cc-5dca503ae859",
"expires": "2019-02-17T20:57:20.24234222-08:00",
"cookievalue": "MySession"
}

pm.test("Check if session id is returned"), function() {
pm.expect(pm.response.text()).to.include("sessionid2");
}


I'm surprised on why this is working? It should fail since I don't have sessionid2 in the json resposnse. I tried all other ways including pm.response.json() and nothing works. Btw, how do I test to see if the key exists in the json response and not necessarily to check the value since the session id value may change each time.



Other than the docs, can somebody point me to a good resource to learn more about postman scripting?



Please help.










share|improve this question





























    0















    I have a couple of requests as part of my collection and wanted to set an environment variable with a value from the json response output. This variable is not defined in Manage Environments section. Is it mandatory that I would have to first declare the environment variable under Manage Environments section? If I do declare it in the Manage section, then it is working.



    const jsonData = pm.response.json();
    pm.environment.set('sessionId', jsonData.sessionId);


    My other question is I'm at loss on why this is passing? I have the following code snippet in one of my requests.



    {
    "sessionid": "15c3f72e-788f-423f-b1cc-5dca503ae859",
    "expires": "2019-02-17T20:57:20.24234222-08:00",
    "cookievalue": "MySession"
    }

    pm.test("Check if session id is returned"), function() {
    pm.expect(pm.response.text()).to.include("sessionid2");
    }


    I'm surprised on why this is working? It should fail since I don't have sessionid2 in the json resposnse. I tried all other ways including pm.response.json() and nothing works. Btw, how do I test to see if the key exists in the json response and not necessarily to check the value since the session id value may change each time.



    Other than the docs, can somebody point me to a good resource to learn more about postman scripting?



    Please help.










    share|improve this question



























      0












      0








      0








      I have a couple of requests as part of my collection and wanted to set an environment variable with a value from the json response output. This variable is not defined in Manage Environments section. Is it mandatory that I would have to first declare the environment variable under Manage Environments section? If I do declare it in the Manage section, then it is working.



      const jsonData = pm.response.json();
      pm.environment.set('sessionId', jsonData.sessionId);


      My other question is I'm at loss on why this is passing? I have the following code snippet in one of my requests.



      {
      "sessionid": "15c3f72e-788f-423f-b1cc-5dca503ae859",
      "expires": "2019-02-17T20:57:20.24234222-08:00",
      "cookievalue": "MySession"
      }

      pm.test("Check if session id is returned"), function() {
      pm.expect(pm.response.text()).to.include("sessionid2");
      }


      I'm surprised on why this is working? It should fail since I don't have sessionid2 in the json resposnse. I tried all other ways including pm.response.json() and nothing works. Btw, how do I test to see if the key exists in the json response and not necessarily to check the value since the session id value may change each time.



      Other than the docs, can somebody point me to a good resource to learn more about postman scripting?



      Please help.










      share|improve this question
















      I have a couple of requests as part of my collection and wanted to set an environment variable with a value from the json response output. This variable is not defined in Manage Environments section. Is it mandatory that I would have to first declare the environment variable under Manage Environments section? If I do declare it in the Manage section, then it is working.



      const jsonData = pm.response.json();
      pm.environment.set('sessionId', jsonData.sessionId);


      My other question is I'm at loss on why this is passing? I have the following code snippet in one of my requests.



      {
      "sessionid": "15c3f72e-788f-423f-b1cc-5dca503ae859",
      "expires": "2019-02-17T20:57:20.24234222-08:00",
      "cookievalue": "MySession"
      }

      pm.test("Check if session id is returned"), function() {
      pm.expect(pm.response.text()).to.include("sessionid2");
      }


      I'm surprised on why this is working? It should fail since I don't have sessionid2 in the json resposnse. I tried all other ways including pm.response.json() and nothing works. Btw, how do I test to see if the key exists in the json response and not necessarily to check the value since the session id value may change each time.



      Other than the docs, can somebody point me to a good resource to learn more about postman scripting?



      Please help.







      postman postman-collection-runner






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 15:13







      user1893003

















      asked Nov 20 '18 at 15:07









      user1893003user1893003

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The first question regarding saving an environment variable, you would need to create an environment file first, in order for it to know where to save the variable. You could use the pm.globals.set() function instead if you didn't want to create a file first.



          For the second question, it looks like your test syntax is wrong as you've closed off the test by adding the extra ) after the test name.



          Using something like this instead should work:



          pm.test("Check if session id is returned", function() {
          pm.expect(pm.response.json()).to.include.key("sessionid");
          })





          share|improve this answer
























          • thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

            – user1893003
            Nov 20 '18 at 19:33













          • Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

            – Danny Dainton
            Nov 20 '18 at 21:34











          • Awesome. Can you let me know the github url?

            – user1893003
            Nov 21 '18 at 2:09











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395923%2fpostman-scripting-why-it-is-not-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The first question regarding saving an environment variable, you would need to create an environment file first, in order for it to know where to save the variable. You could use the pm.globals.set() function instead if you didn't want to create a file first.



          For the second question, it looks like your test syntax is wrong as you've closed off the test by adding the extra ) after the test name.



          Using something like this instead should work:



          pm.test("Check if session id is returned", function() {
          pm.expect(pm.response.json()).to.include.key("sessionid");
          })





          share|improve this answer
























          • thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

            – user1893003
            Nov 20 '18 at 19:33













          • Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

            – Danny Dainton
            Nov 20 '18 at 21:34











          • Awesome. Can you let me know the github url?

            – user1893003
            Nov 21 '18 at 2:09
















          0














          The first question regarding saving an environment variable, you would need to create an environment file first, in order for it to know where to save the variable. You could use the pm.globals.set() function instead if you didn't want to create a file first.



          For the second question, it looks like your test syntax is wrong as you've closed off the test by adding the extra ) after the test name.



          Using something like this instead should work:



          pm.test("Check if session id is returned", function() {
          pm.expect(pm.response.json()).to.include.key("sessionid");
          })





          share|improve this answer
























          • thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

            – user1893003
            Nov 20 '18 at 19:33













          • Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

            – Danny Dainton
            Nov 20 '18 at 21:34











          • Awesome. Can you let me know the github url?

            – user1893003
            Nov 21 '18 at 2:09














          0












          0








          0







          The first question regarding saving an environment variable, you would need to create an environment file first, in order for it to know where to save the variable. You could use the pm.globals.set() function instead if you didn't want to create a file first.



          For the second question, it looks like your test syntax is wrong as you've closed off the test by adding the extra ) after the test name.



          Using something like this instead should work:



          pm.test("Check if session id is returned", function() {
          pm.expect(pm.response.json()).to.include.key("sessionid");
          })





          share|improve this answer













          The first question regarding saving an environment variable, you would need to create an environment file first, in order for it to know where to save the variable. You could use the pm.globals.set() function instead if you didn't want to create a file first.



          For the second question, it looks like your test syntax is wrong as you've closed off the test by adding the extra ) after the test name.



          Using something like this instead should work:



          pm.test("Check if session id is returned", function() {
          pm.expect(pm.response.json()).to.include.key("sessionid");
          })






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 17:56









          Danny DaintonDanny Dainton

          4,3862921




          4,3862921













          • thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

            – user1893003
            Nov 20 '18 at 19:33













          • Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

            – Danny Dainton
            Nov 20 '18 at 21:34











          • Awesome. Can you let me know the github url?

            – user1893003
            Nov 21 '18 at 2:09



















          • thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

            – user1893003
            Nov 20 '18 at 19:33













          • Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

            – Danny Dainton
            Nov 20 '18 at 21:34











          • Awesome. Can you let me know the github url?

            – user1893003
            Nov 21 '18 at 2:09

















          thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

          – user1893003
          Nov 20 '18 at 19:33







          thanks @danny. I'm not seeing an option to create a environment file from within Postman. Please advise. Also, can you please refer to any other useful blogs other than the docs for getting to know more about Postman and possibly Newman?

          – user1893003
          Nov 20 '18 at 19:33















          Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

          – Danny Dainton
          Nov 20 '18 at 21:34





          Have you looked at the Postman documentation, YouTube channel...google etc. Its a widely used tool so there is a wealth of information out there. I have a github repo we different postman related things if you want yo check that out too.

          – Danny Dainton
          Nov 20 '18 at 21:34













          Awesome. Can you let me know the github url?

          – user1893003
          Nov 21 '18 at 2:09





          Awesome. Can you let me know the github url?

          – user1893003
          Nov 21 '18 at 2:09




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395923%2fpostman-scripting-why-it-is-not-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word