04_appium定位元素的方式APP测试实战一、定位元素方法

APP测试实战

一、定位元素方法

 
  1. #生成webdriver对象
  2. awd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
  3. #定位元素的方法同selenium

1、 根据 id 定位

  • awd.find_element( “id” , resourceId的值 )

    Android的resource-id对应ID定位方式

    注意:这个id也可能存在重复情况,可以通过index来获取需要的元素。

 
  1. #示例1: id 定位 元素
  2. #(1)定位 报表 点击
  3. awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_charts").click()
  4. time.sleep(2)
  5. #(2)定位 账本 点击
  6. awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_account").click()
  7. time.sleep(2)
  8. #(3)定位 【发现】元素 点击
  9. awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_discovery").click()
  10. time.sleep(2)
  11. #(4)定位 【设置】元素 点击
  12. awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_setting").click()
  13. time.sleep(2)

2、 根据 xpath定位

2.1绝对路径:

  • awd.find_element( ‘xpath’, ‘/hierarchy/类名1/类名2’)
 
  1. app绝对路径 :/hierarchy/类名1/类名2/类名2[1]
  2. web绝对路径: /html/body/标签
  3. #(3)定位 【发现】元素 点击
  4. awd.find_element("xpath","
  5. /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout[3]/android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout[3]/android.widget.LinearLayout").click()
  6. time.sleep(2)
  7. #点击记一笔的 xpath
  8. '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout[3]/android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.view.View'

2 相对路径 :

  • 使用属性定位元素

    • awd.find_element( ‘xpath’, ‘//*[@属性=”值“]’)

       
          
      1. 相对路径定位//后只可以接Android的class属性或*。
      2. 如: //android.widget.Button[@text="登 录"]
      3. //*[@text="登 录"]
    • 常用属性有:

      • tex属性 定位元素: //*[@text=’内容’]

      • resource-id属性 定位元素 : //*[@resource-id=’xx’]

      • class属性 定位元素 : //*[@classs=’类名’] #android 的类名重复多,当类名唯一的时候使用

        Android的class属性对应ClassName定位方式

        ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)

        find_element(“xpath”,” //*[@classs=’类名’]”) #只有一个指定类名 唯一

        find_elements(“xpath”,” //*[@classs=’类名’]”)[0] # 指定类名如果有多个

  • 从某个元素向下查找待定位的元素

     
      
    1. awd.find_element( 'xpath', '//*[@resource-id=”值“]/类名1/类名2/类名3[2]' )
    • 常见的类名:
      • android.widget.FrameLayout
      • android.widget.LinearLayout
      • android.support.v7.widget.RecyclerView
      • android.widget.TextView
      • android.widget.ImageView
      • android.widget.EditText
      • android.widget.Button
  • 从已定位的元素继续向下查找待定位的元素:

     
      
    1. 元素对象1=awd.find_element( 'xpath', '//*[@属性=”值“]/类名1/类名2/类名3' )
    2. 元素对象2 = 元素对象1.find_element('xpath',"//*[@text='XX']")
 
  1. '''
  2. app绝对路径 : //类名[@属性='xx']/类名 //*[@resoucer-id='xx']/类名/类名[1]
  3. web相对路径 ://标签[@属性="xx"]/标签
  4. '''
  5. #==============示例: 定位 【发现】元素 点击===================
  6. #-----(1)相对路径 text属性定位
  7. aWd.find_element('xpath',"//*[@text='发现']").click()
  8. #-----(2)相对路径 从某个元素向下查找待定位的元素
  9. awd.find_element('xpath',"//*[@resource-id='com.mobivans.onestrokecharge:id/bottomBar_ll_bottom']/android.widget.LinearLayout[3]").click()
  10. '''
  11. 【发现】元素的类名: android.widget.LinearLayout
  12. 【发现】元素的上一级元素 resource-id=com.mobivans.onestrokecharge:id/bottomBar_ll_bottom
  13. '''
  • 示例 : 使用 xpath 相对路径 定位 发现元素 如下图所示:

3 、根据 class_name定位

  • awd.find_elements(‘class name’, ‘//类名’)[索引号]
 
  1. android 的类名重复多,所以使用elements 结合索引号 定位
  2. 类似于 web 页面中的 tag name

4、 使用位置定位元素

 
  1. 如果id,xpath, class_name 都不能定位到元素, 那么使用位置(x,y坐标)定位元素
  • 点击元素 webdriver对象 . tap( [ (x,y) ] )
 
  1. 如何计算x,y的坐标值呢?
  2. 由于手机设备的屏幕大小不同,所以需要先获取屏幕实际尺寸,在屏幕范围内确定x,y的值
  3. 获取屏幕尺寸:
  4. width= webdriver对象.get_window_size()['width']
  5. height= webdriver对象.get_window_size()['height']
  6. #在屏幕区域内 点击
  7. awd.tap( [(width/2,heignt/2)])
  8. #当某个元素 本身点击不起作用时,使用 awd.tap 在屏幕某个位置点击
  9. el=awd.find_element("","")
  10. x=el.location().get("x")
  11. y=el.location().get("y")
  12. awd.tap( [(x+10,y+10)] )

5 、定位 Toast消息提示框

 
  1. Toast消息框 自动显示,过一会儿会自动消失,类似于web的alert提示框
  • awd.find_element(“xpath”,”//*[@class=’android.widget.Toast’]”)

6 、AccessibilityId属性定位

Android的content-desc属性对应AccessibilityId定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用

 
  1. find_element("accessibility id","21 三月 2022")

二、元素操作方法

  • 点击 el.click()

  • 清除 el.clear()

  • 输入 el.sendkeys(‘text’)

  • 获取元素的文本 el.text ——- android.widget.TextView

  • 获取元素的属性 el.getAttribute(‘checked’)

  • 获取元素左上角坐标 el.location el.location.get[“x”] el.location.get[“y”]

  • 获取元素的宽、高 el.size

     
      
    1. # 示例:"记一笔" 元素坐标位置和元素的高宽
    2. #定位 记一笔按钮
    3. jiyibi_button= self.awd.find_element("id","com.mobivans.onestrokecharge:id/main_write1")
    4. # "记一笔" 元素坐标位置
    5. print(jiyibi_button.location) #{'x': 320, 'y': 1174}
    6. print(jiyibi_button.location.get("x"),jiyibi_button.location.get("y"))
    7. # "记一笔" 元素高宽
    8. print(jiyibi_button.size)#{'height': 106, 'width': 80}
    9. print(jiyibi_button.size.get("height"),jiyibi_button.size.get("width"))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值