js 特效

js 特效

本文为霍格沃茨考院学生学习笔记,进阶学习在文末补充。

本系列文章总结了Appium自动化测试从基础到框架(如下图)的高级实战所涉及的知识点精华,希望能帮助你快速总结和复习。

Appium 自动化测试从基础到框架实战Appium 基础 1 (环境搭建和简介)Appium 基础 2 (元素定位和元素常用方法)Appium 基础 3 (手势操作和 uiautomator 查找元素)Appium 基础 4 (显式等待)Appium 基础 5 (toast 和参数化)Appium 基础 6 (webview)Appium_ 企业微信练习 (非 PO,增加和删除联系人)Appium_ 企业微信练习 ( PO–增加联系人)

本文为第二篇,主要讲解Appium手势操作、寻找元素、显示等待(附示例代码)。

Appium 的触屏操作滑动小案例进入雪球应用再主页从下往上滑动避免使用坐标(代码用获取屏幕的长宽来解决这个问题)代码

fromtimeimportsleepfromappiumimportwebdriverpromappium . web driver . common . touch _ actionimportTouchActionclassTestFind():def setup(self):self . desire _ cap = { & # 34;平台名称& # 34;:"安卓& # 34;,"设备名称& # 34;:"127.0.0.1:7555","appPackage & # 34:"com . xueqiu . Android & # 34;,"appActivity & # 34:". view.WelcomeActivityAlias & # 34,"noReset & # 34:"真& # 34;,"unicode键盘& # 34;:True}self.driver=webdriver。远程(& # 34;http://127 . 0 . 0 . 1:4723/wd/hub & # 34;,self . desire _ cap)self . driver . implicitly _ wait(5)def test _ search(self):& # 34;""1.进入雪球应用。2.从下到上滑动主页。3.避免使用坐标:return:& # 34;""#因为雪球真的太慢了,睡10秒(10) #定义一个TouchAcion对象,aciton = touchaction (self。driver) #获取整个屏幕右下角的坐标,window _ rect = self。司机。Get _ window _ rect () #提取屏幕的最大宽度= window _ rect[& # 39]width & # 34;] #提取屏幕的最大高度height = window _ rect[& # 39;身高& # 39;]#x的坐标定义为最大宽度的一半,即圆心的x坐标x1=int(width/2)#定义了起点的y坐标,y _ start = int (height * 4/5) #定义了终点的y坐标,以及顶端1/5的位置,这样,就可以模拟从下往上滑动的动作,y_end=int(height*1/5)#先按一下初始坐标, 然后按住2秒再移动_到最终坐标,然后释放()释放坐标点,用perform()执行一系列动作操作,aciton.press(x=x1,Y = y _ start)。 等等(2000)。move _ to (x = x1,y = y _ end)。释放()。执行()#重复两次,效果更明显。Y = y _ end)。释放()。执行()操作。按(x = x1,y = y _ start)。等等(2000)。Move _ to (x = x1,y = y _ end)。释放()。执行()睡眠(3。百度曾经进入解锁页面,设置解锁密码为一个7字。我偶然发现Appium可以指定不同的初始活动。似乎也依赖于来自timeimportsleepfromapbimportwebdriverfrommapium的应用程序代码。web driver . common . mobilebyimportmobilebyayfrommapium . web driver . common . touch _ action imp。ortTouchActionclassTestFind():def setup(self):self . desire _ cap = { & # 34;平台名称& # 34;:"安卓& # 34;,"设备名称& # 34;:"127.0.0.1:7555","appPackage & # 34:"cn . kmob . screenfingermovelock & # 34;,"appActivity & # 34:"com . Samsung . ui . main activity & # 34;,"noReset & # 34:"真& # 34;}self.driver=webdriver。远程(& # 34;http://127 . 0 . 0 . 1:4723/wd/hub & # 34;,self . desire _ cap)self . driver . implicitly _ wait(5)def test _ search(self):& # 34;""1.进入解锁页面。2.将解锁密码设置为7个字的密码。3.偶然发现appium可以指定不同的初始活动,这似乎取决于应用:return:& # 34;""Sleep(3)#定义一个TouchAcion对象Aciton = TouchAction (self。驱动)#找到七个坐标点,最后通过连续按下,wait,move_to,最后释放手势release(),然后perform()执行aciton.press(x=142,y=190)。等等(200)。move_to(x=408,y=190)。等等(200)。move_to(x=678,y=190)。等等(200)。move_to(x=678,y=464)\。等等(200)。move_to(x=678,Y = 740)。释放()。perform()sleep(2)ui automator查找元素的优缺点。优缺点:xpath定位速度慢。uiautomator是Android的工作引擎,方便快速滚动查找。缺点:复杂的表达式编写,容易出错的IDE不提示定位。通过资源标识定位、通过类名定位、通过内容desc定位、通过文本组合定位和通过父子关系使用驱动定位。find _ element _ by _ Android _ ui automator(& # 34;表情& # 34;注意:外层要用单引号,内层字符串要用双引号,因为本来就是Ja,Ja中的双引号只是表示字符串是通过text,new UiSelector()定位的。文本(& # 34;Text text & # 34)模糊匹配newuiselector()。text contains(& # 34;Text text & # 34)匹配newuiselector()。text start with(& # 34;Text text & # 34)正则表达式匹配newuiselector()。文本匹配(& # 34;Text text & # 34)组合定位比如id和text的属性组合:driver。find _ element _ by _ Android _ ui automator(& # 39;新UiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ account & # 34;).文本(& # 34;我的& # 34;)')父子关系定位:childSelector,先定位父类,再用childSelector定位子类driver . find _ element _ by _ Android _ ui automator(& # 39;新UiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ account & # 34;).child selector(text(& # 34;股票& # 34;))')兄弟定位:from parent driver . find _ element _ by _ Android _ ui automator(& # 39;新UiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ account & # 34;).from parent(text(& # 34;股票& # 34;))')代码from timeimportsleep frommappiumimportwebdriverfrommapium.webdriver.com mon . touch _ actionporttouchaction类find():def setup(self):self . desire _ cap = { & # 34;平台名称& # 34;:"安卓& # 34;,"设备名称& # 34;:"127.0.0.1:7555","appPackage & # 34:"com . xueqiu . Android & # 34;,"appActivity & # 34:". view.WelcomeActivityAlias & # 34,"noReset & # 34:"真& # 34;,"unicode键盘& # 34;:True}self.driver=webdriver。远程(& # 34;http://127 . 0 . 0 . 1:4723/wd/hub & # 34;,self . desire _ cap)self . driver . implicitly _ wait(5)def test _ search(self):& # 34;""1.打开雪球app2。点击我的进入个人信息第3页。点击登录进入登录页面4。输入用户名和密码5。点击登录6。弹出手机号输入失败的提示,断言此提示是否正确:return:& # 34;""#雪球太慢了,只有10秒。我懒得用显示器等着睡觉(10)#在首页找到我的元素,然后点自己。司机。find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;我的& # 34;)').点击()# 2秒睡眠失败会导致下一页的元素刷新过快,无法识别睡眠的元素(2)#识别账号密码登录,然后点击自我。driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;帐户密码登录& # 34;)').click()#睡眠2秒失败会导致下一页的元素刷新过快,无法识别睡眠(2)#输入的帐户名是tongself。司机。find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ account & # 34;)').send _ keys(& # 34;童童& # 34;)#输入密码为tongdong self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ password & # 34;)').send _ keys(& # 34;童童& # 34;)#如果2秒不睡眠,下一页的元素刷新太快,你就认不出睡眠了(2)#点击登录按钮self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;登录& # 34;)').click()#睡眠2秒失败导致下一页元素刷新过快无法识别睡眠(2)#查找有确定元素的错误提示框log in _ incorrect = self . driver . Find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;好& # 34;)')#当某些元素可见时,表示登录失败,用例passassertlogin _ incorrect。is _ displayed()滑动元素搜索有些页面有连续滑动的能力,比如微博,没有分页,可以一直滑动。UIAutomator提供了一个很好的滑动方式。司机。find _ element _ by _ Android _ ui automator(‘ new ui scrollable(new ui selector(). ‘可滚动(true)。实例(0))。scrollintoview(新的ui选择器()。文本包含(“患者”)。’实例(0)。).click()注:虚拟机和真机不一样,有时候真机的滑动是可以的。有时候虚拟机的不ok代码是from timeimportsleep frommapbimportwebdriverfrommapium.webdriver.com mon . touch _ actionporttouchclasstestfind():def setup(self):self . desire _ cap = { & # 34;平台名称& # 34;:"安卓& # 34;,"设备名称& # 34;:"127.0.0.1:7555","appPackage & # 34:"com . xueqiu . Android & # 34;,"appActivity & # 34:". view.WelcomeActivityAlias & # 34,"noReset & # 34:"真& # 34;,"unicode键盘& # 34;:True}self.driver=webdriver。远程(& # 34;http://127 . 0 . 0 . 1:4723/wd/hub & # 34;,self . desire _ cap)self . driver . implicitly _ wait(5)def test _ search(self):& # 34;""0.你的雪球app先关注一个人,然后向下滑动,找到一个关键词,用textContains进行模糊匹配。1.打开雪球app2。点击注意使屏幕向下滑动,直到找到患者的模糊匹配文本元素,然后点击:return:& # 34;""#雪球太慢,只有10秒,懒得用显示器等等。睡眠(10)#点击关注元素self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;注意& # 34;)').点击()#睡眠4秒,怕跳转页面太快,搜索元素睡眠(4)自我。司机。find _ element _ by _ Android _ ui automator(& # 39;newUiScrollable(newUiSelector()。''可滚动(true)。实例(0))。''scrollIntoView(newUiSelector()。text contains(& # 34;病人& # 34;).''实例(0));').点击()睡眠(4)强制等待、隐式等待、显示等待的特征。强制等待就是睡眠,强烈不建议,设定的时间太固定。如果模拟器等3秒,真机可能只需要等2秒。driver.implicitly.wat(timeout),它贯穿所有元素,只需要设置一次,通常是在创建驱动程序之后。显示等待是客户端的等待:参考selenium的两个包和一个例子。从selenium导入web driver wait。web driver . support import expected _ conditions web driver wait。(自我。司机,10)。直到(预期_条件。element _ to _ be _ clickable(locator))介绍显示等待。显示等待与隐式等待相反。必须在每个要等待的元素前声明显示等待。它是为特定元素设置等待时间。在设定的时间内,默认情况下,Meg每隔一段时间检查一次某个元素当前是否存在。如果在指定的时间段内找到一个元素,就直接执行,即找到就执行相关操作。如果超过设定时间未检测到,则会抛出异常,默认检测频率为0.5s抛出异常时默认使用两个常用类webdriverwetexpected _ condition。为什么要使用显示等待?为什么隐式等待不能代替显示等待?要显示可以等待动态加载的AJax元素,需要配合expected_condition检查一般页面上元素的呈现顺序是title第一;然后出现了dom树,存在不完整,dom树看起来在隐式等待,但是此时的元素可能是不可点击的,只使用click方法肯定会出错;CSS出现:可视;可见;JS的出现,JS特效的实现:点击可点击;;HTML文档是自上而下加载的,JS文件的加载会阻塞HTML内容的加载。有些JS异步加载方法会完成JS的加载。下载后,样式表将与之前的样式表一起解析,之前的and元素将重新呈现。存在-可见-可点击可点击,元素出现-可见-可点击,这是元素的三个属性。当DOM树出现时,可能已经显示了定位元素,但是visible和clickable属性可能还没有加载。此时,元素的一些方法,比如element.click(),在JS渲染之前是不可用的。只有元素的click属性才能与element对应的synchronous loading . is _ displayed()对应的element.is_selected()对应的element.is_enabled()JS同步加载。同步模式又称阻塞模式,会阻止浏览器的后续处理,停止后续解析,从而停止后续的文件加载(如图片)、渲染和代码执行。异步加载:异步加载也称为非阻塞加载。在下载和执行JS的同时,浏览器会继续处理后续页面。WebDriverWait用法webdriverwait (self,driver,timeout,poll _ frequency = poll _ frequency,ignored _ exceptions = none)驱动程序:浏览器驱动的超时:timeout,Poll _ frequency per second:check interval step,默认值为0.5signored_exceptions:超时后抛出的最后一个异常,默认值为NoSuchElementException。通常我们只使用driver和timeoutwebdriverwait()。unti (self,method,message = & # 39'方法of)或until_not():方法:在等待期间,每隔一段时间(init中的poll_frequency)调用此传入方法,直到返回值不为Falsemessage:如果超时,抛出TimeoutException。把消息传入异常,直到not,意思是当一个元素很小或者在什么条件下,会继续执行,参数是一样的。expected_conditions类Appium直接为我们封装了类,只需要传递参数。例如,我们使用click()。只需要判断这个元素是否可以点击属性,然后再继续点击usage: expected _ conditions。element _ to _ be _ clickable(locator),其中locator为:(By。ID,& # 34;com . xueqiu . Android:id/TV _ search & # 34;常用的如下:expected _ conditions。element _ to _ be _ clickable:元素是否可以被点击。presence _ of _ element _ located:元素是否被添加到Dom树中。type _ of _ element _ located:元素是否可见。获取元素#获取元素element = webdriverwait (self。司机,10)。直到(Lambda: x.find _ element (by.xpath,& # 39;//*[@ text = & # 34;我的& # 34;]'))#在这里找到元素后,不要等待。实测证明其已通过element.click()示例代码来自frommapbimportwebdriverFromselenium。webdriver。supportimportexpected _ conditions from selenium。webdriver。支持。waitimportwebdriver waitfromappium . we . b driver . common . mobilebyimportmobilebyayclasstestfind():def setup(self):self . desire _ cap = { & # 34;平台名称& # 34;:"安卓& # 34;,"设备名称& # 34;:"127.0.0.1:7555","appPackage & # 34:"com . xueqiu . Android & # 34;,"appActivity & # 34:". view.WelcomeActivityAlias & # 34,"noReset & # 34:"真& # 34;,"unicode键盘& # 34;:True}self.driver=webdriver。远程(& # 34;http://127 . 0 . 0 . 1:4723/wd/hub & # 34;,self . desire _ cap)self . driver . implicitly _ wait(5)def test _ search(self):& # 34;""1.打开雪球app2。点击我的进入个人信息第3页。点击登录进入登录页面4。输入用户名和密码5。点击登录6。弹出手机号输入失败的提示,断言此提示是否正确:return:& # 34;""#By.name方法与文本不对应,所以千万不要使用# self . driver . find _ element(by . name,& # 34;我的& # 34;)# Wait by display,element _ to _ be _ clickable(locator),里面的定位器要记得用祖先webdriverwait (self。司机,15)。直到(预期_条件。element _ to _ be _ clickable ((by。XPath,& # 39;//*[@ text = & # 34;我的& # 34;]'))#在首页查找我的元素,然后点击self . driver . Find _ element(by . XPath,& # 39;//*[@ text = & # 34;我的& # 34;]').单击()& # 39;''Lambda返回元素element = web driver wait(self . driver,10)。直到(lambda: x.find _ element (by.xpath,& # 39;//*[@ text = & # 34;我的& # 34;]'))在这里找到元素后,不要等了,测出来的结果证明已经通过元素了. click()& # 39;''#显示元素webdriverwait (self。司机,15)。直到(预期_条件。element _ to _ be _ clickable((by . XPath,& # 39;//*[@ text = & # 34;帐户密码登录& # 34;]'))#识别账号密码登录的元素,然后点击self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;帐户密码登录& # 34;)').click()#显示元素webdriverwait (self。司机,15)。直到(预期_条件。element _ to _ be _ clickable((by . XPath,& # 39;//*[@ resource-id = & # 34;com . xueqiu . Android:id/log in _ account & # 34;]'))#输入账户名为tong self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ account & # 34;)').send _ keys(& # 34;童童& # 34;)#输入密码为tongdong self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。resourceId(& # 34;com . xueqiu . Android:id/log in _ password & # 34;)').send _ keys(& # 34;童童& # 34;)#显示按钮webdriverwait (self。司机,15)。直到(预期_条件。element _ to _ be _ clickable((by . XPath,& # 39;//*[@ text = & # 34;登录& # 34;]'))#点击登录按钮self . driver . find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;登录& # 34;)').click()#显示元素webdriverwait (self。司机,15)。直到(预期_条件。element _ to _ be _ clickable((by . XPath,& # 39;//*[@ text = & # 34;好& # 34;]'))#找到有确定元素login _ incorrect = self的错误提示框。司机。find _ element _ by _ Android _ ui automator(& # 39;newUiSelector()。文本(& # 34;好& # 34;)')#当某个元素可见时,表示登录失败。有关用例passassertlogin _ incorrect的更多信息。is _ displayed(),我们会在后续文章中分享。

(文章来自霍格沃茨考试学院)

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

发表回复

登录后才能评论