在使用robot framework 撰寫Test Case 時有時候會遇到需要把從其他function接到的類陣列字串(可以直接轉為List Object)先轉為Robot Framework的陣列(List)型別物件再用Index取出裡面某一個元素值的狀況。

  查了 Robot Framework 本身似乎沒有直接提供內建方法把RobotString轉成List,所以這時候就可以考慮使用python code的方式用python強制型別轉換將string convert to list ,不然就得在robot上搞什麼把string分割再取值的惱人方式來把想要的元素值從字串中取出,既複雜又吃力不討好。

  而用python code的方式作強制型別轉換在robot中也有兩個方式:一種是把function寫成library變成keywork再透過importtest case suite檔案的方式讓Test Case直接呼叫keywork來完成。

  但殺雞焉用牛刀,這麼簡單的轉換在robot中其實是根本不用寫成library keyword的,只要用Robot的直接引用 python functionEvaluate就可以達成:

@{arrayMessage}    evaluate    list(${strMessage})

上面這一行是讓robot透過直接call python list() 強制型別轉換function將字串${strMessage} 轉換成list型別給List 變數 @{arrayMessage}

然後就可以用Robot Framework Get From List Function取出 ${arrayMessage} 的第一個元素給 變數 strMessage,然後將變數 ${strMessage} Log出來就可以得到元素的值(是個GUID字串)

還有另一個取值方式是不用透過Get From List Function的,而是可以直接在 ${arrsyMessage} 變數裡指定索引值,就可以將該值 ${arrsyMessage[0]} Log出來。

 

 

Robot Usage Test - a list-like string on robotframework is to covert to list type for getting element with integer index

    ${strMessage}    set Variable    ["a70c3199-2452-4331-895f-767c5b89694d","A70C3199-2452-4331-895F-767C5B89694D"]

    Log    strMessage=${strMessage}

    @{arrsyMessage}    evaluate    list(${strMessage})

    Log    target1=${arrsyMessage[0]}

    ${retResponse}    Get From List    ${arrsyMessage}    1

    Log    target2=${retResponse}

 

[執行結果]

python -m robot -L INFO -d E:\Lab\robot\log -o output-horus.xml -l log-horus.html -r report-horus.html -t "Robot Usage Test - a list-like string on robotframework is to covert to list type for getting element with integer index"  d:\Robot\TestCase\

image

 

2020818日星期二

 

arrow
arrow
    創作者介紹
    創作者 jackterrylau 的頭像
    jackterrylau

    儒道哲學的浪漫人生

    jackterrylau 發表在 痞客邦 留言(0) 人氣()