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.*看了之後覺得不好的地方記得叫我,有更好的工具什麼的記得分享~*