# Robotframework ## 簡介 Robot Framework是一款python編寫的功能自動化測試軟體框架,一般常見的自動化測試分為Web UI和API測試。 * Web UI的測試:藉由抓取html的element方法,來驗證目標element是否符合預期,所以只要網頁上html的部分基本上都可進行驗證,所以就是想辦法把人為的測試行為轉為自動化。 * API的測試:當開發使用前後端分離架構時,後端會提供API,讓前端與測試人員可以使用,所以要驗證API的schema、response、response code是否符合預期。 ## 安裝 * Install requirement library ``` $ pip install robotframework $ pip install robotframework-seleniumlibrary 顯示所有已安裝套件 $ pip freeze ``` * Selenium[^selenium]需要使用瀏覽器的Webdriver[^webdriver],所以需要放到python目錄裡 * [Chrome](https://chromedriver.chromium.org/downloads) * QQ、UC、360瀏覽器[^qq] * Safari - 需在Mac上進行自動化測試,[目前不支援Windows](https://www.edureka.co/community/46309/possible-selenium-automation-scripts-browser-windows-machine) [^selenium]: Selenium為瀏覽器自動化需求所設計的一套工具集合,讓程式可以直接驅動瀏覽器進行各種網站操作 [^webdriver]: WebDriver是用來控制Web瀏覽器的行為,每一個瀏覽器都會有各自相對應的驅動程式(driver) [^qq]: QQ、UC與360瀏覽器是由chrome的open source瀏覽器衍生而成,所以使用chrome webdriver就可以了 * Run robot ``` 跑特定資料夾裡的測試檔案 robot automation/eReport/TestCase/User.robot 跑特定測試檔案裡的測試案例 robot -t "Update User Password Should Success And Then Login Successful" automation/eReport/TestCase/User.robot 建議指定report的output資料夾(*最佳) robot --outputdir D:\Users\harvey_chan\YouCe\report automation/eReport/TestCase/User.robot ``` ## 專案規劃及Robot生命週期 > Suite Setup會在測試的一開始執行,Suite Teardown則在所有測試的結束執行, > 舉例: Suite Setup可以存放開啟瀏覽器這項行為,Suite Teardown則是關閉所有瀏覽器。 > Test Setup單個測試一開始時執行,為預先準備; > Test Teardown為測試結束後一定會做的事情,例如:我想驗證搜尋一筆User的功能, > 所以會在Test Setup預先Create一個User,接下來才做主要的驗證, > 最後在Teardown去把User刪除 > ![](https://i.imgur.com/oUO36q4.png) ## Tutorial ### Robot folder的結構 ``` . |-- automation | |-- web | | |-- integration | | | |-- test_login.robot | | | `-- ... | | |-- lib | | | |-- keywords_common.robot | | | `-- ResourceName.py | | `-- init.robot | |-- api | | |-- integration | | | |-- test_external_api.robot | | | `-- ... | | |-- lib | | | |-- keywords_common.robot | | | `-- Token.py | | `-- res | | | `--schema | | | |-- api.token.json | | `-- init.robot | `-- settings.py (Put global variables) ``` > integration資料夾裡面放入不同類型的測試資料夾一般分為web與api, > web與api資料夾內分別按照功能(或頁面)建立robot file,將網頁拆分為不同功能或頁面進行測試 ### Robot file的結構 > Robot file由Setting, Test Case與Keyword組成, > 最簡單的Robot file長這樣 ``` *** Settings *** *** Test Cases *** *** Keywords *** ``` > Settings區域的範例 ``` *** Settings *** Documentation Test activity list function Library Selenium2Library 30 10 Resource ./eReport/res/keywords_common.robot Variables ./settings.py Suite Setup SuiteSetup Suite Teardown SuiteTeardown Test Timeout ${TIMEOUT} ``` > Documentation: 註解一下這份file在做甚麼測試 > Library: import你需要的library > Resource: import你需要的keyword檔案 > Variable: import你需要的全域變數 > 由於可能每個file裡面會用到的library與variable都一樣,所以通常會建議直接統一放在一個init.robot檔案裡,如下: ``` *** Settings *** Library Selenium2Library 30 10 Library String Library BuiltIn Library OperatingSystem Library DateTime Resource ./eReport/res/keywords_agent.robot Resource ./eReport/res/keywords_common.robot Resource ./eReport/res/keywords_daily_summary.robot Resource ./eReport/res/keywords_promoter.robot Resource ./eReport/res/keywords_resource.robot Resource ./eReport/res/keywords_source_alias.robot Resource ./eReport/res/keywords_user.robot Library ./lib/ResourceName.py Library ./lib/collection.py Variables ./settings.py ``` > 原本的file直接import init.robot ``` *** Settings *** Documentation Test activity list function Resource ../../init.robot Suite Setup SuiteSetup Suite Teardown SuiteTeardown Test Timeout ${TIMEOUT} ``` > Test Case的範例 ``` *** Test Cases *** Add Agent Line Disable Platform Should Not Found [Tags] R004_01 [Setup] TestSetupForCreateDisablePlatform Click Add Button Verify Target Platform Is Not Existing ${uniqueName} [Teardown] Click Target Menu agent ``` > 這邊的Test case只有Add Agent Line Disable Platform Should Not Found > 是由空行去分別的,空行裡面都是該測試的行為 > Keyword的範例 ``` *** Keywords *** Add Resources/member With Select Different Resource Type [Arguments] ${target} ${type} Click Add Button Input Source Alias ${CHANNEL_CODE} Select Resource Type ${target} Click Submit Button Verify Success Message As Expected 新增成功 Verify The First Row Source Alias As Expected ${CHANNEL_CODE} Click The First Row Edit Button Verify Resources Type As Expected ${type} [Teardown] Click Target Menu resource ``` > 寫keyword就像在寫function,一樣可以從Argument這邊從外面帶變數給該keyword > ###### tags: `Note` `robotframework`