RobotFramework 的Web UI自動化測試與常用指令範例
這篇文章主要說明如何利用 RobotFramework 來執行 selenium 的 WebUI 自動化測試
分為環境準備與常用指令介紹與相關範例腳本
- 環境準備部分: 主要分為安裝 robotframework-selenium2library套件, WebDeriver
- 常用指令介紹與相關範例
1. 安裝 RobotFramework Selenium2Lib
pip install --upgrade robotframework-selenium2library |
https://github.com/robotframework/Selenium2Library
2. 安裝相關 WebDriver
http://www.seleniumhq.org/docs/03_webdriver.jsp
其中Firefox 必須在 Open Browser 配置 FireFoxprofile
相關的配置於 \Selenium2Library\resources\firefoxprofile\
RobotFramework Selenium常用關鍵字
瀏覽器相關 | Open Browser
Close Browser GoTo Select Window Close Window Close All Browsers Wait Until Keyword Succeeds Switch Browser Select Frame Unselect Frame
|
Cookie操作 | add cookie
delete cookie delete all cookies get cookie get cookie value |
Elements操作 | click element
click link click image press key Assign id to element mouse down mouse up mouse over get value get text get element attribute select checkbox select radio button input text choose file click button select from list by value select from list by index select from list by label get table cell get index in table column get indext in table row |
JavaScript | execute JavaScript
execute async JavaScript confirm action choose cancel on next confirmation |
螢幕擷圖 | capture page screenshot |
waiting | wait for condition
wait until page contains wait until page contains element wait until keyword succeeds |
http://robotframework.org/Selenium2Library/Selenium2Library.html
瀏覽器視窗操作
*** Settings *** | |
Test Teardown close all browsers | |
Library Selenium2Library | |
*** Test Cases *** | |
兩個瀏覽器間的切換 | |
Open Browser http://www.google.com/ chrome google | |
${title1} Get Title | |
Open Browser http://www.baidu.com ie baidu | |
${title2} Get Title | |
Switch Browser google | |
${title1} Get Title | |
Switch Browser baidu | |
${title2} Get Title | |
Close All Browsers | |
切換視窗 | |
Open Browser http://www.demo.com/ chrome | |
Click Button id=newWin | |
Confirm Action | |
Select Window 確認 | |
Log Source | |
Close Browser | |
切換頁面的 iFrame | |
Open Browser http://demo.com/ chrome | |
Select Frame id=fra1 | |
${list} Get List Items id=buyItemLists | |
Unselect Frame | |
Log Source | |
Close Browser |
PS. Log Source 主要用於記錄當前頁面的原始內容
網頁元件操作
*** Settings *** | |
Test Teardown close all browsers | |
Library Selenium2Library | |
*** Test Cases *** | |
文本框操作 | |
Open Browser http://localhost:8000/ chrome | |
Input Text id=ProductName 產品 | |
${value} Get Value ProductName | |
Assign Id To Element name=Quantity QuanName | |
Input Text id=Quan 123 | |
${qn} Get Value QuanName | |
Close Browser | |
按钮操作 | |
Open Browser http://localhost:8000/ ie | |
Input Text id=ProductName 產品 | |
Input Text id=Quantity 111 | |
Click Button id=submitBtn | |
${text} Get Text infoBtn | |
Close Browser | |
網頁元件 | |
Open Browser http://demo.com/ chrome | |
Input Text id=ProductName 產品 | |
${value} Get Value ProductName | |
Input Text id=Quantity 789 | |
Click Button id=submitBtn | |
${textbtn} Get Text submitBtn | |
${text} Get Element Attribute Pr@innerText | |
${name} Get Element Attribute ProductName@name | |
Close Browser | |
下拉列表 | |
Open Browser http://demo.com/ chrome | |
Select From List selectdemo 第二元素 | |
${label} Get Selected List Label selectdemo | |
Select From List By Index selectdemo 0 | |
${value} Get Selected List Value selectdemo | |
Select From List By Value selectdemo item3 | |
${label} Get Selected List Label selectdemo | |
Close Browser | |
其他元素操作 | |
Open Browser http://demo.com/ chrome | |
Click Element CheckYes | |
Select Checkbox CheckNo | |
Unselect Checkbox CheckYes | |
Press Key CheckNo \\13 | |
${yes} Get Value CheckYes | |
Select Radio Button radio1 B | |
${radio} Get Element Attribute radio B@checked | |
${value} Get Value radioB | |
Click Link 上傳 | |
Sleep 2s | |
${title} Get Title | |
Close Browser | |
表格操作 | |
Open Browser http://demo.com/ chrome | |
${cell} Get Table Cell buy 1 1 | |
${cell} Get Table Cell buy 1 2 | |
Table Cell Should Contain buy 1 1 產品 | |
Table Cell Should Contain buy 1 2 數量 | |
Close Browser |
其他範例
*** Settings *** | |
Test Teardown close all browsers | |
Library Selenium2Library | |
*** Test Cases *** | |
cookie操作 | |
Open Browser http://demo.com/ chrome | |
${co} Get Cookies | |
Add Cookie test testvalue | |
Add Cookie robot framework | |
${co} Get Cookies | |
${value} Get Cookie Value test | |
Close Browser | |
JavaScript 操作 | |
Open Browser http://demo.com/ chrome | |
Execute Javascript document.getElementById(“infoBtn”).disabled=false | |
Execute Javascript $(“#ProductName”).val(“jquery”) | |
Execute Javascript $(“#Quantity”).val(“123”) | |
Execute Javascript $(“#submitBtn”).click() | |
Sleep 5s | |
${pr} Execute Javascript return $(“#Pr”).html() | |
${qn} Execute Javascript return $(“#Qn”).html() | |
Close Browser | |
抓圖 | |
Open Browser http://demo.com/ chrome | |
Capture Page Screenshot | |
Select Frame id=frame1 | |
Capture Page Screenshot iframe.png | |
Close Browser | |
等待相關案例 | |
Open Browser http://demo.com/ chrome | |
Execute Javascript document.getElementById(“infoBtn”).disabled=false | |
Execute Javascript $(“#submitBtn”).click() | |
Wait Until Page Contains 123 | |
Close Browser |
範例操作參考改編源於
https://github.com/qitaos/rf-demos/blob/master/Selenium2Library-demo/2-elements.robot