Appium 自己写了一个 selendroid 下 python 查找 toast 的函数

xdlhy · 2015年06月06日 · 最后由 朱光精 回复于 2017年11月05日 · 3403 次阅读

参考这个帖子http://testerhome.com/topics/2354 回帖中的 waitForElement,我自己写了一个 appium selendroid 模式下的 python 函数查找 toast
message:消息内容
timeout:超时时间
poll_frequency:查询步长
driver:就是 driver

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

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)))

共收到 43 条回复 时间 点赞

不错的~~

不能用

xdlhy #41 · 2015年06月19日 Author

#2 楼 @atm123 什么问题,我自己可以用呀

大神,我现在只是集成 UIAUTOMATOR,在调用 find_toast 时,driver 不知道怎么给值,driver 就是 driver???

#4 楼 @Dwyane 我写这个只能在 appium selendroid 模式下应用,driver 就是 类似 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 这个

@xdlhy
楼主 这个参数怎么传啊
我这样传报错啊 Find_Toast.find_toast(message, 2000, 1, driver)
提示错误 TypeError: find_toast() takes exactly 4 arguments (5 given)

xdlhy #37 · 2015年11月19日 Author

#6 楼 @nancy2896 这个错误意思应该是,要 4 个参数,你给传了 5 个,你是不是写错了

xdlhy #36 · 2015年11月19日 Author

#6 楼 @nancy2896 find_toast(u"登录成功",5,0.5,self.driver)
我平时就这么用的,没啥问题呀

#7 楼 @xdlhy 可是你也看到的 我是传的四个啊 RES.find_toast(u"无语音解析",5, 0.5, self._driver)

还是一样报错

xdlhy #34 · 2015年11月19日 Author

#9 楼 @nancy2896 你用的是 selendroid 模式么?奇了怪了。。。,导入的是 selenium 么?

xdlhy #11 · 2015年11月19日 Author

#9 楼 @nancy2896 你把你的 find_toast 函数也贴一下吧

我现在定义成这样了

@classmethod
    def find_toast(cls, message, timeout, poll_frequency, driver):
        element = WebDriverWait(driver, timeout, poll_frequency).until(method(Driver().get_driver()), expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))
        print element

不报那个错了,报了别的错
Error
Traceback (most recent call last):
File "E:\Users\Administrator\PycharmProjects\Shuangshi_AppiumDemo\Test\TestMyWork\TestMyWorkScripts.py", line 208, in test_19_Question_Check
RES.find_toast(u"无语音解析",5, 1, self.driver)
File "E:\Users\Administrator\PycharmProjects\Shuangshi_AppiumDemo\Test\TestMyWork\TestMyWorkScripts.py", line 69, 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 71, in until
value = method(self._driver)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 59, in __call
_
return _find_element(driver, self.locator)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 278, in _find_element
raise e
WebDriverException: Message: Invalid locator strategy: partial link text

xdlhy #31 · 2015年11月19日 Author

#12 楼 @nancy2896 method(Driver().get_driver() 这个你在函数里添加的是啥呀,我原先没写这个呀

从报的错看,是没有这种定位方式,你是不是用的不是 selendroid 模式?
desired_caps['automationName']='Selendroid'

要导入的那几个模块都导入了么?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

#13 楼 @xdlhy 那个是我自己加的,unti 不是传 2 个参数么?传 1 个也 ok?
那这个只能针对 selendroid 模式?我现在是 4.4 系统

xdlhy #15 · 2015年11月19日 Author

#14 楼 @nancy2896 只有 selendroid 模式能用,until 几个参数我忘了,我这样写肯定没问题
导入的包也都是 selenium

#15 楼 @xdlhy 恩 我知道了 谢谢楼主 改天我用 selendroid 模式试试

这个只对 web app 有用吧?原生应用由于没有 webdriver 对象不能用吧?

xdlhy #18 · 2015年12月24日 Author

#17 楼 @niuniudd 原生可以用的

#18 楼 @xdlhy 请问你是在 appium 下用的吗?
我是单独用的 selendroid,需要提前启动一个服务,服务的命令中需要带被测应用的 apk 绝对路径 (PC 上的路径),代码里需要生成一个 driver 对象,然后才能用 web 相关的方法。
这里原生应用你是怎么做的呢?

用了一下,不知道为什么总是提示找不到。
但是我如果设置提示和查找的 toast 内容都是"a",居然可以找到,很诡异。

xdlhy #21 · 2015年12月30日 Author

#19 楼 @niuniudd 是 appium 下的 selendroid,这些都是有的

#5 楼 @xdlhy 想问下怎么获取到 toast 的文本啊

xdlhy #21 · 2016年01月14日 Author

#22 楼 @neyo 没有看懂你的意思,你自己应该知道 toast 是什么吧

#23 楼 @xdlhy 。。因为实际脚本起来以后,超时了= =所以怀疑是不是 toast 的文本不匹配

我用的是 android selendroid mode。

不知道為什麼用 driver find element by partiial link text 找不到 元素呢!

我一年前也用過這個方法來查 toast 元素,也是用的 selendroid mode。

但是不知道為什麼現在不行了,會不會是新版本不支持了呢?

可否煩請告知你們用的 server+client 版本? 是不是最新的?

大家好,我的奇葩问题终于解决。

原因是,selendroid 版本 0.15 之后,就不支持(有 bug)find link text 来 check toast
而我之前用的 appium 版本,1.4.16, 底层是用的 selendroid 0.17..

所以我需要把自己的 appium 版本降级到旧版本,比如 1.4.8(底层引用 selendroid 0.15)才可以工作
https://github.com/selendroid/selendroid/issues/978

#27 楼 @chrisjywu 解决了我的问题,谢谢!

#28 楼 @neyo 你好,順便提提,最新的可以拿到 toast 的 appium 版本是 1.4.13 :)

#30 楼 @chrisjywu ok,我 memo 下!

xdlhy #12 · 2016年03月02日 Author

#30 楼 @chrisjywu 感谢你的提醒,我最近没来网站

return 什么呀,不太明白

@xdlhy
def find_toast(u'密码错误,请重新输入',5,1,self.driver):
element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT,message))) 你的这个用不了呢
def find_toast(u'密码错误,请重新输入',5,1,self.driver):
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]

用这个需要切换到 slendroin 吗?如果要,切换后对使用 appium 有什么影响?

因为想获取 toast 提示信息,尝试 以 Selendroid 模式启动 Appium 但是一直报错提示建立新 session 失败 如下


def setUp(self):

        desired_caps={}

        desired_caps['device'] = 'android'

        desired_caps['platformName']='Android'

        desired_caps['automationName']='Selendroid'

        desired_caps['browserName']=''

        desired_caps['noReset'] = True

        desired_caps['platformVersion']='4.3'

        desired_caps['deviceName']='SM-G7108V'

        desired_caps['app'] = PATH('C:\Program Files (x86)\Appium\golo.apk')

报错信息如下:

Traceback (most recent call last):
  File "C:\Users\Win-Attack\Desktop\test_get_toast.py", line 53, in setUp
    self.driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
  File "C:\Python27\lib\site-packages\appium\webdriver\webdriver.py", line 36, in __init__
    super(WebDriver, self).__init__(command_executor, desired_capabilities, browser_profile, proxy, keep_alive)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 90, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 177, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: A new session could not be created. (Original error: Required platform doesn't exist (API level >= 17))

求解~希望各位大神帮助~ 或者有没有其他方式获取到 toast 进行比对 多谢各位啦

樓上請重開貼啦,不要搞亂主題。謝謝

#35 楼 @makolm 我們所有的 android 測試都是基於 selendroid 模式,目前沒有發現什麼重大問題。

#38 楼 @chrisjywu 请问 toast message 是中文的情况下,如何才能定位到这个 toast?我这边,日志说,没有找到 elemnt,请问如何解决

代码是:

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("phone_number")
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()
a = find_toast(u"账号或密码错误",5, 1, driver)

日志信息是:

info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hu
b/session/e0229ff2-96e2-de1c-5e18-2f4f03502f36/element","method":"POST","json":{
"using":"partial link text","sessionId":"e0229ff2-96e2-de1c-5e18-2f4f03502f36","
value":"账号或密码错误"}}
info: [debug] Responding to client with error: {"status":7,"value":{"message":"A
n element could not be located on the page using the given search parameters.","
origValue":"Element was not found."},"sessionId":"e0229ff2-96e2-de1c-5e18-2f4f03
502f36"}

又又 [该话题已被删除] 中提及了此贴 06月30日 20:27

楼主,我用了你的方法,怎么提示下面的错误:
Error
Traceback (most recent call last):
File "D:\python_test\SIP\TestSuit_Backup.py", line 278, in test_c0005_cancel_backup
a = find_toast(u"Password can not be empty", 20, 0.5, self.driver)
File "D:\python_test\SIP\TestSuit_Backup.py", line 24, 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)
TimeoutException: Message:

急急急啊,在线等回复

运行结果 Message: A new session could not be created. (Original error: Waited 20 secs for selendroid server and it never showed up),怎么解?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册