appium定位元素

appium定位元素的方法

  1. 通过id定位元素
  2. 通过class_name定位元素
  3. 通过content-desc定位元素
  4. 通过text定位元素
  5. 通过xpath定位元素
  6. 通过uiautomator定位元素(android独有)

通过id定位元素find_element_by_id
元素的resource-id:优先使用id定位,有时候id并不是唯一的,要先判断一下,是否唯一。
resource-id = ‘com.bingfor.cs:id/coupon’
driver.find_element_by_id(id )

通过class_name定位元素find_element_by_class_name
clsaa属性决定了界面元素的类型
driver.find_element_by_class_name(‘android.widget.TextView’)

通过content-desc定位元素find_dlement_by_accessibility_id
content-desc属性是用来描述该元素的作用
如果要查询的界面元素的content-desc在当前界面唯一,则可以使用此方法。大部分情况下,该字段为空

通过text定位元素driver.find_element_by_android_uiautomator(“text(‘xxx’)”)
元素id相同时,可以根据text来进行定位
driver.find_element_by_android_uiautomator(“text(‘登录’)”)

通过xpath定位元素
xpath = "//[@resource-id='com.bigfor.cs:id/me]
driver.find_element_by_xpath(xpath)*

根据content-desc定位 driver.find_element_by_accessibility_id()

使用UIAutomator元素属性名称来定位

根据 resourceId 属性 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("%s")’)

根据 text 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().text("%s")’)
#对应uiautomator名称:“text”

根据 description 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().description("%s")’)
#对应uiautomator名称:“content-desc”

根据 className 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().className("%s")’)
#对应uiautomator名称:“class”

根据 index 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().index("%s")’)
#对应uiautomator名称:“index”

根据xpath定位 driver.find_element_by_xpath()
driver.find_element_by_xpath("//android.widget.TextView[@text=‘speaking’]")

获取页面信息

1、 def get_current_activity(cls, driver):获取当前页面的activity
2、 def get_current_pagesource(cls, driver):
3、 def get_contexts(cls, driver):

appium控件操作

  1. 输入:send_keys()
  2. 点击单击:click()
  3. 滑动 driver.swipe(x1, y1, x2, y2) x1 y1:起始位置; x2 y2:结束位置
    swipe还有个参数是滑动时间,duration
  4. 缩放 pinch() 默认缩小一半
  5. zoom() 默认放大一倍
  6. 长按long_press()
  7. 短按 press()
  8. 释放 release()
  9. 等待 wait()
  10. 取消cancel()
  11. 执行 perform()
    此类事件的操作跟selenium中webdriver一样,有个action类,在Appium中是TouchAction
    action = TouchAction(driver)
    action.long_press().wait(1000).perform() 默认单位是毫秒
  12. 卸载app remove_app()
  13. 关闭app app close_app()
  14. 隐式等待 app wait_activity() 需要等待的activity 超时时间 检测时间间隔三个参数
  15. get_screenshot_as_file() 截屏 保存图片路径

版权声明:本文为weixin_38330025原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。