Tost元素识别

本文介绍如何在Appium中获取Android应用中的Toast消息,一种用于短暂显示信息的UI组件。从Appium 1.6.3版本开始,通过配置特定参数并利用UiAutomator2,可以实现对Toast内容的读取。文章详细解释了必要的环境设置和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢?

 

 

 

 

 

Toast简介

Android中的Toast是一种简易的消息提示框。 当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。

Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,一般3秒左右就消失了。因此使用传统的元素定位工具,我们是无法定位到Toast元素的(传说中低调奢华有内涵)。

Appium Toast内容获取

Add ability to verify TOAST messages (these can't be interacted with, only text retrieval allowed)

Appium 1.6.3开始支持识别Toast内容,主要是基于UiAutomator2,因此需要在Capablity配置如下参数:

desired_caps['automationName']='uiautomator2'

安装appium-uiautomator2-driver: 安装命令如下:

cnpm install appium-uiautomator2-driver

安装成功后可以在 C:\Users\XXXX\node_modules看到对应的文件:

  • _appium-uiautomator2-driver@1.12.0@appium-uiautomator2-driver
  • _appium-uiautomator2-server@1.10.0@appium-uiautomator2-server

 

 

 

安装selenium模块

pip install selenium

 

安装完成后使用如下命令检测是否安装成功

#查看selenium版本

C:\Users\Shuqing>pip show selenium

Name: selenium

Version: 3.11.0

Summary: Python bindings for Selenium

Home-page: https://github.com/SeleniumHQ/selenium/

Author: UNKNOWN

Author-email: UNKNOWN

License: Apache 2.0

Location: c:\python35\lib\site-packages

Requires:

Required-by: Appium-Python-Client

测试环境
  • jdk版本:"1.8.0_05"
  • appium版本:1.7.2 (不能低于1.6.3)
  • selenium:3.11.0
  • 测试设备:Android 5.1.1
  • Python:3.5
  • 测试App:考研帮Android app V3.1.0
测试场景

进入登录界面输入错误的用户名或者密码,获取Toast内容:

  • “用户名或密码错误,你还可以尝试4次”
  • “验证失败次数过多,请15分钟后再试”
代码实现

get_toast.py

# coding=utf-8

from find_element.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('zxss018')

 

driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('zxw2018')

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).until(lambda x:x.find_element_by_xpath(message))

print(toast_element.text)

 

 

注意:Toast内容为中文时,顶部必须注释# coding=utf-8 否则会因为编解码导致文字识别失败。

参考资料

https://testerhome.com/topics/6685

转载于:https://www.cnblogs.com/xuzhongtao/p/9723199.html

ST-DBSCAN算法是基于DBSCAN算法的扩展,用于空间数据中的聚类。它在DBSCAN的基础上增加了时间维度,使得可以识别和处理时空数据中的聚类。其算法流程如下: 1. 对于每一个数据点p,找到与其距离在ε内的所有点,并将这些点放入集合N(p)中。 2. 如果p是核心点,即N(p)中包含至少MinPts个点,则p成为一个种子点,将其放入当前聚类集合C中,并将N(p)中的所有点都加入C中。 3. 对于N(p)中的每个点q,如果q是一个核心点并且还没有被分配到任何一个聚类集合中,则将其加入C中,并且将N(q)中的所有点都加入C中。 4. 重复步骤2和3,直到C中的所有点都被分配到一个聚类集合中。 5. 对于下一个未被分配到任何聚类集合中的点,重复步骤2-4。 ST-DBSCAN算法可以简单地扩展为将时间维度考虑进去。具体地,我们可以将每个数据点p表示为(p.x, p.y, p.t),其中p.x和p.y表示空间坐标,p.t表示时间坐标。我们可以根据空间距离和时间距离来计算点之间的距离,进而进行聚类。 下面是ST-DBSCAN算法的Python实现代码: ```python from collections import defaultdict from typing import List, Tuple def st_dbscan(points: List[Tuple[float, float, float]], epsilon: float, min_pts: int, tau: float) -> List[List[Tuple[float, float, float]]]: """ ST-DBSCAN algorithm implementation. :param points: A list of points, where each point is a tuple of (x, y, t). :param epsilon: The maximum distance between two points to be considered as neighbors. :param min_pts: The minimum number of points required to form a dense region. :param tau: The maximum time difference between two points to be considered as neighbors. :return: A list of clusters, where each cluster is a list of points. """ clusters = [] visited = set() core_points = set() neighbor_points = defaultdict(set) for i, point1 in enumerate(points): if point1 in visited: continue visited.add(point1) neighbor_points[i] = set() neighbors = get_neighbors(points, i, epsilon, tau) if len(neighbors) >= min_pts: core_points.add(i) for j in neighbors: neighbor_points[i].add(j) neighbor_points[j].add(i) for i in core_points: if i not in visited: cluster = set() visited.add(i) cluster.add(points[i]) neighbors = neighbor_points[i] while neighbors: j = neighbors.pop() if j not in visited: visited.add(j) cluster.add(points[j]) if j in core_points: neighbors |= neighbor_points[j] clusters.append(list(cluster)) return clusters def get_neighbors(points: List[Tuple[float, float, float]], i: int, epsilon: float, tau: float) -> List[int]: """ Get all the neighbors of point i. :param points: A list of points, where each point is a tuple of (x, y, t). :param i: The index of the point. :param epsilon: The maximum distance between two points to be considered as neighbors. :param tau: The maximum time difference between two points to be considered as neighbors. :return: A list of indices of the neighbors. """ neighbors = [] for j, point2 in enumerate(points): if i == j: continue distance = ((point2[0] - points[i][0]) ** 2 + (point2[1] - points[i][1]) ** 2) ** 0.5 time_diff = abs(point2[2] - points[i][2]) if distance <= epsilon and time_diff <= tau: neighbors.append(j) return neighbors ``` 其中,输入参数points是一个由元组组成的列表,每个元组表示一个点的坐标和时间;epsilon和min_pts分别是DBSCAN算法中的两个参数;tau表示时间距离的阈值。输出是一个列表,其中每个元素都是一个列表,表示一个聚类集合中的所有点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值