appium简明教程(10)——控件定位基础
2015-06-05 14:45
399 查看
转载自 http://www.cnblogs.com/nbkhic/p/3807871.html
狭义上讲,UI级的自动化测试就是让机器代替人去点来点去的过程。
但机器去点什么(点上面还是点左边),怎么点(是长按还是轻触),这些东西是必须由代码的编写者所指示清楚的。
控件定位就是解决机器点什么的问题的。
一般说来,我们可以这样告诉机器:去点登陆按钮。
机器很笨,它并不知道什么是登陆按钮。因为登陆按钮是自然语言的描述。
如果你让一个人去点登陆按钮,那么他其实也是要经过一系列的脑补以后才可以做这件事的。
这个脑补的过程还原如下:
这个一定是个按钮
这个按钮一定在被测的应用上
这个按钮大概上面有登陆这个文字信息
嗯,还真有一个,那么点吧。
这就是人探索性测试的一个简单过程。一般来说,如果你给出的信息不太充分,人类还是可以通过一系列的探索性思维去理解你的描述的。这个属于心理学的问题,不展开解释。
但是机器并不是人,如果你给出的描述不精确的话,机器是不会自发性的进行探索和脑补的。
因此控件定位就是精确的描述控件特征并告诉机器的过程。
本文版权归乙醇所有,欢迎转载,但请注明作者与出处,严禁用于任何商业用途
控件的特征就是控件的属性,我们可以通过上一讲中的uiautomatorviewer去获取。
在appium的定位世界里,下面这些方法是可以为我们使用的。也就是说,我们通过下面几个约定好的方式,按照webdriver和appium的DSL(自行搜索并理解)进行控件特征的描述和定位。
继承自webdriver的方法,也就是通过这3个特征可以定位控件
find by "class" (i.e., ui component type,andorid上可以是android.widget.TextView)
find by "xpath" (i.e., an abstract representation of a path to an element, with certain constraints,由于appium的xpath库不完备的原因,这个不太推荐)
find by "id"(android上是控件的resource id)
由Mobile JSON Wire Protocol 协议中定义的方法,更适合移动设备上的控件定位
在appium 的client对Mobile JSON Wire Protocol中定义的方法进行了封装,使其调用起来更加方便
当然了,你也可以使用原生的webdriver方法
另外,ruby lib里提供了一些非常好用的简便方法来进行控件的定位,好写,好读。
text(value_or_index) :Find the first TextView that contains value or by index. If int then the TextView at that index is returned.
button(value_or_index):Find the first button that contains value or by index. If int then the button at that index is returned
更多请看这里
总的来说就是在driver里增加了
find_element_by_accessibility_id
find_elements_by_accessibility_id
find_element_by_android_uiautomator
find_element_by_android_uiautomator
等方法
前面也讲过了,新增了这些方法
讨论:从上面可以看出来,python 和 java client对移动端控件定位的封装是比较初级的。ruby lib中封装了很多方便和简洁的方法,因此可以看出,使用ruby lib是优于python和java的选择。当然,如果忽略性能的话。
下一节我们开始具体看下如何用resource id去定位控件。
狭义上讲,UI级的自动化测试就是让机器代替人去点来点去的过程。
但机器去点什么(点上面还是点左边),怎么点(是长按还是轻触),这些东西是必须由代码的编写者所指示清楚的。
控件定位就是解决机器点什么的问题的。
一般说来,我们可以这样告诉机器:去点登陆按钮。
机器很笨,它并不知道什么是登陆按钮。因为登陆按钮是自然语言的描述。
如果你让一个人去点登陆按钮,那么他其实也是要经过一系列的脑补以后才可以做这件事的。
这个脑补的过程还原如下:
这个一定是个按钮
这个按钮一定在被测的应用上
这个按钮大概上面有登陆这个文字信息
嗯,还真有一个,那么点吧。
这就是人探索性测试的一个简单过程。一般来说,如果你给出的信息不太充分,人类还是可以通过一系列的探索性思维去理解你的描述的。这个属于心理学的问题,不展开解释。
但是机器并不是人,如果你给出的描述不精确的话,机器是不会自发性的进行探索和脑补的。
因此控件定位就是精确的描述控件特征并告诉机器的过程。
本文版权归乙醇所有,欢迎转载,但请注明作者与出处,严禁用于任何商业用途
控件的特征就是控件的属性,我们可以通过上一讲中的uiautomatorviewer去获取。
在appium的定位世界里,下面这些方法是可以为我们使用的。也就是说,我们通过下面几个约定好的方式,按照webdriver和appium的DSL(自行搜索并理解)进行控件特征的描述和定位。
继承自webdriver的方法,也就是通过这3个特征可以定位控件
find by "class" (i.e., ui component type,andorid上可以是android.widget.TextView)
find by "xpath" (i.e., an abstract representation of a path to an element, with certain constraints,由于appium的xpath库不完备的原因,这个不太推荐)
find by "id"(android上是控件的resource id)
由Mobile JSON Wire Protocol 协议中定义的方法,更适合移动设备上的控件定位
-ios uiautomation: a string corresponding to a recursive element search using the UIAutomation library (iOS-only)
-android uiautomator: a string corresponding to a recursive element search using the UiAutomator Api (Android-only)
accessibility id: a string corresponding to a recursive element search using the Id/Name that the native Accessibility options utilize.
在appium 的client对Mobile JSON Wire Protocol中定义的方法进行了封装,使其调用起来更加方便
ruby篇
find_element :accessibility_id, 'Animation' find_elements :accessibility_id, 'Animation' find_element :uiautomator, 'new UiSelector().clickable(true)' find_elements :uiautomator, 'new UiSelector().clickable(true)'
当然了,你也可以使用原生的webdriver方法
find_element id: 'resource_id'
另外,ruby lib里提供了一些非常好用的简便方法来进行控件的定位,好写,好读。
text(value_or_index) :Find the first TextView that contains value or by index. If int then the TextView at that index is returned.
button(value_or_index):Find the first button that contains value or by index. If int then the button at that index is returned
更多请看这里
python篇
el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")') self.assertIsNotNone(el) els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)') self.assertIsInstance(els, list) el = self.driver.find_element_by_accessibility_id('Animation') self.assertIsNotNone(el) els = self.driver.find_elements_by_accessibility_id('Animation') self.assertIsInstance(els, list)
总的来说就是在driver里增加了
find_element_by_accessibility_id
find_elements_by_accessibility_id
find_element_by_android_uiautomator
find_element_by_android_uiautomator
等方法
java篇
前面也讲过了,新增了这些方法findElementByAccessibilityId() findElementsByAccessibilityId() findElementByIosUIAutomation() findElementsByIosUIAutomation() findElementByAndroidUIAutomator() findElementsByAndroidUIAutomator()
讨论:从上面可以看出来,python 和 java client对移动端控件定位的封装是比较初级的。ruby lib中封装了很多方便和简洁的方法,因此可以看出,使用ruby lib是优于python和java的选择。当然,如果忽略性能的话。
下一节我们开始具体看下如何用resource id去定位控件。
相关文章推荐
- 中国气象网 气象数据开放平台 API使用方法 (Android)
- appium简明教程(9)——如何获取android app的Activity
- android 无法生成R文件的原因剖析
- 网页图表Highcharts实践教程之图表区
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- appium简明教程(8)——那些工具
- Unity5 如何做资源管理和增量更新
- Android中滑屏实现----手把手教你如何实现触摸滑屏以及Scroller类详解
- IOS控件大全及控件大小
- Android定位开发之百度定位、高德定位、腾讯定位,三足鼎立一起为我所用!
- iOS - 版面实现记录四
- android 常用方法集合
- Android Studio快捷键大全
- iOS 时间戳转换成标准时间
- IOS的各种控件(转载,防止遗忘)
- 关于Android中各种尺寸的总结
- Android批量图片加载经典系列——使用LruCache、AsyncTask缓存并异步加载图片
- Android Studio之版本管理工具Git (图文教程)附连接到Git@OSC教程
- Android 得到当前已连接的wifi的信号强度
- Android批量图片加载经典系列——使用LruCache、AsyncTask缓存并异步加载图片