python + appium automated test element identification -06toast

Toast in Android is a simple message box. When the view displayed to the user, as displayed in floating applications. And Dialog is not the same, it never gets focus, can not be clicked.
Toast class ideology is as unobtrusive as possible, and also displays information to the user, want them to see. Toast displays and the limited time, usually about three seconds disappeared. Therefore, the use of traditional elements positioning tool, we are very difficult to locate Toast elements. So, how should we get to toast elements?

Toast content acquisition environment ready

Appium 1.6.3 Toast content began to support identification, mainly based UiAutomator2, the following parameters need to configure Capablity: desired_caps [ 'automationName'] = 'uiautomator2'
mounted appium-uiautomator2-driver: installation command follows cnpm install appium-uiautomator2-driver
installation selenium modules: pip install selenium

For example scenario

Enter the login screen enter the wrong user name or password, access to the contents of Toast:
"user name or password is incorrect, you can also try four times"
"Authentication failed too many times. Please try again in 15 minutes."

# coding=utf-8
from findElement.capability import driver
from selenium.webdriver.support.ui import WebDriverWait

driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear()
driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('username')

driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('password')
driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click()


error_message="用户名或密码错误,你还可以尝试4次"
#limit_message="验证失败次数过多,请15分钟后再试"

message='//*[@text=\'{}\']'.format(error_message)
# message='//*[@text=\'{}\']'.format(limit_message)

toast_element=WebDriverWait(driver,5,0.5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_24601279/article/details/104027643