آزمایش Web APIs توسط Postman - قسمت چهارم - نوشتن آزمونها و اسکریپتها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۲/۱۸ ۱۰:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
}); | نوع بررسی | Response assertions |
| بررسی مقدار status code دریافتی از سرور با مقدار مورد انتظار | pm.response.to.have.status(200); |
| آیا status code دریافتی، معادل یکی از مقادیر مشخص شدهاست؟ | pm.expect(pm.response.code).to.be.oneOf([201,202]); |
| آیا status name دریافتی از سرور، معادل عبارت مشخصی است؟ | response.to.have.status("Created"); |
| مقایسهی مقدار responseTime با مقدار مورد انتظار | pm.expect(pm.response.responseTime).to.be.below(200); |
| آیا هدر تنظیم شدهی توسط response، دارای کلید مورد انتظار است؟ | pm.response.to.have.header("X-Cache"); |
| آیا هدر تنظیم شدهی توسط response، دارای کلید و مقدار مشخصی است؟ | pm.response.to.have.header("X-Cache", "HIT"); |
| آیا نام یکی از کوکیهای تنظیم شدهی توسط response، معادل مقدار مورد انتظار است؟ | pm.expect(pm.cookies.has("sessionId")).to.be.true; |
| آیا نام و مقدار یکی از کوکیهای تنظیم شدهی توسط response، معادل مقادیر مشخصی هستند؟ | pm.expect(pm.cookies.get("sessionId")).to.equal("abcb9s"); |
| آیا بدنهی response، دقیقا معادل مقدار مشخص است؟ | pm.response.to.have.body("OK"); |
|
آیا بدنهی response، حاوی مقدار مشخصی است؟ | pm.expect(pm.response.text()).to.include("Order placed."); |
{
"id": 12,
"name": "DNT",
"isDeleted": false,
"prefs": {
"comments": "members",
"voting": "disabled"
}
} pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("DNT");
pm.expect(jsonData.prefs.voting).to.eql("disabled");
pm.expect(jsonData.isDeleted).to.eql(false);
}); pm.test("Post should be created", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("DNT");
pm.expect(jsonData.isDeleted).to.eql(false);
});
pm.test("Post's voting feature should be disabled", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.prefs.voting).to.eql("disabled");
}); const jsonData = pm.response.json();
pm.test("Post should be created", function () {
pm.expect(jsonData.name).to.eql("DNT");
pm.expect(jsonData.isDeleted).to.eql(false);
});
pm.test("Post's voting feature should be disabled", function () {
pm.expect(jsonData.prefs.voting).to.eql("disabled");
});
const jsonData = pm.response.json(); console.log(jsonData);
pm.environment.set("newId", "name " + parseInt(Math.random() * 10000)); https://localhost:5001/users/{{newId}} pm.test("Test Name", function(){
let a= {
"name" : "Vahid"
};
let b= {
"name" : "Vahid"
};
pm.expect(a).to.eql(b);
}); | عملکرد | متد |
| تبدیل XML به شیء JSON | let jsonObject = xml2Json(responseBody); |
| تبدیل JSON به شیء JSON | let jsonData = pm.response.json(); |
| تبدیل بدنهی response به متن ساده | let text = pm.response.text(); |
تبدیل HTML بدنهی response به یک شیء از نوع cheerio؛ مانند:let titleText = html.find('h1').text(); | let html = cheerio(responseBody); |
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Status code is 200", function () {
pm.expect(pm.response.code).to.equal(200);
});
pm.test("Request is successful", function () {
pm.response.to.be.succes;
}); // Status code is in the 2XX range
pm.test("Request results in a client error", function () {
pm.response.to.be.clientError;
}); // Status code is in the 4XX range
pm.test("Request results in a Not Found error", function () {
pm.response.to.be.notFound;
}); // 404
pm.test("Status code is 200 or 204", function () {
pm.expect([200, 204]).to.include(pm.response.code);
}); pm.test("Response has Content-Type header", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("Response has Content-Type header with application/json; charset = utf - 8 as value", function () {
pm.response.to.have.header(
'Content-Type',
'application/json; charset=utf-8');
}); pm.test("Response has a non-empty body", function () {
pm.expect(pm.response.text()).not.empty;
});
pm.test("Response has a non-empty body", function () {
pm.expect(pm.response.json()).not.empty;
});
pm.test("Response has a non-empty body", function () {
pm.response.to.have.body();
});
pm.test("Response has a non-empty body", function () {
pm.response.to.have.jsonBody();
}); var updatedAuthor = pm.response.json();
pm.test("Author properties have been updated", function () {
pm.expect(updatedAuthor.firstName).to.equal("Vahid");
pm.expect(updatedAuthor.lastName).to.equal("N");
});