# Keyword ## 簡介 Keyword是Robotframework的特色,類似於其他語言的Function,可以帶參數進到Keyword裡面,Test case都是由大量Keyword組合而成,Keyword的撰寫建議以明確能讓人一眼看懂操作的方式命名,才可讓後續維護及Debug時可以快速知道操作步驟 ## 撰寫方式 一開始放置你要使用的keyword,後面代入官方文件提供可用的參數 * 以Input Text為範例(參數如果帶有"=",表示預設值) ``` # 官方文件描述 Input Text locator text clear=True # 實際使用 Input Text //input[@id="input_demo"] hello ``` >locator:定位器,我們使用XPath來做定位 有些Keyword可以return值 * 以Get Text為範例 ``` # 官方文件描述 Get Text locator # 官方備註: Returns the text value of the element identified by locator. # 實際使用 ${result} = Get Text //p[@id="demo"] ``` ## SeleniumLibrary #### [Set Selenium Speed](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Set%20Selenium%20Speed) > 當你覺得Selenium跑太快的時候可以使用,建議0.1s ``` Set Selenium Speed 0.1s ``` #### [Wait Until Element Is Enabled](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Wait%20Until%20Element%20Is%20Enabled) > 通常在對Element進行操作之前會使用此keyword,事先確保element是可用的 #### [Wait Until Element Is Visible](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Wait%20Until%20Element%20Is%20Visible) > 等待該element於頁面中顯示出來,可以用來驗證動態效果 #### [Wait Until Page Contains Element](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Wait%20Until%20Page%20Contains%20Element) > 可以用來驗證頁面是否存在該元素 #### [Click Element](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Click%20Element) > 非常常用的Keyword,例如點擊button或radio都可使用 > #### [Select From List By Label](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Select%20From%20List%20By%20Label) > 當你的element是一個List時可以使用,可以根據文字選取特定選項 ``` Select From List By Label //select[@id="agent_id"] ${text} ``` #### [Get Text](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Get%20Text) > 獲取Element中的文字 #### [Input Text](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Input%20Text) > 輸入文字到Element裡面,通常是input element或textarea element > ## BuiltIn > 官方文件說明 > BuiltIn is Robot Framework's standard library that provides a set of generic keywords needed often. It is imported automatically and thus always available. The provided keywords can be used, for example, for verifications (e.g. Should Be Equal, Should Contain), conversions (e.g. Convert To Integer) and for various other purposes (e.g. Log, Sleep, Run Keyword If, Set Global Variable). #### [Run Keyword If](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20If) > 當條件成立時,執行指定的Keyword #### [Set Suite Variable](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable) > 當你想要讓SuiteSetup的變數變成全域變數時,可以使用,使用後此變數生命週期將進行到SuiteTeardown才結束 #### [Set Test Variable](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Test%20Variable) > 讓變數的生命週期進行到Test case結束為止 #### [Wait Until Keyword Succeeds](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Wait%20Until%20Keyword%20Succeeds) > Run Keyword直到成功為止,設定總共幾秒,幾秒重跑一次 ``` Wait Until Keyword Succeeds 5s 1s Verify Redirect In Specified URL users ``` #### [Should Be Equal As Strings](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Should%20Be%20Equal%20As%20Strings) > 當想比對字串是否"完全"符合預期時使用,使用情境都嘗試使用Get Text從Element取得文字,再做比對 ``` ${text} = Get Text //ul[@class="parsley-errors-list filled"] Should Be Equal As Strings ${text} ${expected} ``` #### [Should Contain](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Should%20Contain) > 當想比對字串是否包含特定字串時使用 ``` Should Contain ${text} ${expected} ``` ## String 用於字串處理 #### [Generate Random String](https://robotframework.org/robotframework/latest/libraries/String.html#Generate%20Random%20String) > 亂數產生值,範例是產生字母+數字交錯且長度50的亂數值 ``` ${randomString} = Generate Random String 50 [LETTERS][NUMBERS] ``` ## Datetime #### [Get Current Date](https://robotframework.org/robotframework/latest/libraries/DateTime.html#Get%20Current%20Date) > 獲取當前的時間 ``` ${currentDate} = Get Current Date result_format=%Y-%m-%d ``` ## Other #### For Loop ``` FOR ${index} IN RANGE 1 ${count} ... END ``` #### Template > 當步驟會重複使用時使用 > 為甚麼不是使用For而是使用Template? > 因為Template如果中途失敗, > 他還是會執行下一次的Template, > 反之,如果使用For的話當其中一個步驟Fail了, > 那測試就會直接全Fail ``` *** Test Cases *** Add Invalid Platform Name Should Fail [Template] Add Invalid Platform Name And Then Verify Error Message As Expected !@#$% Plat Form ${SPACE*4} ${EMPTY} 新增平台名稱 不能留空。 [Teardown] Click Target Menu platform --- *** Keywords *** Add Invalid Platform Name And Then Verify Error Message As Expected [Arguments] ${invalid} ${errorMessage}=新增平台格式錯誤 Input Name ${invalid} Click Add Button Wait Until Keyword Succeeds 5s 1s Verify Error Message As Expected ${errorMessage} ``` ###### tags: `Note` `keyword`