暑期實習7-1:變數|Robot Framework筆記

這是一篇我在2022年於台北趨勢科技暑期實習的筆記,如果對趨勢科技暑期實習面試有興趣,可以看暑期實習1:趨勢科技面試過程|2022;如果你想要寫自動化測試,Robot Framework是一個開源、易讀、易學的框架,並且它可以連結Python。

Robot Framework字串、串列、字典要怎麼定義?字串需要引號嗎?多行怎麼辦?二維結構又要怎麼定義?取值怎麼取?變數名稱可以有空白嗎?空白很重要,但什麼時候要不要空白?1個空白?2個空白?變數符號也很重要,什麼時候使用$?@?&?

一、字串 (String)

(一)建立字串

  • 定義字串要使用$符號
  • 變數名稱和值都可以有空白
  • 字串變數不需要單/雙引號,否則值就會有單/雙引號
  • 存路徑時,要使用\\,而非\,因為\對於Robot Framework是特殊符號

1. 全域變數

  • 不用關鍵字和=,但要有空白(至少2個)
${str}    String
在區域中更改全域變數的值
  • Set Global Variable:在這所有檔案中共享
  • Set Suite Variable:在這整份檔案中共享
  • 可以在區域中,直接使用Set Suite Variable宣告或更改變數值
Set Suite Variable    ${str}    String # Correct
Set Suite Variable    ${EMPTY} # Correct,設空值
${str} =  Set Suite Variable    String # Error

${str} =  Set Suite Variable    Kewords # Error,不能直接將Keyword回傳值存在全域變數中
${result} =  Keword # 先將Keyword回傳值存在變數result中
${str} =  Set Suite Variable    ${result} # 然後再將變數result值存在全域變數中

2. 區域變數

  • 要使用Set Variable這個關鍵字
  • =左邊最多1個空白,=右邊最少2個空白
  • 關鍵字和值之間至少要有2個空白
${str1} = Set Variable    String # Correct
${str2} =    String # Error, 少Set Variable
${i am str3} =    I am a string. # Correct
${str4} = Set Variable String # Error, =右邊只有1個空白
${str5} =    Set Variable String # Error, 關鍵字和值之間只有1個空白
${str6} =    Set Variable    "String" # Correct,但值會有雙引號
${str7} =    Set Variable    C:\\TsungSquare\\is\\a_good.website # Correct
${str8} =    Set Variable    C:\TsungSquare\is\a_good.website #Error, 需要使用\\

(二)使用Regexp刪除字串

  • Regexp的\至少要3個(\\\),如果要表示句號(.),則需要使用\.。

${folder} =    Remove String Using Regexp    ${path}    \\\\${file_name}\..*$

(三)切割字串

  • 可以使用Split String和Split String From Right,前者是從左邊切割,後者是從右邊切割
  • 參數有 字串, 分界字元(separator)=None, 最大切割(max_split)=-1,回傳List
  • 最大切割為-1為全切割,0為不切割,1為切割1次,2為切割2次,以此類推!

${str} =    Set Variable    TsungSquare is a good website.

# All split, ${split} = ["TsungSquare", "is", "a", "good", "website."]
${split} =    Split String    ${str}    ${SPACE}    -1

# No split, ${split} = ["TsungSquare is a good website."]
${split} =    Split String    ${str}    ${SPACE}    0

# Split 1, ${split} = ["TsungSquare", "is a good website."]
${split} =    Split String    ${str}    ${SPACE}    1

# Split 1 from right, ${split} = ["TsungSquare", "is a good website."]
${split} =    Split String From Right    ${str}    ${SPACE}    1

二、串列 (List)

(一)建立串列

  • 定義list要使用@符號
  • 變數名稱和值都可以有空白
  • 值之間至少要2個空白
  • 如果要斷行,要使用3個句點(...),句點與值之間至少要有2個空白

1. 全域變數

  • 不用關鍵字與=,但要有空白(至少2個)
@{LIST}    value1
...        value2
...        value3

2. 區域變數

  • 要使用Create List這個關鍵字,
  • =左邊最多1個空白,=右邊最少2個空白
@{list1} =    Create List    value1    value2
@{list2} =    Create List
...        value1
...     value2
@{list3} =    Create List
...           TsungSquare
...           is
...           a good website
# An list having 0 element
@{list4} =    Create List

# An list having 1 empty element
@{list5} =    Create List    ${EMPTY}

(二)合併串列

  • 要使用Combine Lists這個關鍵字
  • 因為要定義左值串列 (combined_list),所以串列使用@;右值的部分是要使用已經定義好的串列,故串列使用$
  • =、關鍵字、串列之間至少2個空白
@{combined_list} =    Combine Lists    ${list1}  ${list2}  ${list3}

(三)取值

  • index從0開始,支援負數
  • 使用串列時,要使用$
  • 取值時,要使用[]

Log ${list}[index] # Correct, 印出list[index]
Log ${list[index]} # Correct @{str} = Create List TsungSquare is a good website. Log ${str}[0] # TsungSquare Log ${str}[1] # is Log ${str}[-1] # website.

(四)二維List

  • 先定義好一維list,再定義二維list Ref
  • 使用二維list時,使用[][]
*** Variables ***
@{colors} red green blue
@{animals} cow pig dog
@{things} ${animals} ${colors} *** Test Cases *** 2-Dimensional List
Log List ${things} is ${things[1][1]}
Log The ${things[0][1]}


三、字典 (Dictionary)

(一)建立字典

  • 定義字典要使用&符號
  • 變數名稱、Key和value都可以含有空白
  • Value不能放函數但可以放變數,故必須把函數的值存進變數,再透過變數間接存到dictionary中!
  • key-value的=前後都不能有空白,不然會被視為key或value的一部分!

1. 全域變數

  • 不用關鍵字與=,但要有空白(至少2個)
&{DIC} key1=value1
...       key2=value2
... key3=value3

2. 區域變數

  • 要使用Create Dictionary關鍵字
# Correct
${value3} = Return value3
&{dic1} = Create Dictionary
... key1=value1
... key3=${value3}
... key2=value2
# Correct, 但${dic2}[ke1]=Return value3,而非value3 &{dic2} = Create Dictiyonary
... key1=Return value3
... key2=value2

(二)修改或插入項目於字典

  • 要使用Set To Dictionary關鍵字
Set To Dictionary ${dic}
... key1=value1
... key2=value2

(三)取值

  • 中括號[]要置於大括號{}外面
Log    ${dic}[key1]

(四)二維Dictionary

  • 如果${dic2}更改,則${DIC}[item2]也會隨著更新!因為dictionary是傳參考給Create Dictionary!!
@{dic1} = Create Dictionary key1=value1 key2=value2
@{dic2} = Create Dictionary key3=value3 key4=value4
@{DIC} = Create Dictionary item1=${dic1} item2=${dic2}

(五)使用Run Keyword If檢查key1是否在${dic}?

  • 使用單引號包住key,然後使用$,in前後最多1個空格然後in要小寫! Ref

Run keyword if    'key1' in ${dic}    Keyword


四、其他

# Get the type of variable 
${type} =    Evaluate    type(${var})

(一)布林值 (Boolean)

# Set true bool variable 
${bool_1} =    Set Variable    ${True}
# Set false bool variable
${bool_2} =    Set Variable    ${False}

# Correct: Print True
Run Keyword If    "${bool_1}"=="True"    Log    True
# Correct: Print True
Run Keyword If    "${bool_1}"=="${True}"    Log    True

# Set true string variable
${str_1} =    Set Variable    True
# Set false string variable
${str_2} =    Set Variable    False

# Correct: Print True
Run Keyword If    "${str_1}"=="True"    Log    True
# Correct: Print True
Run Keyword If    "${str_1}"=="${True}"    Log    True

相關文章

  1. 暑期實習0:背景、面試、結果|趨勢、默克、時刻
  2. 暑期實習1:趨勢科技面試過程|2022
  3. 暑期實習2:默克集團一面過程|2022
  4. 暑期實習3:默克集團二面過程|2022
  5. 暑期實習4:時刻科技面試過程|2022
  6. 暑期實習5:租屋、銀行帳戶、托運機車、健身房|前置作業
  7. 暑期實習6-1:寧夏夜市、給力健身、傳品牛排、趨勢報到|實習日記
  8. 暑期實習6-2:AI機器人、遼寧夜市、饒河夜市|實習日記
  9. 暑期實習6-3:金魚腦、暖心借錢、不要加班、code review|實習日記
  10. 暑期實習6-4:DISC有禮貌、人資會議、薪資微調|實習日記
  11. 暑期實習6-5:內推、旭集、瓦城、畢業典禮|實習日記
  12. 暑期實習7-1:變數|Robot Framework筆記
  13. 暑期實習7-2:控制、迴圈、關鍵字|Robot Framework筆記
  14. 暑期實習7-3:Git筆記
  15. 暑期實習8-0:美食地圖|總目錄
  16. 暑期實習8-1:美食地圖|大安區
  17. 暑期實習8-2:美食地圖|大安區
  18. 暑期實習8-3:美食地圖|中山區
  19. 暑期實習8-4:美食地圖|中山區
  20. 暑期實習8-5:美食地圖|中正區
  21. 暑期實習8-6:美食地圖|松山區
  22. 暑期實習∞|工作、薪資福利、心得|2022趨勢科技


參考資料

  1. Gidil (2014) Creating and using a 2 dimensional array. stack overflow [Accessed by Aug. 2022]
  2. omheffels (2020) Run Keyword If key exists in dictionary (Robot Framework). stack overflow [Accessed by Aug. 2022]

留言