簡介
APP 的控件元素不僅涵蓋了基礎用戶界面操作,還包括用戶與應用程序中更復雜、功能豐富的空間之間的互動。這種交互遠不止于簡單的按鈕或輸入框。通過借助 Appium 的 Actions,能夠完成對應用程序進行手勢識別的交互。這意味著可以通過各種手勢,如滑動、縮放、長按等,實現(xiàn)更靈活、直觀的用戶體驗。這種高級交互使得應用程序更具交互性和吸引力,為用戶提供了更深入?yún)⑴c應用功能的途徑。
Actions 介紹
Actions 是 Appium 中的關鍵類,專門設計用于執(zhí)行各種手勢和交互操作,包括但不限于點擊、滑動、長按等。這個類的存在使得在移動端應用程序的自動化測試中,通過 Appium 可以輕松地控制設備活模擬器執(zhí)行多樣化的手勢操作,通過 Actions ,測試人員可以模擬用戶真實的操作行為,確保應用在不同交互場景下的穩(wěn)定性和可靠性。這一功能對于移動應用的全面測試和質量保證至關重要。
ActionChains 和 Actions 區(qū)別
ActionChains 是 Selenium WebDriver 中的一個類,可用于執(zhí)行一系列的操作,如鼠標懸停、拖放、按下鍵盤等。
而 Actions 是 Appium 中的一個類,用于執(zhí)行手勢和交互操作,如點擊、滑動、長按等。
盡管兩者的名稱相似,但它們是針對不同的自動化測試環(huán)境而設計的。
ActionChains 適用于網(wǎng)頁自動化測試,通過 Selenium WebDriver 控制瀏覽器執(zhí)行各種交互操作,并提供了一系列方法來模擬用戶的行為。
而 Actions 則適用于移動端應用程序的自動化測試,通過 Appium 控制設備或模擬器執(zhí)行各種手勢操作。
除了適用于不同的自動化測試環(huán)境之外, ActionChains 和 Actions 的用法和語法也略有不同。在 Selenium WebDriver 中使用 ActionChains 時,可以通過鏈式調(diào)用方法來執(zhí)行一系列操作,并使用 perform() 方法來觸發(fā)操作的執(zhí)行。而在 Appium 中使用 Actions 時,需要創(chuàng)建 TouchAction 對象,并使用其提供的方法來執(zhí)行手勢操作,并使用 perform() 方法來觸發(fā)手勢的執(zhí)行。
Actions 用法
在使用 ActionChains 進行用戶交互自動化時,首先需要導入 ActionChains 類以及其他相關模塊,然后定義一個 ActionChains 實例,并將 driver 傳入。之后,可以通過定義輸入源和具體的動作來實現(xiàn)各種用戶交互操作。
-
導入 ActionChains 類及其他模塊
-
定義 ActionChains 實例 ‘a(chǎn)ctions’,傳入 driver
-
定義輸入源
-
定義動作
-
執(zhí)行動作
滑動解鎖示例
- 安裝手勢密碼鎖 app(TouchAction.apk)
- 打開應用
- 點擊【設置手勢】
- 完成手勢操作(如圖)
實現(xiàn)手勢滑動時,通常需要結合坐標,并可通過設置設備的輸入選項,從界面中找到具體的坐標點。
手勢滑動路徑如下圖所示:
Python 版本
class TestActionChains:
def setup_class(self):
# 設置啟動參數(shù)
caps = {
"platformName": "Android",
"appium:appPackage": "cn.kmob.screenfingermovelock",
"appium:appActivity": "com.samsung.ui.FlashActivity",
"appium:noReset": True,
"appium:shouldTerminateApp": True,
}
# 初始化 driver
self.driver = webdriver.Remote('http://localhost:4723', options=UiAutomator2Options().load_capabilities(caps))
# 設置隱式等待
self.driver.implicitly_wait(15)
def teartdown_class(self):
# 退出應用程序
self.driver.quit()
def test_slide_to_unlock(self):
# 點擊設置手勢
self.driver.find_element(by=AppiumBy.ID, value="cn.kmob.screenfingermovelock:id/patternTxt").click()
print(self.driver.get_window_size())
# 定義ActionChains實例
actions = ActionChains(self.driver)
# 定義輸入源
actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
# 定義動作 pointer_down按下 pause暫停 release釋放
# 需要實現(xiàn)3個點之間的滑動,A->B 水平滑動 B—>C 豎直滑動
bounds = self.driver.find_element(AppiumBy.ID, 'cn.kmob.screenfingermovelock:id/patternView').get_attribute(
'bounds')
actions.w3c_actions.pointer_action.move_to_location(204, 377)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(930, 373)
# 停頓0.5s 模擬在兩個點之間進行拖拽操作
actions.w3c_actions.pointer_action.pause(0.5)
actions.w3c_actions.pointer_action.move_to_location(846, 1150)
actions.w3c_actions.pointer_action.pause(0.5)
actions.w3c_actions.pointer_action.release()
# 執(zhí)行操作
actions.perform()
# 獲取【繼續(xù)】按鈕的 clickable 屬性值
result = self.driver.find_element(AppiumBy.ID, "cn.kmob.screenfingermovelock:id/btnTwo").get_attribute(
"clickable")
# 斷言【繼續(xù)按鈕】可點擊
assert result == "true"
總結
-
Actions 用法
-
滑動解鎖示例文章來源:http://www.zghlxwxcb.cn/news/detail-847065.html
獲取更多軟件測試技術資料/面試題解析,請點擊!文章來源地址http://www.zghlxwxcb.cn/news/detail-847065.html
到了這里,關于App自動化測試:高級控件交互技巧的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!