日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Appium自動化測試中獲取Toast信息操作_python

作者:測試之路king ? 更新時間: 2022-04-20 編程語言

Toast簡介

Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。

Toast 定位

Appium 1.6.3開始支持識別Toast內容,主要是基于UiAutomator2,因此需要在Capablity配置參數

啟動參數配置

desired_caps['automationName']='uiautomator2'

環境

  • Appium-Python-Client: 2.1.2
  • selenium: 4.1.0
  • Appium:v1.20.2

測試應用

  • 網易云課堂

測試設備

  • 夜神模擬器 Android 7.1.2

測試場景

  • 進入登錄界面輸入用戶名和錯誤的密碼,獲取Toast內容

代碼實現

# _*_ coding:utf-8 _*_

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

desired_caps = {
? ? "platformName": "Android",
? ? "platformVersion": "7.1.2",
? ? "udid": "127.0.0.1:62001",
? ? "appPackage": "com.netease.edu.study",
? ? "appActivity": "com.netease.edu.study.activity.ActivityWelcome",
? ? "noReset": True,
? ? 'automationName': 'uiautomator2'
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
driver.implicitly_wait(30)

# 點擊我的菜單
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tab_account").click()

# 點擊登錄注冊按鈕
driver.find_element(AppiumBy.XPATH, "http://*[@text='登錄/注冊']").click()

# 點擊手機號碼登錄
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/login_phone_login").click()

# 輸入手機號碼
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_num").send_keys("132****475")

# 輸入錯誤密碼
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_pwd").send_keys("wy12345")

# 點擊登錄按鈕
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/button").click()

# 獲取toast提示
toast_text = driver.find_element(AppiumBy.XPATH, "http://*[@class=\"android.widget.Toast\"]").text
print(toast_text)

執行結果:

說明

toast 獲取主要使用一個通用的class屬性獲取,通過xpath的方式://*[@class="android.widget.Toast"]

toast信息存在是否存在判斷封裝

代碼

def is_toast_exist(driver,text,timeout=20,poll_frequency=0.5):
? ? '''is toast exist, return True or False
? ? :Agrs:
? ? ?- driver - 傳driver
? ? ?- text ? - 頁面上看到的文本內容
? ? ?- timeout - 最大超時時間,默認20s
? ? ?- poll_frequency ?- 間隔查詢時間,默認0.5s查詢一次
? ? :Usage:
? ? ?is_toast_exist(driver, "看到的內容")
? ? '''
? ? try:
? ? ? ? toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
? ? ? ? WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
? ? ? ? return True
? ? except:
? ? ? ? return False

toast信息內容獲取

代碼

def is_toast_exist(driver,timeout=20,poll_frequency=0.5):
? ? '''is toast exist, return toast_text or None
? ? :Agrs:
? ? ?- driver - 傳driver
? ? ?- timeout - 最大超時時間,默認20s
? ? ?- poll_frequency ?- 間隔查詢時間,默認0.5s查詢一次
? ? :Usage:
? ? ?is_toast_exist(driver)
? ? '''
? ? try:
? ? ? ? toast_loc = ("xpath", "http://*[@class=\"android.widget.Toast\"]")
? ? ? ? WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
? ? ? ? toast_text = driver.find_element(AppiumBy.XPATH, "http://*[@class=\"android.widget.Toast\"]").text
? ? ? ? return toast_text
? ? except:
? ? ? ? return None

原文鏈接:https://ceshizhilu.blog.csdn.net/article/details/122693435

欄目分類
最近更新