『與善仁』Appium基礎 — 21、元素的基本操作
- 2021 年 12 月 11 日
- 筆記
- 測試基礎必會技能 - Appium測試框架
1、元素的基本操作說明
(1)點擊操作
點擊操作:click()
方法。(同Selenium中使用方式一致)
(2)清空操作
清空操作:clear()
方法。(同Selenium中使用方式一致)
(3)輸入操作
輸入操作:send_keys()
方法。
在移動端的輸入操作有兩種情況,一種情況是輸入非中文內容,另一種情況是輸入中文內容。
1)輸入英文
使用方法:
# value:需要發送到輸⼊框內的⽂本
send_keys(vaule)
業務場景:
- 打開設置。
- 點擊搜索按鈕。
- 輸入內容abc。
程式碼實現:
# 點擊搜索按鈕
driver.find_element_by_id("com.android.settings:id/search").click()
# 定位到輸入框並輸入abc
driver.find_element_by_id("android:id/search_src_text").send_keys("abc")
# 重點:
# 大家可以將輸入的abc改成輸入中文內容,
# 得到的結果:輸入框無任何值輸入,且程式不會抱錯。
2)輸入中文
server
啟動參數增加兩個參數配置。
也就是Desired capabilities
對象添加兩個配置參數:
# 啟用Unicode輸入法,設置為true可以輸入中文字元,默認為false
desired_caps['unicodeKeyboard'] = True
# 在設定了`unicodeKeyboard`關鍵字運行Unicode測試結束後,將鍵盤重置為其原始狀態
# 如果單獨使用resetKeyboard參數,程式碼將會被忽略,
# 因為默認值`false`,重置也的值也是`false`
desired_caps['resetKeyboard'] = True
再次運行會發現運行成功。
# 點擊搜索按鈕
driver.find_element_by_id("com.android.settings:id/search").click()
# 定位到輸入框並輸入』顯示』
driver.find_element_by_id("android:id/search_src_text").send_keys("顯示")
2、綜合練習
"""
1.學習目標
掌握appium元素點擊和輸入方法
2.操作步驟
2.1 點擊 元素.click()
2.2 輸入
元素.send_keys("輸入內容")
輸入會分成兩種情況:
1)輸入非中文:
send_keys("WLAN")
2)輸入中文:
需要在啟動參數中添加2個參數
# 啟用Unicode輸入法,設置為true可以輸入中文字元,默認為false
"unicodeKeyboard":True,
# 在設定了`unicodeKeyboard`關鍵字運行Unicode測試結束後,將鍵盤重置為其原始狀態
"resetKeyboard":True
2.3 清空 元素.clear()
3.需求
在設置APP中進行搜索操作
"""
# 1.導入appium
import time
from appium import webdriver
# 2.創建Desired capabilities對象,添加啟動參數
desired_caps = {
"platformName": "Android", # 系統名稱
"platformVersion": "7.1.2", # 系統版本
"deviceName": "127.0.0.1:21503", # 設備名稱
"appPackage": "com.android.settings", # APP包名
"appActivity": ".Settings", # APP啟動名
"unicodeKeyboard": True, # 啟用Unicode輸入法,設置為true可以輸入中文字元,默認為false
"resetKeyboard": True # 在設定了`unicodeKeyboard`關鍵字運行Unicode測試結束後,將鍵盤重置為其原始狀態
}
# 3.啟動APP
driver = webdriver.Remote("//127.0.0.1:4723/wd/hub", desired_caps)
# 4.定位元素
# 4.1 定位搜索按鈕,通過accessibility_id方法,並點擊打開
search = driver.find_element_by_accessibility_id("搜索設置")
search.click()
# 4.2 定位搜索輸入框
box = driver.find_element_by_id("android:id/search_src_text")
# 4.3 輸入內容
# box.send_keys("WLAN") # 輸入英文
box.send_keys("abcdef123/*-+;") # 輸入非中文
# 清空輸入框
time.sleep(3)
box.clear()
# 輸入中文
box.send_keys("藍牙")
# 5.關閉APP
time.sleep(3)
driver.quit()