1.用途
在接口自动化操作的过程中,使用Excel的形式管理测试用例,方便,简单,快捷,学习成本低,后期拓展到数据库容易。
问题:1.很多时候,测试人员不只是要在Excel上写上用例,还需要配置Json入参模板
2.嵌套类型的Json入参在准备测试用例的时候难以处理
3.往往Excel一行用例就是一个case,涉及嵌套时不容易控制下一个嵌套的行数
总之就是想减轻测试人员的负担,减少测试人员对代码的接触。今天要介绍的这套Excel读取方式,脚本可能不是很好,也没有用python或者其他语言封装,今天主要是想阐述一种设计思路
2.前期准备
robot framework开发环境一套
ExcelLibrary库适量
Collections库适量
其他配料若干
3.直接上脚本
Keyword:
*** Settings ***
Documentation 用途:将excel中的文件内容按照一定规则转换为Json格式
Library ExcelLibrary
Library json
Library String
Library Collections
*** Keywords ***
RowToJsonPartFun
[Arguments] ${sheet} ${row} ${jsonSon}
${json} Set Variable
: FOR ${col} IN RANGE 0 100
\ ${json} Set Variable ${json}${jsonSon}
\ ${colName} Read Cell Data By Coordinates ${sheet} ${col} 0
\ Exit For Loop If "${colName}"=="EOF"
\ ${content} Read Cell Data By Coordinates ${sheet} ${col} ${row}
\ ${contentFirstChar} Get Substring ${content} 0 1
\ ${jsonSon} Run Keyword If "${contentFirstChar}"=="{" CoverObjType ${content} ${row}
\ ... ${colName}
\ ... ELSE IF "${contentFirstChar}"=="[" CoverListType ${content} ${row}
\ ... ${colName}
\ ... ELSE IF "${contentFirstChar}"=="$" CoverMyRowType ${content} ${colName}
\ ... ELSE Set Variable "${colName}":"${content}",
${json} Get Substring ${json} 0 -1
[Return] ${json}
RowToFullJson
[Arguments] ${partPath}
${fullPath} GetFullExcelPath ${partPath}
Open Excel ${fullPath}
${json} RowToJsonPartFun Sheet1 1 {
${json} Set Variable ${json}}
[Return] ${json}
CoverObjType
[Arguments] ${content} ${row} ${colName} ${endSymStatus}=false
${json} Set Variable
${sheetForNext} Get Substring ${content} 1 -1
${json} RowToJsonPartFun ${sheetForNext} ${row} ${json}
${json} Set Variable {${json}},
${json} Run Keyword If "${endSymStatus}"=="true" Get Substring ${json} 0 -1
... ELSE Set Variable ${json}
${json} Run Keyword If "${endSymStatus}"=="notName" Set Variable ${json}
... ELSE Set Variable "${colName}":${json}
[Return] ${json}
GetFullExcelPath
[Arguments] ${partPath}
${fullPath} Set Variable ../TestCase/${partPath}
[Return] ${fullPath}
CoverListType
[Arguments] ${content} ${row} ${colName}
${json} Set Variable
${contentSecChar} Get Substring ${content} 1 2
${contentForNext} Get Substring ${content} 1 -1
${json} Run Keyword If "${contentSecChar}"=="{" CoverObjType ${contentForNext} ${row} ${colName}
... true
... ELSE IF "${contentSecChar}"=="$" DealListObjMyRow ${contentForNext} ${colName}
... ELSE Get Substring ${content} 1 -1
${json} Set Variable [${json}],
${json} Set Variable "${colName}":${json}
[Return] ${json}
CoverMyRowType
[Arguments] ${content} ${colName}
${row} ${content} GetRowByContent ${content}
${json} CoverObjType ${content} ${row} ${colName}
[Return] ${json}
GetRowByContent
[Arguments] ${content}
${row} Set Variable ${0}
: FOR ${i} IN RANGE 0 100
\ ${content} Get Sub String ${content} 1 #去掉最前面的$符
\ ${contentChar} Get Sub String ${content} 0 1
\ Exit For Loop If "${contentChar}"=="{"
\ Exit For Loop If "${contentChar}"=="$"
\ ${charVal} Run Keyword If "${contentChar}"=="0" Set Variable ${0}
\ ... ELSE IF "${contentChar}"=="1" Set Variable ${1}
\ ... ELSE IF "${contentChar}"=="2" Set Variable ${2}
\ ... ELSE IF "${contentChar}"=="3" Set Variable ${3}
\ ... ELSE IF "${contentChar}"=="4" Set Variable ${4}
\ ... ELSE IF "${contentChar}"=="5" Set Variable ${5}
\ ... ELSE IF "${contentChar}"=="6" Set Variable ${6}
\ ... ELSE IF "${contentChar}"=="7" Set Variable ${7}
\ ... ELSE IF "${contentChar}"=="8" Set Variable ${8}
\ ... ELSE Set Variable 9
\ ${row} Set Variable ${row*10+${charVal}}
${row} Set Variable ${row-1}
[Return] ${row} ${content}
DealListObjMyRow
[Arguments] ${content} ${colName}
${json} Set Variable
@{rows} Create List
: FOR ${i} IN RANGE 0 100
\ ${endChar} Get Sub String ${content} 0 1
\ Exit For Loop If "${endChar}"=="{"
\ ${rowItem} ${content} GetRowByContent ${content}
\ Append To List ${rows} ${rowItem}
${json} GetListObjJson ${content} ${colName} @{rows}
[Return] ${json}
GetListObjJson
[Arguments] ${content} ${colName} @{rows}
${json} Set Variable
: FOR ${row} IN @{rows}
\ ${jsonSon} CoverObjType ${content} ${row} ${colName} notName
\ ${json} Set Variable ${json}${jsonSon}
${json} Get Substring ${json} 0 -1
[Return] ${json}
Case:
ExcelDemo
[Tags]
${json} RowToFullJson aa.xls
Log ${json}
Comment ${test} To Json ${json}
Comment Log ${test}
拼接的Json:
{
"merchantId":"1129",
"storeId":"哈哈哈",
"object":
{
"orderId":"sa",
"orderNo":"55685",
"object":
{
"aid":"1129"
}
},
"list":[1,2,3,4],
"listobj":
[
{
"acc":"sas",
"bccc":"fd"
},
{
"acc":"aaaa",
"bccc":"aaaaa"
}
]
}
Excel表格内容:
4.Excel规则符号简介
第一行所有列名即对应Json属性名称,EOF为读取终止信息
$:该符号后面必须跟整数,代表接下来引用的嵌套对应Excel的Sheet中的对应的行数,如若是list< Object >类型,则可以通过多个符号指定list中的多个对象所在行数(如'$1$2$5{Sheet2}
代表该list中的对象是Sheet2中的第1,2,5条)。
{}:大括号代表这里是一个嵌套的对象,大括号里的内容是对象所在Excel的Sheet
[]:中括号代表这里是一个数组,数组有两种情况,一种是字符串,值等内容,此类内容直接填在[]中,用,隔开即可。另一种是list< Object >,此类需在{}中指定对象对应的Excel的Sheet
5.注意事项
此脚本注意事项:
# Excel中的数字必须是文本格式,不然类型不匹配,读取的时候会变成浮点型
#一旦指定了嵌套所在行数,该嵌套的下一个嵌套会默认上一个嵌套的行数
#如若不指定行数,默认与Json第一层行数或上一层嵌套行数相同
6.*看了之后觉得不好的地方记得叫我,有更好的工具什么的记得分享~*