基於Fitnesse的介面自動化測試-關鍵字設計-樣例-通過正則表示式獲取值
阿新 • • 發佈:2020-08-10
需求
當返回響應結構比較複雜,可以通過正則表示式來獲取
實現
1.編寫建構函式和成員變數
private String content;
public StringFixture() {
}
public StringFixture(String content) {
this.content = content;
}
2.實現方法(關鍵字)
public String getValueByRegGroup(String regular,int groupId){ String value = null; value = RegularUtil.getStringByRegGroup(regular, this.content,groupId); return value; } public static String getStringByRegGroup(String regular, String dircetContent,int groupId) { String result = null; Pattern pattern = Pattern.compile(regular); Matcher matcher = pattern.matcher(dircetContent); if (matcher.find()) { result = matcher.group(groupId); } return result; }
使用
1.引入類對應package
|import |
|own.slim.string|
2.編寫指令碼
|script |string fixture |!-[{"sn":1,"date":"20200708","pay":"78.98"},{"date":"20200709","pay":"90.77","sn":2}]-!| |$pay_1= |getValueByRegGroup;|"sn":1.*?"pay":"(.{5})" |1 | |$date_1=|getValueByRegGroup;|"sn":1.*?"date":"(.{8})" |1 | |$pay_2= |getValueByRegGroup;|.*"pay":"(.{5})".*?"sn":2 |1 | |$date_2=|getValueByRegGroup;|.*"date":"(.{8})".*?"sn":2 |1 |
3.測試
總結
使用正則表示式獲取值,難度是編寫一個合適的表示式,如果可以熟練編寫正則表示式,那麼這種取值方式將是最靈活的方式。