Robot Framework Regular Expressi
在处理server log的时候,想用一用正则表达式抓抓参数,没想到玩不太转,遇到点坑。找了半天,发现RF使用Regular Expression的中文文档基本没有,除了道长在《Robot Framework 自动化测试修炼宝典》中,稍稍用一下节带过,google 上百度一下,也都是泪。这里就当个插曲,讲讲遇到的小坑,然后稍稍分享下regular expression in RF。
这里盗个版权,道长书里是这样写的:
image.png源码:
*** Settings ***
Library RequestsLibrary
Library requests
Library BuiltIn
Library Collections
Resource ../../Library/manageCase.txt
Library String
${rm} Set Variable paic-2.523.6-90
${subn} Evaluate re.subn('[^\\w]','_','${rm}') re
Log ${subn[0]}
${sub} Evaluate re.sub('[\\d]','*','${rm}') re
${findnumber} Evaluate re.findall('\\d','${rm}') re
${findnumbers} Evaluate re.findall('\\d+','${rm}') re
自己在使用的时候,发现报错了,欲哭无泪。
image.pngError Log:
20170816 14:41:47.366 : INFO : testDemo
20170816 14:41:47.368 : INFO : ${log} = [13:45:45.318 TRACE][http_1][XNIO-1 task-45] tags.model.pojo.Charge(save:80) - Charge{userId= http_1, phone='18612345678'&id='test123 ', cid='cid_phone_test ', timestamp='1502862345313', statusCode=' ...
20170816 14:41:47.370 : FAIL : Evaluating expression 're.findall('phone=\d*','[13:45:45.318 TRACE][http_1][XNIO-1 task-45] tags.model.pojo.Charge(save:80) - Charge{userId= http_1, phone='18612345678'&id='test123 ', cid='cid_phone_test ', timestamp='1502862345313', statusCode=' 2000' }')' failed: SyntaxError: invalid syntax (<string>, line 1)
failed: SyntaxError: invalid syntax (<string>, line 1)
之前都是搞Java的,刚开始学python,这个坑就酱跳进去了,参数里面不能有单引号',要不就跟findall('','')里面的这个四个货有冲突,要么转义\,要么删掉'。case 里面的${log}是抓的,只能删了
${log} Replace String ${log} ' ${EMPTY}
然后就好了,醉了
image.png目前用到的是正则表达式去抓值,看到还有人用这个去做验证,目前还没有用到,先留着以后用
http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-framework-python-menu/216-robot-framework-regular-expression-test-and-keywords
Test case:
PatternTest
Should Match Regexp test@test.com ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)(\.[A-Za-z]{2,})$ 'email validation'
Should Match Regexp 192.168.1.1 ^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$ 'IP Validation'
Should Match Regexp <("["]*"|'[']'|[^'">])> 'Html Tag Validation'
Should Match Regexp ])*> 'Html Tag Validation'
image.png image.png最后奉上Builtln的文档,里面Evaluate就没提regular,还是道长有灵性。
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html