Appium_第3堂课程笔记

  1 # APP定位元素问题:
  2 1、xpath在appium上提示不推荐使用绝对路径:
  3     但是可以使用相对路径去定位
  4 2、id元素去定位;APP元素定位80%用id,剩余的20%用xpath可以完成
  5 
  6 # 工具二:
  7 - 安卓官方提供 的 uiautomatorviewer
  8 - 可以升级:但是升级前需要备份:防止出错无法处理:类似写代码:上线有问题:本地备份了,方便代码回滚一个道理
  9 - uiautomatorviewer2
 10 - uiautomator升级版工具:升级xpath的表达式:但是无人维护,别人写的我们借用
 11 
 12 #工具三:
 13 -atx :无需下载安装;在终端下运行:weditor 元素定位辅助工具 ;模拟器自动生成atx
 14 - pip install weditor 
 15 缺点:运行weditor会和appium有冲突 
 16 所以在运行时需要操作atx 分开运行
 17 
 18 # 这是通过安卓原生的定位方式,我们需要写 java 语言, 没有提示
 19 # 坑:java 语言当中的字符串是使用 双引号, 不能使用单引号
 20 # appium 定位元素方式:
 21 - 优先使用 :id
 22 -xpath 方便获取 绝对路径的方式
 23 -Android_uiautomator 原生模式
 24 # 优势:原生的
 25 locator = 'new UiSelector().resourceId("com.lemon.lemonban:id/navigation_my").checkable(false)'
 26 driver.find_element_by_android_uiautomator(locator)
 27 - content-desc
 28 # 就通过描述 desc 去获取元素,因为极有可能没有
 29 driver.find_element_by_accessibility_id()
 30 # tagname 不行,
 31 # class_name , 可以,但是相当于原来的 tag_name, 不能精确定位
 32 
 33 -className 基本上不用
 34 
 35 # 使用weditor 时 unautomator 启动失败:
 36 # 查看uiautomator进程
 37 adb shell pm list package | findStr uiautomator # windows系统
 38 adb shell pm list package | grep uiautomator    # Mac系统
 39 
 40 # 单个删除程序uiautomator
 41 adb uninstall com.github.uiautomator
 42 adb uninstall com.github.uiautomator.test
 43 # 多个删除的方式:
 44 1、自己编写脚本:
 45 比如:安装手机上的证书;adb相关的命令 #执行脚本
 46 2、shell 三剑客
 47 Linux命令实现
 48 # 跳过某个步骤直接去其他也面:
 49 #  获取activity
 50 adb shell dumpsys activity | find "mResumedActivity"
 51 
 52 # 进入首页看到欢迎界面,欢迎界面需要滑动
 53 # driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
 54 # time.sleep(3)
 55 # driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
 56 # time.sleep(3)
 57 # 滑动的场景:
 58 swipe() 比如:滑动解锁;九宫格的的方式:某个app的欢迎页面:app的东哥应用程序页面滑动:比如系统界面上滑功能;下滑应用选择功能等
 59 
 60 # 不能使用绝对坐标,得使用百分比坐标
 61 # 获取屏幕的宽度和高度。800, 1000, 直接从x轴的90%,10%
 62 # 720px --> 80px , 纵坐标: 500 -》 500
 63 
 64 # d2.py
 65 from appium.webdriver.common.mobileby import MobileBy
 66 # 如果是 app 特有的方式,android_uiautomator
 67 driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector()..')
 68 # 关闭
 69 driver.quit()
 70 
 71 # d3.py
 72 # # 获取现在的页面源代码 // 主要是方便大家的时候你可以查找某些信息是否在源代码中显示
 73 # print(driver.page_source)
 74 #
 75 # # 获取包名
 76 # print(driver.current_package)
 77 # # 获取本页面 activity, 理解成 web 当中的 url
 78 # print(driver.current_activity)
 79 # # 获取现在的上下文环境, NATIVE_APP 原生app环境,对应的还会有 h5 环境
 80 # print(driver.current_context)
 81 
 82 time.sleep(5)
 83 
 84 
 85 # start_activity: 操作可以直接调到具体的页面
 86 # 新版activity 只需要从 .activity开始,
 87 driver.start_activity(app_package='com.lemon.lemonban', app_activity='.activity.LoginActActivity')
 88 
 89 time.sleep(3)
 90 
 91 # d4.py
 92 import time
 93 
 94 from appium.webdriver import Remote
 95 
 96 caps = {
 97     'platformName': 'Android',
 98     'deviceName': 'emulator-5554',
 99     'app': r'D:data柠檬班环境app测试环境应用apk包Future-release-2018.apk',
100     'noReset': False,
101 }
102 
103 
104 driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
105                 desired_capabilities=caps)
106 
107 # 等待
108 driver.implicitly_wait(10)
109 
110 time.sleep(5)
111 
112 # 进入首页看到欢迎界面,欢迎界面需要滑动
113 # driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
114 # time.sleep(3)
115 # driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
116 # time.sleep(3)
117 
118 # 不能使用绝对坐标,得使用百分比坐标
119 # 获取屏幕的宽度和高度。800, 1000, 直接从x轴的90%,10%
120 # 720px --> 80px , 纵坐标: 500 -》 500
121 
122 # 先获取屏幕的宽度和高度
123 size = driver.get_window_size()
124 height = size['height']
125 width = size['width']
126 
127 driver.swipe(start_x=width * 0.9,
128              start_y=height * 0.5,
129              end_x=width * 0.1,
130              end_y=height * 0.5)
131 
132 time.sleep(3)
133 
134 driver.swipe(start_x=width * 0.9,
135              start_y=height * 0.5,
136              end_x=width * 0.1,
137              end_y=height * 0.5)
138 
139 time.sleep(3)
140 
141 # d5.py 函数封装:类封装调用的方法--作业
142 def swipe_left(driver, offset=0.9):
143     """封装 swipe"""
144     size = driver.get_window_size()
145     height = size['height']
146     width = size['width']
147 
148     driver.swipe(start_x=width * offset,
149                  start_y=height * 0.5,
150                  end_x=width * (1-offset),
151                  end_y=height * 0.5)
152 
153 
154 def swipe_right(driver, offset=0.9):
155     """封装 swipe"""
156     size = driver.get_window_size()
157     height = size['height']
158     width = size['width']
159 
160     driver.swipe(start_x=width * (1-offset),
161                  start_y=height * 0.5,
162                  end_x=width * offset,
163                  end_y=height * 0.5)
164 
165 
166 def swipe_up(driver, offset=0.9):
167     size = driver.get_window_size()
168     height = size['height']
169     width = size['width']
170 
171     driver.swipe(start_x=width * 0.5,
172                  start_y=height * offset,
173                  end_x=width * 0.5,
174                  end_y=height * (1 - offset))
175 def swipe_down(driver, offset=0.9):
176     size = driver.get_window_size()
177     height = size['height']
178     width = size['width']
179 
180     driver.swipe(start_x=width * 0.5,
181                  start_y=height * offset,
182                  end_x=width * 0.5,
183                  end_y=height * (1 - offset))
184 
185 
186 
187 
188 import time
189 
190 from appium.webdriver import Remote
191 
192 caps = {
193     'platformName': 'Android',
194     'deviceName': 'emulator-5554',
195     'app': r'D:data柠檬班环境app测试环境应用apk包Future-release-2018.apk',
196     'noReset': False,
197 }
198 
199 
200 driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
201                 desired_capabilities=caps)
202 
203 # swipe(driver) # 滑动页面1次
204 # swipe(driver)# 滑动页面2次
205 # swipe(driver)# 滑动页面3次
206 # swipe(driver)# 滑动页面4次
207 # swipe(driver)# 滑动页面5次
原文地址:https://www.cnblogs.com/zhang-ping1205/p/14604372.html