先来回顾一下之前写过的关于RuoYi后台管理系统项目实战相关的几篇文章:
测试项目实战----RuoYi后台管理系统
项目实战-RuoYi后台管理系统-用户管理测试点设计
项目实战-RuoYi后台管理系统-登录相关接口分析
Docker搭建webdis用于提供api查询redis中的数据
项目实战-RuoYi后台管理系统-用Postman实现登录接口的自动化
项目实战-RuoYi后台管理系统-用Python基于图像识别技术处理登录页面的验证码
由于经常看到大家苦于没有实战的项目用来练习,因此才找了这么个开源的项目搭建给大家练练手,在以前的文章中,我有教大家怎么去简单的分析用户管理模块的测试点设计,以及从获取验证码到登录的接口用例的设计。但是之前并没有直接提供完整的postman的脚本,因为考虑到很多小伙伴平常在群里白嫖资料惯了,没有养成一个良好的学习方式,我如果直接就将脚本给出来,看之前自己没有动手去实操过一遍的话,理解的没有这么深刻。
最近,也看到不少小伙伴自己用postman实现了这些功能,并且有的还在jmeter上进行了实现,看到这里还是挺欣慰的,现在的话呢,将脚本直接分享出来,有需要的直接保存成json文件再导入到postman工具中去即可:
collection脚本内容如下:(包含前后置脚本)
{
"info": {
"_postman_id": "8a604670-781b-4f48-ac71-4c06abd13b17",
"name": "ruoyi",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1067488"
},
"item": [
{
"name": "获取验证码接口",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"操作成功\");\r",
" pm.expect(jsonData.code).to.eql(200);\r",
" \r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{apiurl}}/captchaImage",
"host": [
"{{apiurl}}"
],
"path": [
"captchaImage"
]
}
},
"response": []
},
{
"name": "登录-验证码失效的场景",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"验证码已失效\");\r",
" pm.expect(jsonData.code).to.eql(500);\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"123456\",\r\n \"code\": \"1222\",\r\n \"uuid\": \"096e79dad5ca44109cb9ac7421b5ef53\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-正常登录的场景(用真实验证码登录)",
"event": [
{
"listen": "test",
"script": {
"exec": [
"//清理环境变量\r",
"pm.environment.unset(\"loginuuid\")\r",
"pm.environment.unset(\"logincode\")\r",
"\r",
"// 开始断言\r",
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"操作成功\");\r",
" pm.expect(jsonData.code).to.eql(200);\r",
" pm.expect(jsonData.token).to.not.eql(null)\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"// 调接口获取验证码\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"\r",
"pm.sendRequest(apiurl + \"/captchaImage\", function (err, response) {\r",
" var loginuuid = response.json().uuid\r",
" pm.environment.set(\"loginuuid\", loginuuid)\r",
" console.log(\"uuid:\", loginuuid);\r",
" // 拿到验证码生成的uuid,用于查redis中的验证码\r",
"\r",
" // 调接口查redis中的验证码的值 (验证码存储的key格式:captcha_codes:63fb9e2dc58448df973efb97c62a20c1,后面这一截为uuid)\r",
"\r",
" pm.sendRequest(\"http://8.129.162.225:7379/get/captcha_codes:\" + loginuuid, function (err, response) {\r",
" var logincode = response.json().get.match(/\"(\\S*)\"/)[1] \r",
" console.log(\"验证码:\",logincode);\r",
" pm.environment.set(\"logincode\", logincode)\r",
" });\r",
"});\r",
"\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"123456\",\r\n \"code\": \"{{logincode}}\",\r\n \"uuid\": \"{{loginuuid}}\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-输入错误验证码登录",
"event": [
{
"listen": "test",
"script": {
"exec": [
"//清理环境变量\r",
"pm.environment.unset(\"loginuuid\")\r",
"\r",
"// 开始断言\r",
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"验证码错误\");\r",
" pm.expect(jsonData.code).to.eql(500);\r",
" \r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"// 调接口获取验证码\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"\r",
"pm.sendRequest(apiurl + \"/captchaImage\", function (err, response) {\r",
" var loginuuid = response.json().uuid\r",
" pm.environment.set(\"loginuuid\", loginuuid)\r",
" console.log(\"uuid:\", loginuuid);\r",
"});\r",
"\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"123456\",\r\n \"code\": \"错误验证码\",\r\n \"uuid\": \"{{loginuuid}}\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-输错密码登录(用真实验证码登录)",
"event": [
{
"listen": "test",
"script": {
"exec": [
"//清理环境变量\r",
"pm.environment.unset(\"loginuuid\")\r",
"pm.environment.unset(\"logincode\")\r",
"\r",
"// 开始断言\r",
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"用户不存在/密码错误\");\r",
" pm.expect(jsonData.code).to.eql(500);\r",
" pm.expect(jsonData.token).to.not.eql(null)\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"// 调接口获取验证码\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"\r",
"pm.sendRequest(apiurl + \"/captchaImage\", function (err, response) {\r",
" var loginuuid = response.json().uuid\r",
" pm.environment.set(\"loginuuid\", loginuuid)\r",
" console.log(\"uuid:\", loginuuid);\r",
" // 拿到验证码生成的uuid,用于查redis中的验证码\r",
"\r",
" // 调接口查redis中的验证码的值 (验证码存储的key格式:captcha_codes:63fb9e2dc58448df973efb97c62a20c1,后面这一截为uuid)\r",
"\r",
" pm.sendRequest(\"http://8.129.162.225:7379/get/captcha_codes:\" + loginuuid, function (err, response) {\r",
" var logincode = response.json().get.match(/\"(\\S*)\"/)[1] \r",
" console.log(\"验证码:\",logincode);\r",
" pm.environment.set(\"logincode\", logincode)\r",
" });\r",
"});\r",
"\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"1234561111\",\r\n \"code\": \"{{logincode}}\",\r\n \"uuid\": \"{{loginuuid}}\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-输错用户名登录(用真实验证码登录)",
"event": [
{
"listen": "test",
"script": {
"exec": [
"//清理环境变量\r",
"pm.environment.unset(\"loginuuid\")\r",
"pm.environment.unset(\"logincode\")\r",
"\r",
"// 开始断言\r",
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"登录用户:yanshi111111 不存在\");\r",
" pm.expect(jsonData.code).to.eql(500);\r",
" pm.expect(jsonData.token).to.not.eql(null)\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"// 调接口获取验证码\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"\r",
"pm.sendRequest(apiurl + \"/captchaImage\", function (err, response) {\r",
" var loginuuid = response.json().uuid\r",
" pm.environment.set(\"loginuuid\", loginuuid)\r",
" console.log(\"uuid:\", loginuuid);\r",
" // 拿到验证码生成的uuid,用于查redis中的验证码\r",
"\r",
" // 调接口查redis中的验证码的值 (验证码存储的key格式:captcha_codes:63fb9e2dc58448df973efb97c62a20c1,后面这一截为uuid)\r",
"\r",
" pm.sendRequest(\"http://8.129.162.225:7379/get/captcha_codes:\" + loginuuid, function (err, response) {\r",
" var logincode = response.json().get.match(/\"(\\S*)\"/)[1] \r",
" console.log(\"验证码:\",logincode);\r",
" pm.environment.set(\"logincode\", logincode)\r",
" });\r",
"});\r",
"\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi111111\",\r\n \"password\": \"123456\",\r\n \"code\": \"{{logincode}}\",\r\n \"uuid\": \"{{loginuuid}}\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-用万能验证码登录",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"操作成功\");\r",
" pm.expect(jsonData.code).to.eql(200);\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"123456\",\r\n \"code\": \"1234\",\r\n \"uuid\": \"xxxxxxxxxxx\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
},
{
"name": "登录-正常登录后检查验证码是否失效",
"event": [
{
"listen": "test",
"script": {
"exec": [
"\r",
"// 开始断言\r",
"pm.test(\"断言返回结果\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.msg).to.eql(\"操作成功\");\r",
" pm.expect(jsonData.code).to.eql(200);\r",
" pm.expect(jsonData.token).to.not.eql(null)\r",
"})\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});\r",
"\r",
"\r",
"\r",
"// 开始用相同的验证码再次登录\r",
"//构造一个登录请求\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"var logincode = pm.environment.get(\"logincode\")\r",
"var loginuuid = pm.environment.get(\"loginuuid\")\r",
"\r",
"//清理环境变量\r",
"pm.environment.unset(\"loginuuid\")\r",
"pm.environment.unset(\"logincode\")\r",
"\r",
"const loginRequest = {\r",
" url: apiurl + '/login',\r",
" method: \"POST\",\r",
" header: 'Content-Type: application/json',\r",
" body: {\r",
" mode: 'raw',\r",
" raw: JSON.stringify({\r",
" \"username\": \"yanshi\",\r",
" \"password\": \"123456\",\r",
" \"code\": logincode,\r",
" \"uuid\": loginuuid\r",
" }) \r",
" }\r",
"};\r",
"\r",
"// 发送请求\r",
"pm.sendRequest(loginRequest, function (err, res) {\r",
"\r",
" pm.test(\"Status code is 200\", function () {\r",
"\r",
" var jsonData = res.json();\r",
" pm.expect(jsonData.msg).to.eql(\"验证码已失效\");\r",
" pm.expect(jsonData.code).to.eql(500);\r",
" });\r",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"// 调接口获取验证码\r",
"var apiurl = pm.environment.get(\"apiurl\")\r",
"\r",
"pm.sendRequest(apiurl + \"/captchaImage\", function (err, response) {\r",
" var loginuuid = response.json().uuid\r",
" pm.environment.set(\"loginuuid\", loginuuid)\r",
" console.log(\"uuid:\", loginuuid);\r",
" // 拿到验证码生成的uuid,用于查redis中的验证码\r",
"\r",
" // 调接口查redis中的验证码的值 (验证码存储的key格式:captcha_codes:63fb9e2dc58448df973efb97c62a20c1,后面这一截为uuid)\r",
"\r",
" pm.sendRequest(\"http://8.129.162.225:7379/get/captcha_codes:\" + loginuuid, function (err, response) {\r",
" var logincode = response.json().get.match(/\"(\\S*)\"/)[1] \r",
" console.log(\"验证码:\",logincode);\r",
" pm.environment.set(\"logincode\", logincode)\r",
" });\r",
"});\r",
"\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Accept",
"value": "application/json, text/plain, */*"
},
{
"key": "isToken",
"value": "false"
},
{
"key": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Origin",
"value": "http://8.129.162.225"
},
{
"key": "Referer",
"value": "http://8.129.162.225/"
},
{
"key": "Accept-Language",
"value": "zh,zh-CN;q=0.9,en;q=0.8"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"yanshi\",\r\n \"password\": \"123456\",\r\n \"code\": \"{{logincode}}\",\r\n \"uuid\": \"{{loginuuid}}\"\r\n}"
},
"url": {
"raw": "{{apiurl}}/login",
"host": [
"{{apiurl}}"
],
"path": [
"login"
]
}
},
"response": []
}
]
}
环境变量的配置脚本:
{
"id": "279be738-3e40-4de1-ba2d-f13563a601d5",
"name": "ruoyi",
"values": [
{
"key": "apiurl",
"value": "http://8.129.162.225:8080",
"type": "default",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2022-05-25T15:30:52.472Z",
"_postman_exported_using": "Postman/9.19.3"
}
考虑到有的小伙伴复制脚本可能不知道怎么导入,或者懒得去复制,json文件我也上传到网盘了,有需要的在公众号回复关键字“若依项目实战”即可获取。
文章来源:https://www.toymoban.com/news/detail-419758.html
如需进群和同行业大佬交流学习,请添加微信xiaobotester邀请加入。
文章来源地址https://www.toymoban.com/news/detail-419758.html
到了这里,关于项目实战-RuoYi后台管理系统-登录功能Postman接口自动化脚本分享的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!