robotframework獲取中文(wen)(wen)文(wen)(wen)件(jian)名(ming)并且去掉(diao)后綴
問題背景
項(xiang)目上有個模型(xing)(xing)文(wen)(wen)件(jian)一(yi)直(zhi)在變動,但是(shi)自動化腳(jiao)本里面導入(ru)該模型(xing)(xing)是(shi)把(ba)模型(xing)(xing)名稱(cheng)(cheng)寫(xie)死了的,因此更新了模型(xing)(xing)文(wen)(wen)件(jian)之后,自動化測試(shi)代(dai)碼里面為了導入(ru)該文(wen)(wen)件(jian)也(ye)一(yi)直(zhi)要修改,所以想通過模糊匹配(pei)文(wen)(wen)件(jian)名稱(cheng)(cheng)來獲(huo)取該文(wen)(wen)件(jian)名稱(cheng)(cheng)并導入(ru)。
解決方案
使用List Files In Directory關(guan)鍵(jian)字(zi)列出目錄下面(mian)符(fu)合模糊(hu)匹(pi)配(pei)的文件名稱(cheng)(cheng),該關(guan)鍵(jian)字(zi)返回一個列表,獲取列表中的第一個文件名稱(cheng)(cheng)(項目中該文件名稱(cheng)(cheng)前綴固定,后面(mian)攜帶的日期在變化)
${list}= List Files In Directory ${filepath}/mdaf/common/中文/應急中心/EMC_Package/ pattern=DC資源池級應急*
Log ${list}
${dc_model}= Set Variable ${list[0]}
Log ${dc_model}
${file_name} Remove String ${dc_model} .zip
Set Suite Variable ${dc_file_name} ${file_name}
踩的坑
最開始直接AI提供的方(fang)案是使用
{dc_model}= Evaluate str('{dc_model}')
{filename_without_ext}= Evaluate '{dc_model}'.replace('.zip', '')
結果卻報錯了UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 0: unexpected end of data 類似的這種報錯都是字符編碼格式的。
出(chu)現這個(ge)問題(ti)的(de)原因是使用(yong)了python的(de)模(mo)塊去(qu)處理字(zi)(zi)符(fu)(fu)串(chuan),默認(ren)轉成了unicode字(zi)(zi)符(fu)(fu)串(chuan)。
建議
建(jian)議優先(xian)使用robotframework自(zi)帶的(de)處理字(zi)符串的(de)關鍵字(zi)
與字符串處理相關的關鍵字
- 基本字符串操作
Remove String
${result}= Remove String Hello World World
# 結果: "Hello "
${result}= Remove String filename.zip .zip
# 結果: "filename"
Replace String
${result}= Replace String Hello World World Robot
# 結果: "Hello Robot"
Get Substring
${result}= Get Substring Hello World 0 5
# 結果: "Hello"
Split String
@{parts}= Split String one,two,three ,
# 結果: @{parts} = ['one', 'two', 'three']
Catenate
${result}= Catenate Hello World
# 結果: "Hello World"
${result}= Catenate SEPARATOR=- Hello World
# 結果: "Hello-World"
- 字符串檢查
Should Contain
Should Contain Hello World World
# 驗證字符串包含指定內容
Should Not Contain
Should Not Contain Hello World Robot
# 驗證字符串不包含指定內容
Should Start With
Should Start With Hello World Hello
# 驗證字符串以指定內容開頭
Should End With
Should End With Hello World World
# 驗證字符串以指定內容結尾
- 字符串轉換
Convert To Lowercase
${result}= Convert To Lowercase Hello World
# 結果: "hello world"
Convert To Uppercase
${result}= Convert To Uppercase Hello World
# 結果: "HELLO WORLD"
Convert To String
${result}= Convert To String 123
# 結果: "123"
- 字符串清理
Strip String
${result}= Strip String ${SPACE}Hello World${SPACE}
# 結果: "Hello World" (去除前后空格)
Fetch From Left
${result}= Fetch From Left Hello World o
# 結果: "Hell"
Fetch From Right
${result}= Fetch From Right Hello World o
# 結果: "rld"
- 字符串比較
Should Be Equal
Should Be Equal Hello Hello
# 驗證兩個字符串相等
Should Be Equal As Strings
Should Be Equal As Strings 123 123
# 將參數轉為字符串后比較
Should Match
Should Match Hello World Hello*
# 使用通配符模式匹配
Should Match Regexp
Should Match Regexp example@email.com ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# 使用正則表達式匹配
- 高級字符串處理
Get Length
${length}= Get Length Hello World
# 結果: 11
Count Values In String
${count}= Count Values In String Hello World l
# 結果: 3 (統計'l'出現的次數)
Encode String To Bytes & Decode Bytes To String
${bytes}= Encode String To Bytes Hello World UTF-8
${string}= Decode Bytes To String ${bytes} UTF-8
# 處理編碼轉換
