时下最火的App自动化利器:Toast原理解析及操作实例,快速上手没阻碍!
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简介</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast 是一种轻量级的<span style="color: black;">信息</span>提示,常常以小弹框的形式<span style="color: black;">显现</span>,<span style="color: black;">通常</span><span style="color: black;">显现</span> 1 到 2 秒会自动消失,<span style="color: black;">能够</span>出<span style="color: black;">此刻</span>屏幕上中下任意位置。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast<span style="color: black;">拥有</span>如下的特点:</p><span style="color: black;">没</span>法被点击,<span style="color: black;">区别</span>于 Dialog,永远不会<span style="color: black;">得到</span>焦点。Toast <span style="color: black;">表示</span>的时间有限,Toast 会<span style="color: black;">按照</span>用户设置的<span style="color: black;">表示</span>时间后自动消失是系统级别的控件,属于系统 settings<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast 的思想:尽可能不引人<span style="color: black;">重视</span>,<span style="color: black;">同期</span>还向用户<span style="color: black;">表示</span>信息,<span style="color: black;">期盼</span>用户看到。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如下图,<span style="color: black;">便是</span>一种 Toast <span style="color: black;">信息</span>框类型:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q1.itc.cn/images01/20240320/35f988ce191c4d05b3b018215456799f.jpeg" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast 定位</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Appium 在抓取 Toast 时,<span style="color: black;">运用</span>的是 uiautomator 底层,<span style="color: black;">而后</span>将 Toast 元素放入控件树中。<span style="color: black;">必须</span><span style="color: black;">重视</span>的是,Toast 本身并不属于常规的用户界面控件,而是一种短暂的<span style="color: black;">信息</span>提示,在这个过程中,Appium <span style="color: black;">运用</span>的是 uiautomator2。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast <span style="color: black;">显现</span>的时间<span style="color: black;">通常</span>比较短,<span style="color: black;">能够</span><span style="color: black;">经过</span>等待<span style="color: black;">或</span>打印页面元素的方式判断<span style="color: black;">是不是</span>存在,并且<span style="color: black;">经过</span> Xapth <span style="color: black;">或</span> Accessibility ID 的定位方式找到 Toast 元素。在页面结构中,Toast 元素大致如下所示:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><!-- 在<span style="color: black;">这儿</span><span style="color: black;">能够</span>看到Toast<span style="color: black;">信息</span>提示框的class是android.widget.Toast,且一个页面<span style="color: black;">通常</span><span style="color: black;">仅有</span>一个 --></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><android.widget.Toast index="1" package="com.android.settings" class="android.widget.Toast" text="Clicked popup menu item Search" displayed="true" /></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、配置 driver 的初始化和关闭 driver 的<span style="color: black;">过程</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">class TestToast:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def setup_class(self):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">caps = {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">platformName: android,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">appium:appPackage: io.appium.android.apis,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">appium:appActivity: io.appium.android.apis.view.PopupMenu1,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"appium:noReset": True,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"appium:shouldTerminateApp": True</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.driver = webdriver.Remote("http://127.0.0.1:4723", options=UiAutomator2Options().load_capabilities(caps))</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.driver.implicitly_wait(15)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def teardown_class(self):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.driver.quit()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 定位 toast <span style="color: black;">信息</span>提示框</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def test_get_toast(self):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 定位到 Make a Popup! 按钮并点击</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Make a Popup!").click()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># <span style="color: black;">信息</span>框类型<span style="color: black;">选取</span>search</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.driver.find_element(AppiumBy.XPATH, "//*[@text=Search]").click()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 获取 Toast 弹框文本信息</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">result = self.driver.find_element(AppiumBy.XPATH, "//*").text</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">assert result == "Clicked popup menu item Search"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总结</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Toast <span style="color: black;">信息</span>提示框是一个<span style="color: black;">显现</span>时间短并且<span style="color: black;">没</span>法被点击,<span style="color: black;">通常</span><span style="color: black;">能够</span>用 Xpath <span style="color: black;">或</span> Accessibility ID 的方式去定位。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">举荐</span>学习</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q1.itc.cn/images01/20240320/7aa42baaf2d44154931d0c9bc07a281b.png" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python全栈<span style="color: black;">研发</span>与自动化测试<span style="color: black;">研发</span>高薪私教班,内容全面升级,可<span style="color: black;">选取</span>岗位<span style="color: black;">更加多</span>,就业面更广。4 个月 30+ 项目实战强化训练,资深测试架构师、开源项目作者亲授 BAT 大厂前沿最佳实践,0<span style="color: black;">基本</span>可学,带你一站式<span style="color: black;">把握</span>测试<span style="color: black;">研发</span><span style="color: black;">必须</span>核心技能!<span style="color: black;">增多</span>高薪私教服务,针对性<span style="color: black;">处理</span>工作<span style="color: black;">困难</span>,简历<span style="color: black;">指点</span>+模拟面试+面试复盘,助你斩获<span style="color: black;">更加多</span>Offer!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q7.itc.cn/q_70/images01/20240320/e03596813745450cac4cc71d5f49a939.png" style="width: 50%; margin-bottom: 20px;"><a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛:http://www.fok120.com/</span>,查看<span style="color: black;">更加多</span></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>
同意、说得对、没错、我也是这么想的等。 你的话语真是温暖如春,让我心生感激。 真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。 “BS”(鄙视的缩写)
页:
[1]