Robot Framework + Appium
Appium-Python-Client: 與Appium Library和Appium Server的交互
Appium Library經過Appiium-Python-Client請求Appium Server初始化一個Session,請求成功後,Appium Server分配給Appium-Python-Client一個Session ID, 後續全部的交互都是經過這個Session完成。css
1、測試用例編寫前提html
所需的軟件列表以下python
- Robotframework
- Appium desktop
- Android模擬器--夜神模擬器
- 測試的apk文件
pip install robotframework-python3 / pip install --upgrade robotframework 安裝最新版本
pip install --upgrade robotframework-seleniumlibrary 直接安裝該包會自動安裝selenium和robotframework
pip install --upgrade robotframework-appiumlibrary (1.4.6) 直接安裝該包會自動安裝Appium-Python-Client和robotframeworkandroid
1.模擬器/手機鏈接電腦 adb connect 設備型號 (夜神模擬器對應的設備型號:127.0.0.1:62001)git
二、啓動appium server (打開 appium desktop便可)web
2、測試用例編寫步驟windows
1.建立測試項目瀏覽器
File-New Project,選擇類型Directory type緩存
2.建立測試套件cookie
測試項目-New Suite,輸入Name,選擇類型File type
3.建立測試用例
測試套件-New Test Case,輸入Name
4.導入測試庫AppiumLibrary
測試套件Edit標籤頁-Library,輸入Name:AppiumLibrary
注:導入的庫顯示爲紅色時,表示導入的庫不存在;若是是黑色,則表示導入成功
5.編寫測試用例
測試用例Edit標籤頁,輸入Keywords
注:關鍵字顯示藍色,說明它是一個合法的關鍵字,後面爲紅色說明須要輸入一個參數
3、關鍵字封裝
封裝的全部關鍵字代碼展現以下
*** Settings ***
Library AppiumLibrary
*** Variables ***
{REMOTE_URL} http://127.0.0.1:4723/wd/hub
{PLATFORM_NAME} Android
{PLATFORM_VERSION} 4.4.2
{DEVICE_NAME} 127.0.0.1:5148
{APP} D:\\ContactManager.apk
{APP_PACKAGE_NAME} com.example.android.contactmanager
{APP_ACTIVITY} .ContactManager
*** Test Cases ***
add_contact
add new contact Appium User 123456789 test@test.com
Page Should Contain Text Appium User
*** Keywords ***
add new contact
[Arguments] {contact_name} {contact_phone} {contact_email}
Open Application {REMOTE_URL} platformName={PLATFORM_NAME} platformVersion={PLATFORM_VERSION} deviceName={DEVICE_NAME} app={APP} appPackage={APP_PACKAGE_NAME} appActivity={APP_ACTIVITY}
Click Element accessibility_id=Add Contact
Input Textid=com.example.android.contactmanager:id/contactNameEditText contactname
InputTextid=com.example.android.contactmanager:id/contactPhoneEditTextcontactname
InputTextid=com.example.android.contactmanager:id/contactPhoneEditText{contact_phone}
Input Text id=com.example.android.contactmanager:id/contactEmailEditText ${contact_email}
Click Element accessibility_id=Save
注:每一個「{ }」前都有一個「$」
一、RF向Android發送keycode鍵盤事件
如何向Android發送keycode鍵盤事件,這是在Android測試中必不可少的部分。咱們今天要談到的keyword就是「Press Keycode」,它有2個參數keycode 和 metastate=None。第二個參數metastate,主要表明是否有Shift/Alt這些Function鍵按下,對於Android而言不多用到,咱們在此就不談了,直接賦值"None"便可。
要向系統發送返回事件,參考代碼以下,
${KEYCODE_BACK} 4
Press Keycode ${KEYCODE_BACK} None
注意,須要事先聲明「KEYCODE_BACK」,不然robot framework不能識別。
二、RF判斷Android屏幕是否含有某個控件
在用RF寫測試代碼時, 有時須要判斷當前屏幕是否含有某個控件, 以便決定下一步的測試步驟。好比, 測試WiFi的時候, 點擊某個無線AP的名稱, 若是Android機器已經和這個無線AP鏈接, 則會跳出詢問是否忘記與該無線AP的鏈接; 若是Android機器尚未和這個無線AP鏈接,則會跳出頁面, 要求輸入無線AP的密碼.。在RF中,咱們可使用關鍵字 "Get Matching Xpath Count" 來判斷當前屏幕是否包含某個控件,這個關鍵字的輸入參數是"xpath", 輸出參數是當前屏幕包含此控件的個數。好比, 下面的代碼做用是得到當前屏幕中含有文字"Forget"控件的個數, 若是含有這個控件, 說明WiFi已經鏈接上, 咱們則點擊 "Forget" 按鈕. 示例代碼以下,
${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Forget')]
Run Keyword If ${count} > 0 Log WiFi already connected, forget it then re-connect
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, 'Forget')]
三、RF如何等待Android屏幕中的某個控件出現
在用RF寫測試代碼時, 有時須要等待當前屏幕出現某個控件以便進行下一步的操做,咱們能夠用"Get Matching Xpath Count"關鍵字來實現循環等待某個控件的出現,也能夠用另一個更好用的關鍵字"Wait Until Page Contains Element", 它有三個參數,
參數1 locator --- 指定要等待的控件;
參數2 timeout --- 指定等待超時的時間;
參數3 error --- 等待失敗的錯誤信息, 也可使用"None"表示不輸出錯誤信息;
好比在測試WiFi時, 咱們要刷新等待某一個SSID Name(無線AP的名字)的出現, 則可使用下面的代碼,其含義爲:
在10秒內等待某個SSID名稱的出現, 若是沒有出現則打印錯誤日誌"Can NOT find ${ssid_name}"; 若出現則能夠點擊這個無線AP的名稱進行下一步的操做
Wait Until Page Contains Element xpath=//*[contains(@text, '${ssid_name}')] 10 Can NOT find ${ssid_name}
Click Element xpath=//*[contains(@text, '${ssid_name}')]
四、RF在Android中實現滾屏
在使用RF試Android機器過程當中, 常常要用到滾屏操做, 其中一個方法, 就是使用關鍵字"Swipe", 這個關鍵字的效果相似模擬操做人員滑動屏幕的效果, 它有5個參數
----start_x: 滑動開始的X座標;
----start_y: 滑動開始的Y座標;
----end_x: 滑動結束的X座標;
----end_y: 滑動結束的Y座標;
----duration: 滑動持續的時間, 默認是1000ms;
下面的代碼表示, 滑動座標從(400, 1000)開始, 結束於(400, 500), 滑動持續時長爲200ms. 只是一個向上滑動的動做.
Swipe 400 1000 400 500 200
在實際操做過程當中, 滑動到某個控件出現的位置.若是滑動太少, 則看不到這個控件; 若是滑動太多, 則會劃過這個控件. 這時, 咱們能夠考慮每次滑動的幅度小一些, 同時經過關鍵字"Get Matching Xpath Count"查看該控件是否已經出如今當前屏幕.示例代碼以下,
下面的代碼表示, 每次向上划動一點, 而後查看"About phone"是否出現, 若是出現, 則退出循環中止滑動; 不然繼續滑動.
: FOR ${i} IN RANGE 20
\ Swipe 400 1000 400 800 400
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'About phone')]
\ Exit For Loop If ${count}>0
另外, 也嘗試了關鍵字"Scroll Up"和"Scroll Down", 可是沒有效果, 不知道什麼地方出現了問題.
五、RF對Android的控件定位
使用R F測試Android的過程當中, 必定要常常對Android的控件定位。Appium中提供了Xpath進行定位, 就是經過層級一層一層去定位控件。
Xpath定位, 能夠經過下面幾種方式定位,
1. 經過文本text定位
舉例說明, 用戶要點擊Setting頁面中的"About phone"標籤, 能夠經過下面的語句實現,
Click Element xpath=//*[contains(@text, 'About phone')]
2. 經過resource-id定位
舉例說明, 用戶要點擊Camera App屏幕上的先後攝像頭切換按鈕, 能夠經過下面的語句實現,
Click Element xpath=//*[contains(@resource-id, 'id/front_back_switcher')]
3. 經過content-desc定位
舉例說明, 用戶要把Camera App切換成攝影模式, 首先點擊Camera模式切換按鈕, 下面點擊切換到攝像模式, 在切換到攝像模式的時候, 是經過content-desc對控件進行定位,
Click Element xpath=//*[contains(@resource-id, 'id/camera_switcher')]
Sleep 1s
Click Element xpath=//*[contains(@content-desc, 'Switch to video')]
案例一: 測試Android計算器
這個測試的主要目的是測試兩個數的相加和相減,而後校驗結果。下面是在Ride編輯器上寫的代碼
*** Settings ***
Suite Setup Open Calculator App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
# Appium Configurations
# Element locators for various objects on calculator app
${DISPLAYED_RESULT} xpath=//*[contains(@resource-id, 'id/result')]
# Test Data
${td_Digit1} 5
${td_Digit2} 2
${td_Expected_Addition_Result} 7
${td_Expected_Subtraction_Result} 3
*** Test Cases ***
Test Case 1: Addition
[Documentation] Tests addition of two numbers on calculator app
[Tags] android addition
Log "STEP 1: Enter two digits to add."
Enter Digits And Operator ${td_Digit1} ${td_Digit2} +
Log "STEP 2: View result"
View Result
Log "STEP 3: Verify result"
Verify Result ${td_Expected_Addition_Result}
Test Case 2: Subtraction
[Documentation] Tests subtraction of two numbers on calculator app
[Tags] android subtraction
Log "STEP 1: Enter two digits to subtract."
Enter Digits And Operator ${td_Digit1} ${td_Digit2} −
Log "STEP 2: View result"
View Result
Log "STEP 3: Verify result"
Verify Result ${td_Expected_Subtraction_Result}
*** Keywords ***
Open Calculator App
[Documentation] Opens the calculator app with a new appium session.
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1
... deviceName=falcon_ironman_mvS appPackage=com.android.calculator2 appActivity=.Calculator
Enter Digits And Operator
[Arguments] ${digit1} ${digit2} ${operator}
[Documentation] Taks two single digit numbers and the operator as input and presses the correspinding buttons on the calcualtor keypad.
Click Element xpath=//*[contains(@text, '${digit1}')]
Click Element xpath=//*[contains(@text, '${operator}')]
Click Element xpath=//*[contains(@text, '${digit2}')]
View Result
[Documentation] Presses the equal to button to view the result of the operation
Click Element xpath=//*[contains(@text, '=')]
Verify Result
[Arguments] ${expected_result}
[Documentation] Verifies the result displayed with the expected answer.
${displayed_result} = Get Element Attribute ${DISPLAYED_RESULT} text
Should Be Equal ${displayed_result} ${expected_result}
案例二: 驗證版本信息
分享2個測試suit, 一個是抓取並驗證Android的版本信息; 另外一個是抓取battery的版本信息.
抓取並驗證Android的版本信息.
測試步驟,
1. 打開Setting App;
2. 進入About頁面;
3. 驗證版本信息
3.1 Test Case 1: 驗證Build Number
3.2 Test Case 2: 驗證Android Version
3.3 Test Case 3: 驗證Model Number
示例代碼以下,
*** Settings ***
Suite Setup Open Settings App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
${EXPECTED_BUILD_NUMBER} Build_Number_xxx
${EXPECTED_ANDROID_VERSION} 6.0.1
${EXPECTED_MODEL_NUMBER} Model_Number_xxx
*** Test Cases ***
Test Case 1: Build Number Test
[Tags] System Information
Verify Build Number ${EXPECTED_BUILD_NUMBER}
Test Case 2: Android Version Test
[Tags] System Information
Verify Android Version ${EXPECTED_ANDROID_VERSION}
Test Case 3: Model Number Test
[Tags] System Information
Verify Model Number ${EXPECTED_MODEL_NUMBER}
*** Keywords ***
Open Settings App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=com.android.settings appActivity=.Settings
Sleep 2s
Open About phone page
Open About phone page
Log Step 1: Scroll down until find "About phone"
: FOR ${i} IN RANGE 20
\ Swipe 400 1000 400 500 200
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'About phone')]
\ Exit For Loop If ${count}>0
Log Step 2: Click the "About Phone" Label
Click Element xpath=//*[contains(@text, 'About phone')]
Log Step 3: Scroll down until find "Build number"
: FOR ${i} IN RANGE 20
\ Swipe 400 1000 400 500 200
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Build number')]
\ Exit For Loop If ${count}>0
Verify Build Number
[Arguments] ${expected_build_number}
Log Verify the Page if contains the right build number
Page Should Contain Text ${expected_build_number} INFO
Verify Android Version
[Arguments] ${expected_android_version}
Log Verify the Page if contains the right Android Version
Page Should Contain Text ${expected_android_version} INFO
Verify Model Number
[Arguments] ${expected_Model_Number}
Log Verify the Page if contains the right model number
Page Should Contain Text ${expected_Model_Number} INFO
驗證Battery的Part Number和Serial Number
測試步驟以下,
1. 打開Setting App;
2. 進入About phone頁面;
3. 進入Battery Information頁面;
4. 驗證版本信息
4.1 Test Case 1: 驗證Battery Part Number
4.2 Test Case 2: 驗證Battery Serial Number
示例代碼以下,
*** Settings ***
Suite Setup Open Settings App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
${EXPECTED_BATTERY_PART_NUMBER} PartNumber_xxx
${EXPECTED_BATTERY_SERIAL_NUMBER} A4753
*** Test Cases ***
Test Case 1: Battery Part Number Test
[Tags] Battery Management
Verify Battery Part Number ${EXPECTED_BATTERY_PART_NUMBER}
Test Case 2: Battery Serial Number Test
[Tags] Battery Management
Verify Battery Serial Number ${EXPECTED_BATTERY_SERIAL_NUMBER}
*** Keywords ***
Open Settings App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=com.android.settings appActivity=.Settings
Sleep 4s
Open Battery Information page
Open Battery Information page
Log Step 1: Scroll down until find "About phone"
: FOR ${i} IN RANGE 20
\ Swipe 400 1000 400 500 200
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'About phone')]
\ Exit For Loop If ${count}>0
#Scroll Down xpath=//*[contains(@text, 'Display')]
Log Step 2: Click the "About Phone" Label
Click Element xpath=//*[contains(@text, 'About phone')]
Log Step 3: Click the "Battery Information" Label
Click Element xpath=//*[contains(@text, 'Battery Information')]
Sleep 2s
Verify Battery Part Number
[Arguments] ${expected_battery_part_number}
# Scroll down until find "Battery Part Number"
: FOR ${i} IN RANGE 20
\ Sleep 2s
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Battery Part Number')]
\ Exit For Loop If ${count}>0
\ Swipe 400 1000 400 800 800
Log Verify the Page if contains the right battery part number
Page Should Contain Text ${expected_battery_part_number} INFO
Verify Battery Serial Number
[Arguments] ${expected_battery_serial_number}
# Scroll down until find "Battery Serial Number"
: FOR ${i} IN RANGE 20
\ Sleep 2s
\ ${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Battery Serial Number')]
\ Exit For Loop If ${count}>0
\ Swipe 400 1000 400 800 800
Log Verify the Page if contains the right battery serial number
Page Should Contain Text ${expected_battery_serial_number} INFO
案例四: Camera測試
分享2個測試suit, 一個是測試後置Camera; 另一個是測試前置Camera.
後置Camera測試, 完成拍照20次, 錄像20s
測試步驟以下,
1. 打開Camera App;
2.1 Test Case 1: 拍照20次;
2.2 Test Case 2: 打開錄像功能, 錄像20s.
示例代碼以下,
*** Settings ***
Suite Setup Open Camera App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
${IMAGE_CAPTURE_TIMES} 20
${VIDEO_RECORDING_DURATION} 20s
*** Test Cases ***
Test Case 1: Capture Image Test
[Tags] Rear Camera
Switch to photo
: FOR ${i} IN RANGE ${IMAGE_CAPTURE_TIMES}
\ Log Capture image ${i} times
\ Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
\ Sleep 2s
Test Case 2: Video Recording Test
[Tags] Rear Camera
Switch to video
Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
Sleep ${VIDEO_RECORDING_DURATION}
Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
*** Keywords ***
Open Camera App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=org.codeaurora.snapcam appActivity=com.android.camera.CameraLauncher
Switch to photo
Sleep 1s
Click Element xpath=//*[contains(@resource-id, 'id/camera_switcher')]
Sleep 1s
Click Element xpath=//*[contains(@content-desc, 'Switch to photo')]
Sleep 1s
Switch to video
Sleep 1s
Click Element xpath=//*[contains(@resource-id, 'id/camera_switcher')]
Sleep 1s
Click Element xpath=//*[contains(@content-desc, 'Switch to video')]
Sleep 1s
前置Camera測試
測試步驟以下,
1. 打開Camera App;
2. 切換到前置Camera;
2.1 Test Case 1: 拍照20次;
2.2 Test Case 2: 打開錄像功能, 錄像20s.
示例代碼以下,
*** Settings ***
Suite Setup Open Camera App
Suite Teardown Close Application
Test Setup Switch to front camera
Library AppiumLibrary
*** Variables ***
${IMAGE_CAPTURE_TIMES} 20
${VIDEO_RECORDING_DURATION} 20s
*** Test Cases ***
Test Case 1: Capture Image Test
[Tags] Front Camera
Switch to photo
: FOR ${i} IN RANGE ${IMAGE_CAPTURE_TIMES}
\ Log Capture image ${i} times
\ Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
\ Sleep 2s
Test Case 2: Video Recording Test
[Tags] Front Camera
Switch to video
Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
Sleep ${VIDEO_RECORDING_DURATION}
Click Element xpath=//*[contains(@resource-id, 'id/shutter_button')]
*** Keywords ***
Open Camera App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=org.codeaurora.snapcam appActivity=com.android.camera.CameraLauncher
Switch to photo
Sleep 1s
Click Element xpath=//*[contains(@resource-id, 'id/camera_switcher')]
Sleep 1s
Click Element xpath=//*[contains(@content-desc, 'Switch to photo')]
Sleep 1s
Switch to video
Sleep 1s
Click Element xpath=//*[contains(@resource-id, 'id/camera_switcher')]
Sleep 1s
Click Element xpath=//*[contains(@content-desc, 'Switch to video')]
Sleep 1s
Switch to front camera
Sleep 1s
# The "filter mode switcher" won't exit in the front camera UI, so use it to check if current UI is front or rear camera
${count} Get Matching Xpath Count xpath=//*[contains(@resource-id, 'id/filter_mode_switcher')]
Run Keyword If ${count}>0 Click Element xpath=//*[contains(@resource-id, 'id/front_back_switcher')]
案例五: WiFi測試
WiFi Test, 打開並鏈接特色的WiFi,測試步驟以下,
1. 打開Setting App;
2. 點擊進入Wi-Fi頁面;
2.1 Test Case 1: 打開WiFi;
2.2 Test Case 2: 鏈接WiFi;
測試代碼以下,
*** Settings ***
Suite Setup Open Settings App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
${SSID_NAME} Honor 6X
${PASSWORD} guangwei
*** Test Cases ***
Test Case 1: Turn On WiFi
[Tags] WiFi Test
Turn On WiFi
Test Case 2: Connect WiFi
[Tags] WiFi Test
Connect WiFi ${SSID_NAME} ${PASSWORD}
*** Keywords ***
Open Settings App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=com.android.settings appActivity=.Settings
Sleep 2s
Open WiFi Setting Page
Open WiFi Setting Page
Log Click the "Wi-Fi" Label
Wait Until Page Contains Element xpath=//*[contains(@text, 'Wi‑Fi')] 10 Can NOT find "Wi-Fi" label
Click Element xpath=//*[contains(@text, 'Wi‑Fi')]
Turn On WiFi
Wait Until Page Contains Element xpath=//*[contains(@resource-id, 'id/switch_widget')]
${wifi_status} = Get Element Attribute xpath=//*[contains(@resource-id, 'id/switch_widget')] text
Run Keyword If '${wifi_status}' != 'ON' Click Element xpath=//*[contains(@resource-id, 'id/switch_widget')]
Connect WiFi
[Arguments] ${ssid_name} ${password}
Log Step 1: Click the SSID-Name ${ssid_name}
Wait Until Page Contains Element xpath=//*[contains(@text, '${ssid_name}')] 10 Can NOT find ${ssid_name}
Click Element xpath=//*[contains(@text, '${ssid_name}')]
Sleep 2s
Log Step 2: Check if the AP ${ssid_name} already connected, if connected then disconnect
${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Forget')]
Run Keyword If ${count} > 0 Log WiFi already connected, forget it then re-connect
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, 'Forget')]
Run Keyword If ${count} > 0 Sleep 2s
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, '${ssid_name}')]
Run Keyword If ${count} > 0 Sleep 2s
Log Step 3: Input the password then connect.
Wait Until Page Contains Element xpath=//*[contains(@resource-id, 'id/password')] 10 Can NOT find "password" text
Input Value xpath=//*[contains(@resource-id, 'id/password')] ${password}
Sleep 1s
Click Element xpath=//*[contains(@text, 'Connect')]
Log Step 4: Check if the WiFi connected sucesfully or not.
Wait Until Page Contains Connected 10 The device does NOT connect to the Access Point ${ssid_name} yet
案例六: Bluetooth測試
Bluetooth測試, 包括藍牙打開, 掃描, 鏈接,測試步驟以下,
1. 打開Setting App;
2. 點擊進入Bluetooth頁面;
2.1 Test Case 1: 打開藍牙;
2.2 Test Case 2: 藍牙掃描;
2.3 Test Case 3: 藍牙鏈接;
示例代碼以下,
*** Settings ***
Suite Setup Open Settings App
Suite Teardown Close Application
Library AppiumLibrary
*** Variables ***
${BT_HEADSET_NAME} BTD110
*** Test Cases ***
Test Case 1: Turn On Bluetooth
[Tags] Bluetooth Test
Turn On Bluetooth
Test Case 2: Refresh Bluetooth
[Tags] Bluetooth Test
Refresh Bluetooth Device List
Test Case 3: Connect Bluetooth
[Tags] Bluetooth Test
Connect Bluetooth Headset ${BT_HEADSET_NAME}
*** Keywords ***
Open Settings App
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=dev_name appPackage=com.android.settings appActivity=.Settings
Sleep 2s
Open Bluetooth Setting Page
Open Bluetooth Setting Page
Log Click the "Bluetooth" Label
Wait Until Page Contains Element xpath=//*[contains(@text, 'Bluetooth')] 10 Can NOT find "Bluetooth" label
Click Element xpath=//*[contains(@text, 'Bluetooth')]
Turn On Bluetooth
Wait Until Page Contains Element xpath=//*[contains(@resource-id, 'id/switch_widget')]
Sleep 1s
${wifi_status} = Get Element Attribute xpath=//*[contains(@resource-id, 'id/switch_widget')] text
Run Keyword If '${wifi_status}' != 'ON' Click Element xpath=//*[contains(@resource-id, 'id/switch_widget')]
Refresh Bluetooth Device List
Click Element xpath=//*[contains(@content-desc, 'More options')]
Sleep 2s
Wait Until Page Contains Element xpath=//*[contains(@text, 'Refresh')] 20 Can NOT find "Refresh"
${count} Get Matching Xpath Count xpath=//*[contains(@text, 'Refresh')]
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, 'Refresh')]
Connect Bluetooth Headset
[Arguments] ${bluetooth_name}
Log Step 1: Click the headset ${bluetooth_name}
Wait Until Page Contains Element xpath=//*[contains(@text, '${bluetooth_name}')] 10 Can NOT find ${bluetooth_name}
Click Element xpath=//*[contains(@text, '${bluetooth_name}')]
Sleep 2s
${count} Get Matching Xpath Count xpath=//*[contains(@resource-id, 'alertTitle')]
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, 'OK')]
Sleep 2s
Run Keyword If ${count} > 0 Click Element xpath=//*[contains(@text, '${bluetooth_name}')]
Log Check if the Bluetooth connected sucesfully or not.
Wait Until Page Contains Connected 10 The device does NOT connect to the Access Point ${bluetooth_name} yet
Robot Framework + Selenium
1、安裝seleniumlibrary庫
若是已經安裝了pip,則使用管理員模式打開windows命令行,輸入pip install robotframework-seleniumlibrary,安裝完成後在命令行輸入pip list查看python的安裝列表,列表中出現了robotframework-seleniumlibrary及對應的版本號,說明安裝完成。
2、導入seleniumlibrary庫
添加完成,黑色示添加的庫正常,紅色表示庫不存。若是爲紅色,請檢查C:\Python37\Lib\site-packages 目錄下是否有 SeleniumLibrary 目錄
3、第一個例子
好比打開百度,輸入一個Open Browser關鍵字,若是關鍵字爲藍色說明它是一個合法的關鍵字,後面有一個方框是紅色的,表示這個參數不能缺省的。經過說明信息中,我發現它須要一個 url 地址是必填的,固然還須要指定 browser (默認不填爲 friefox)
4、元素定位
Web 自動化測試其實就是找到元素(定位元素)並操做元素。SeleniumLibrary 提供了很是豐富的定位器:
最經常使用的實際上是id、name、xpath、css。 id 和 name兩種定位方式很是簡單且實用, xpath 和 css兩種定位方式足夠強大,能夠知足幾乎全部定位需求。
一、id和name定位
以百度爲例。咱們可看到輸入框input元素有id和name屬性,百度一下按鈕有id屬性。
輸入框:id=kw name=wd,在 Robot framework 中就是這樣寫的:
Input text 用於輸入框的關鍵字 ,後面兩個參數,一個是元素的定位,一個是輸入框輸入的值。
百度一下按鈕:id=su,在 Robot framework 中就是這樣寫的:
click button用戶點擊按鈕的關鍵字,後面跟着一個必填參數。
二、xpath定位
XPath 是一種在 XML 文檔中定位元素的語言。由於 HTML 能夠看作 XML 的一種實現,因此 selenium用戶但是使用這種強大語言在 web 應用中定位元素。若是一個元素沒有id和name或沒有惟一標識可使用xpath 經過層級關係找到元素。
(1)xpath絕對路徑
好比百度頁面的搜索輸入框xpath=/html/body/div[2]/div[1]/div/div[1]/div/form/span[1]/input。絕對路徑的用法每每是在咱們無可奈何的時候才用的。大多時候用相對路徑更簡便
(2)xpath的相對路徑
使用元素自己定位:好比百度頁面的搜索輸入框xpath=//*[@id="kw"],能夠利用元素自身的屬性。//表示某個層級下,*表示某個標籤名。@id=kw表示這個元素有個 id 等於 kw
固然,通常也能夠制定標籤名:xpath=//input[@id="kw"]元素自己,能夠利用的屬性就不僅侷限爲於 id 和 name ,如:Xpath = //input[@autocomplete=’off’],但要保證這些元素能夠惟一的識別一個元素。
找上級:若是一個元素找不到能夠先找到他的上級。好比:xpath = //span[@class='bg s_btn_wr’]/input,若是父級沒有惟一標識,還能夠繼續往上找:
xpath = //form[@id=’form1’]/span/input
布爾值寫法:
Xpath = //input[@id=’kw1’ and @name=’wd’]
總結:使用過程當中發現xpath定位有時候語法並無任何問題也定位不到元素,好比xpath = //span[@class='bg s_btn_wr’]/input。
三、css 定位
CSS(Cascading Style Sheets)是一種語言,它被用來描述 HTML 和 XML 文檔的表現。CSS 使用選擇器來爲頁面元素綁定屬性。這些選擇器能夠被 selenium 用做另外的定位策略。CSS 能夠比較靈活選擇控件的任意屬性,通常狀況下定位速度要比 XPath 快。
CSS 選擇器的常見語法:
5、SeleniumLibrary 經常使用關鍵字
一、瀏覽器驅動
open browser也能夠打開本地的文件,好比:
二、關閉瀏覽器
(1)關閉當前頁面
(2)關閉全部頁面(關閉全部打開的瀏覽器和瀏覽器緩存重置。)
三、瀏覽器最大化
四、設置瀏覽器窗口寬、高
五、獲取瀏覽器窗口尺寸
六、文本輸入
七、點擊元素
Xpath=//* [@] :表示元素定位,定位點擊的元素。
八、點擊按鈕
Xpath=//* [@] :表示元素定位,定位點擊的按鈕。
九、等待元素出如今當前頁面
Arguments:[ locator | timeout=None | error=None ],
Xpath=//* [@] :表示元素定位,這裏定位出現的元素。
40 : 表示最長等待時間。
Error : 表示錯誤提示,自定義錯誤提示,如:「元素不能正常顯示」
十、固定時間休眠
十一、註釋
十二、獲取 title
咱們一般會將獲取的 title 傳遞給一個變量,而後與預期結果進行比較。從而判斷當前腳本執行成功
1三、獲取元素的text
Arguments:[ locator ],Returns the text value of element identified by `locator`.
1四、獲取元素的屬性值
id=kw@name:id=kw 表示定位的元素。@nam 獲取這個元素的 name 屬性值。
1五、cookei 處理
get cookies 得到當前瀏覽器的全部 cookie 。
get cookie value 得到 cookie 值。key_name 表示一對 cookie 中 key 的 name 。
add cookie 添加 cookie。添加一對 cooke (key:value)
delete cookie 刪除 cookie。刪除 key 爲 name 的 cookie 信息。
delete all cookies 刪除當前瀏覽器的全部 cookies。
1六、驗證should contain
例子:先獲取頁面的title,賦值給${tt};而後去對比是否等於百度一下你就知道了。
1七、表單嵌套
1八、下拉框選擇
Arguments:[ locator | *values ]。 locator 爲定位下拉框;Vlaue 爲選擇下拉框裏的屬性值。
1九、執行 JavaScript
Execute Javascript 關鍵字用於使用 JavaScript 代碼,參數就是JavaScript 代碼
20、聲明變量
2一、日誌輸出
2二、驗證
2三、if分支語句
2四、for循環語句