【APPUIAuto】3.appium元素定位,隐式等待

desirecapability 介绍

  1. 客户端和服务端建立连接的json串,由客户端告诉服务器被测设备、被测包、被测页面信息
  2. 设置内容:app apk地址,appPackage包名,appActivity Activity名字, noReset、fullReset是否在登录前后重置相关环境
  3. skipDeviceInitialization 跳过设备初始化
  4. dontStopAppOnReset 不对应用不操作
    介绍文档
from appium import webdriver


desire_cap = {
  "platformName": "Android",
  "deviceName": "882QADTN22V59",
  "appPackage": "com.xueqiu.android",
  "appActivity": ".view.WelcomeActivityAlias",
  'noReset': True,
  'dontStopAppOnReset':True,# 首次启动的时候不停止APP
  'skipDeviceInitialization' :True # 跳过安装、权限设置操作

}
driver = webdriver.Remote('http://localhost:4723/wd/hub',desire_cap)
driver.implicitly_wait(10)
el3 = driver.find_element_by_id("com.xueqiu.android:id/home_search")
el3.click()
el4 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el4.send_keys("郑州")
el5 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.LinearLayout/android.widget.TextView[1]")
el5.click()
driver.back() # 返回上一级页面
driver.back()
driver.quit()

等待方式

强制等待sleep
隐式等待:implicitlyWait
等待某一个元素,显示等待webdriverwait(driver,xx).util

app控件定位

  • Android、ios基础知识
    1. 通过容器布局属性管理子控件的位置关系,布局过程就是把页面上的所有的控件根据他们间距大小,摆放在正确的位置
    2. Android 其他布局
      a). linearlayout,线性布局(横向、纵向都可以,用的最多的布局接口)
      b). relativeLayout,相对布局(确定一个布局的位置,根据第一个布局的位置,以及和第二个布局的间距来确定第二个布局的位置)
      c). FrameLayout 帧布局(用在布局最底层)
      d). AbsoluteLayout 绝对布局(用绝对坐标布局,用的较少)
      e). TableLayout 表格布局(把页面图片、文字以表格形式布局)
      f). GridLayout 网格布局
      g). ConstraintLayout 约束布局
    3. Android四大组件
      a). activity 与用户交互的可视化界面
      b). service 实现程序后台运行的解决方案(如音乐app后台播放)
      c). content provider 提供程序需要的数据
      d). broadcast receiver 监听外部时间的到来(比如来电)
      4.常用控件
      a). TextView 文本控件,EditText,可编辑文本控件
      b). Button 按钮,ImageButton图片按钮,ToggleButton 开关控件
      c). ImageView图片控件
      d). CheckBox 复选框控件,RadioButton单选框控件
  • dom结构解读(Document Object Model 文档对象模型)
    1. Android是定制的xml
    2. app source 类似于dom标识app的曾经,代表界面所有的控件树结构
    3. 每个控件都有他的属性(resourceid,xpath,aid)没有css
  • 定位方法
    1. id
    2. xpath
  • android定位工具 uiautomatorviewer
    android 8.0 以上需要
    adb shell uiautomator dump /sdcard/app.uix
    adb pull /sdcard/app.uix E:/app.uix

存到E盘的app.uix文件中,在E盘新建一个文本文档改成app.uix

adb shell screencap -p /sdcard/app.png
adb pull /sdcard/app.png E:/app.png

在E盘新建一个文本文档改成app.png)

再打开ui automator viewer导入文件夹,选择E盘,导入生成的两个文件即可;

  • 元素常用方法
    1. 点击 click()
    2. 输入操作 send_keys()
    3. 设置元素值set_value()
    4. 清除 clear()
    5. 是否可见 is_displayed(),返回TRUE FALSE
    6. 是否可用 is_enabled() ,返回TRUE FALSE
    7. 是否被选中 is_selected(),返回TRUE FALSE
    8. 获取属性值 getattribute(name)

weditor 定位元素

1.安装weditor:pip install weditor == 0.6.4
2.安装uiautomator2 :python -m uiautomator2 init(需要在手机上允许安装)
3.启动 :python -m weditor
4.在新开的浏览器窗口,输入 设备信息(adb devices 可获取到),cnnect连接,然后及时刷新页面即可


image.png

weditor 和appium 服务有冲突,运行appium服务时需要卸载掉 终端的 uiautomator
按照以下操作即可

1.找到终端关于 uiautomator的包:adb shell pm list package|findstr uiautomator
2.卸载包: adb uninstall com.github.uiautomator adb uninstall com.github.uiautomator.test
3.这样就可以使用appium服务了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容