概述
以一个页面的增删改查接口为示例
1、src/api/list/homepage.js
const keywordCase = require('../../config/case/keyword_case');
const compare = require('../../utils/compare');
const request = require('../../utils/request')
const before = require('../../config/global/before');
const excute = require('../../utils/regexp');
const assert = require('assert')
const rep = require('../../utils/replace_config')
describe('keywordlist', function () {
let cookie;
let tempConfig = {};
let field = {};
let count = 0;
beforeAll(async () => {
await before().then(res => {
cookie = res;
})
});
beforeEach(async () => {
tempConfig = rep(keywordCase.keyword[count], field);
});
/**
* @description: 循环执行每个用例
*/
for (let i in keywordCase.keyword) {
it(`${keywordCase.keyword[i].name}:${keywordCase.keyword[i].config.url}`, async function () {
tempConfig['config']['headers']['Cookie'] = cookie;
await request(tempConfig.config).then(response => {
let temp = excute(tempConfig, response.config.data, response.data,field);
field = Object.assign(temp, field)
compare(keywordCase.keyword[i]['assert'], response.data);
}).catch(error => {
assert.ifError(error)
})
})
}
afterEach(function () {
count++;
})
})
2、用例case
src/config/case/keyword_case.js
const pathList = require('../../config/global/path')
const data = require('../data/keyword')
/**
* @description isQuote:true,为true时,说明引用了提取的字段,引用字段要和提取字段名称一致,且引用字段名称前面需要加{}符号
* @description isExtract:1 提取reqest,2提取response,3 提取request和response
*/
exports.keyword = [
{
isExtract: 3,
name:'save',
config: {
url: pathList.keywordPathList.save,
method: 'post',
headers: {},
data: data.save
},
assert: [{code: '0000'}, {msg: '执行成功'}],
res_extract: ['data'],
req_extract:['keywordName']
},
{
isQuote: true,
name:'edit',
config: {
url: pathList.keywordPathList.save + '/' + '{data}',
method: 'put',
headers: {},
data: data.put
},
assert: [{code: '0000'}, {msg: '执行成功'}]
},
{
isQuote: true,
name:'get',
config: {
url: pathList.keywordPathList.save,
method: 'get',
headers: {},
params: {
keywordArrayName: '{keywordName}',
page: 1,
pageSize: 11
},
},
assert: [{msg: '执行成功'}, {totalCount: 1}]
},
{
isQuote: true,
name: 'delete',
config: {
url: pathList.keywordPathList.save +'/'+ '{data}',
method: 'delete',
headers: {},
params: {
name: '{keywordName}'
}
},
assert: [{code: '0000'}, {msg: '已删除'}]
},
]
3、数据
src/config/data/keyword.js
module.exports = {
put: {
id: '{data}',
keyword: "",
keywordName: '{keywordName}',
keywordDesc: "自动化测试"
},
save: {keyword: "", keywordName: '自动化测试', keywordDesc: "自动化测试"},
}
url另外集中维护,这样设计的目的,无论是接口修改字段、新增接口、新增断言,都无需动代码,只要改配置即可
4、封装的工具类
src/utils/regexp.js 作用主要是对response或者request进行值提取
const type_judge = require('../utils/type_judge');
const assert = require('assert')
/**
* @description 根据正则表达式提取字符串中的数据
* @param {string} str 要提取的字符串
* @param {object|string} obj 待提取的对象或者字符串
* @return {array} 数组
*/
function extract(str, obj) {
let string_pattern = `(?<="${str}":").+?(?=")`;
let number_pattern = `(?<="${str}":)\d+`;
let arr;
if (type_judge(obj) === 'String') {
arr = obj.match(new RegExp(string_pattern, 'gm')) || null
if (arr === null) {
arr = obj.match(new RegExp(number_pattern, 'gm')) || null
}
}
else if (type_judge(obj) === 'Object') {
arr = JSON.stringify(obj).match(new RegExp(string_pattern, 'gm')) || null;
if (arr === null) {
arr = JSON.stringify(obj).match(new RegExp(number_pattern, 'gm')) || null;
}
} else {
assert.fail('type of parms(obj) is not correct')
}
return arr
}
/**
* @description 根据正则表达式保存提取的数据
* @param {array}array
* @param {object}obj
* @param {object}field
* @return {object}返回所有从对象中提取的数据
*/
function saveExtract(array, obj, field = {}) {
if (array !== [] && array != undefined) {
for (let i in array) {
let arr = extract(array[i], obj);
if (arr !== null) {
for (let j in arr) {
field[array[i]] = arr[j]
}
} else
break
}
}
return field
}
/**
*
* @param {object}obja
* @param {object}objb
* @param {object}objc
* @return {object}根据isExtract的值,提取对应的对象的值
*/
function excute(obja,objb,objc,field) {
let temp = {};
if (obja['isExtract']) {
switch (obja['isExtract']) {
case 1:
temp = saveExtract(obja['req_extract'], objb);
break;
case 2:
temp = saveExtract(obja['res_extract'], objc);
break;
case 3:
temp = Object.assign(saveExtract(obja['req_extract'], objb), saveExtract(obja['res_extract'], objc));
break;
default:
break
}
return Object.assign(field, temp)
}
else
return field
}
module.exports = excute
src/utils/replace_config.js
作用是把提取的对应字段的值替换给case中接口引用的地方
/**
* @description 正则匹配并替换引用
* @param {object}obj
* @param {object}field
* @return {object}返回替换后的对象||原对象
*/
function rep(obj, field) {
if (obj['isQuote']) {
let temp = JSON.stringify(obj)
for (let i in Object.keys(field)) {
let reg = new RegExp(`{(${Object.keys(field)[i]})}`, 'gm')
temp = temp.replace(reg, field[Object.keys(field)[i]])
}
let result = JSON.parse(temp)
// result['config']['url'] = encodeURI(result['config']['url'])
return result
} else {
return obj
}
}
module.exports = rep
最后
以上就是优秀大山为你收集整理的4.4 nodejs自动化测试代码设计思路的全部内容,希望文章能够帮你解决4.4 nodejs自动化测试代码设计思路所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复