Cuando son guardadas hay que guardarlas con JSON.stringify() y extraerlas con JSON.parse()
Es una serie de key-value pairs, te permiten customizar los requests usando variables que pueden ser cambiadas facilmente
//guardar variables pm.environment.set("variable_key", "variable_value"); //recuperar variable pm.environment.get("variable_key");
<span class="token comment">//guardar arrays o objetos</span> <span class="token keyword">var</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span> pm<span class="token punctuation">.</span>environment<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">"array"</span><span class="token punctuation">,</span> JSON<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//recuperar array o objeto</span> <span class="token keyword">var</span> array <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>pm<span class="token punctuation">.</span>environment<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">"array"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//borrar variable</span> pm<span class="token punctuation">.</span>environment<span class="token punctuation">.</span><span class="token function">unset</span><span class="token punctuation">(</span><span class="token string">"variable_key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Las globals son variables que estan accesibles en todos los scopes.
//colocar global variable
pm.globals.set("variable_key", "variable_value");
//sacar variable
pm.globals.unset("variable_key");
- pm - Variable PostMan, permite acceder a todo lo relacionado con postman
- pm.response - Contiene la respuesta del request
- .to.have.status(200)
- pm.environment.get(‘nombre del environment’) - obtiene una variable de environment
- pm.expect(valor).to.equal(valor)
- pm.response - Contiene la respuesta del request
Tests
pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error");
pm.test("response must be valid and have a body", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants // assert that the response has a valid JSON body pm.response.to.be.withBody; pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed }); pm.expect(pm.response.text()).to.include("string_you_want_to_search"); pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); });