问题描述:作为 Appium 初学者,想实现如下功能,

  1. app 登录界面,输入错误的 “用户名” 或 “密码”,点击登录
  2. Toast 提示 “账号或密码错误”
  3. 想捕获这个 Toast,并对其内容进行验证

参考文章:https://testerhome.com/topics/2715,写的简单脚本如下

现在的结果:
无法捕获到 Toast,提示 Element was not found

疑问:不知道到底是原理出了问题,还是因为 message 是 “中文” 的缘故,找不到元素

请各位帮忙,指导一下,在此谢过

===========================================================================================

**python   脚本代码如下**


 # -*- coding: utf-8 -*-  
from appium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.4'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.mpr.mprepubreader'
desired_caps['appActivity'] = '.activity.SplashActivity'
desired_caps['automationName']='Selendroid'
desired_caps['unicodeKeyboard']= True
desired_caps['resetKeyboard']= True
desired_caps['app'] = 'F:\\02_PhoneTesting\\Appium\\Killer-Android-release-test.apk'


driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


def find_toast(message, timeout, poll_frequency, driver):
      element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))
      return element

time.sleep(5)
driver.find_element_by_id("login_account").clear()
driver.find_element_by_id("login_account").send_keys("13168732222")
driver.find_element_by_id("login_password_edit").clear()
driver.find_element_by_id("login_password_edit").send_keys("1234567")
driver.find_element_by_id("login_btn_bg_btn").click()

find_toast(u"账号或密码错误", 5, 1, driver)

=============================================================================================

PyCharm 中,运行的结果是:

C:\Python27\python.exe F:/02_PhoneTesting/Appium/Scripts/Test1.py
Traceback (most recent call last):
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 34, in <module>
    find_toast(u"账号或密码错误", 5, 1, driver)
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 25, in find_toast
    element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1

===============================================================================================

**Appium Server的日志是:**

info: [debug] [SELENDROID ERR]  ... 22 more

info: --> POST /wd/hub/session/787653a7-dbdb-3d82-648d-bc3a1f508214/element {"using":"partial link text","sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214","val
ue":"账号或密码错误"}

info: [debug] Proxying command to localhost:8080

info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/787653a7-dbdb-3d82-648d-bc3a1f508214/element","method":"POST","json":{
"using":"partial link text","sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214","value":"账号或密码错误"}}

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.",
"origValue":"Element was not found."},"sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214"}


↙↙↙阅读原文可查看相关链接,并与作者交流