app定位元素
工具一:
=================================================================
工具二:
- 安卓官方提供的 uiautomatorviewer
- C:\at_will\android-sdk-windows\tools\bin\uiautomatorviewer.bat
=======================================================================
工具三:
- atx : weditor 元素定位辅助工具
- pip install weditor
weditor: 优势在xpath, 但是与appium冲突,两者不能同时打开
使用 weditor 时 uiautomator 启动失败:
- adb shell pm list package | findStr uiautomator
卸载uiautomator 服务:
- adb uninstall com.github.uiautomator
- adb uninstall com.github.uiautomator.test
- adb uninstall com.io.appium.uiautomator2.server
- adb uninstall com.io.appium.uiautomator2.server.test
==========================================================================
查找元素
总结元素定位方式:
- 优先使用 id
- xpath 方便获取
- android_uiautomator 原生模式
- content-desc
- className 基本上不用
driver.find_element('id',)
driver.find_element('xpath',)
# 这是通过安卓原生的定位方式,我们需要写 java 语言, 没有提示
# 坑:java 语言当中的字符串是使用 双引号, 不能使用单引号
# 优势:原生的
# locator = 'new UiSelector().resourceId("com.lemon.lemonban:id/navigation_my")
# 链式调用 new UiSelector().resourceId(元素).checkable(元素)
locator = 'new UiSelector().resourceId("com.lemon.lemonban:id/navigation_my").checkable(false)'
driver.find_element_by_android_uiautomator(locator)
# 就通过描述 desc 去获取元素,因为极有可能没有
driver.find_element_by_accessibility_id()
# tagname 不行,
# class_name , 可以,但是相当于原来的 tag_name, 不能精确定位
=============================================================
adb shell dumpsys activity | find "mResumedActivity"
# 找元素
driver.find_element(By.ID, 'com.lemon.lemonban:id/navigation_my').click()
# 如果是 app 特有的方式,android_uiautomator
driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector()..')
=========================================================================
import time
from appium.webdriver import Remote
# 得到一个driver
# 1、TODO: adb devices 确定手机在线
# 2、启动 appium 服务
# 如果appium启动了 4444 端口,就可以直接访问:因为Remote 设置了默认参数。
# Remote 提供的参数端口号和 appium 服务端口号保持一致
# 平台, 你要链接哪个手机, 操作哪个app
# 三个变量 key 不能变的。
# caps = {
# 'platformName': 'Android',
# 'deviceName': 'emulator-5554',
# 'app': r'D:\data\柠檬班环境\app测试环境\应用apk包\lemon_app_webview_debug.apk'
# }
#
# driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
# desired_capabilities=caps)
# 可以不提供 app 路径,而是提供 app 的包名 和 activity
# 包名:
# activity: app当中的哪个页面。
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.common.by import By
caps = {
'platformName': 'Android',
# 'platformVersion': '8.0', # 校验系统版本
# 'automationName': 'Uiautomator2',
'deviceName': 'emulator-5554',
'appPackage': 'com.lemon.lemonban',
'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity',
# 'app': r'D:\data\柠檬班环境\app测试环境\应用apk包\Future-release-2018.apk',
# 会使用缓存数据
# 'noReset': False,
# 'chromedriverExecutable': r'd:\'
# - unicodeKeyboard: True
# - resetKeyboard: True
# autoGrantPermissions: True
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=caps)
# 等待
driver.implicitly_wait(10)
# 找元素
# driver.find_element(By.ID, 'com.lemon.lemonban:id/navigation_my').click()
# # 如果是 app 特有的方式,android_uiautomator
# # driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector()..')
#
# # 获取现在的页面源代码 // 主要是方便大家的时候你可以查找某些信息是否在源代码中显示
# print(driver.page_source)
# # 获取包名
# print(driver.current_package)
# # 获取本页面 activity, 理解成 web 当中的 url
# print(driver.current_activity)
# # 获取现在的上下文环境, NATIVE_APP 原生app环境,对应的还会有 h5 环境
# print(driver.current_context)
time.sleep(5)
# start_activity: 操作可以直接调到具体的页面
# 新版activity 只需要从 .activity开始,
driver.start_activity(app_package='com.lemon.lemonban', app_activity='.activity.LoginActActivity')
time.sleep(3)
================================================================
swipe 滑动
import time
from appium.webdriver import Remote
caps = {
'platformName': 'Android',
'deviceName': 'emulator-5554',
'app': r'D:\data\柠檬班环境\app测试环境\应用apk包\Future-release-2018.apk',
'noReset': False,
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=caps)
# 等待
driver.implicitly_wait(10)
time.sleep(5)
# 进入首页看到欢迎界面,欢迎界面需要滑动
# driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
# time.sleep(3)
# driver.swipe(start_x=800, end_x=0, start_y=200, end_y=200)
# time.sleep(3)
# 不能使用绝对坐标,得使用百分比坐标
# 获取屏幕的宽度和高度。800, 1000, 直接从x轴的90%,10%
# 720px --> 80px , 纵坐标: 500 -》 500
# 先获取屏幕的宽度和高度
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x=width * 0.9,
start_y=height * 0.5,
end_x=width * 0.1,
end_y=height * 0.5)
time.sleep(3)
driver.swipe(start_x=width * 0.9,
start_y=height * 0.5,
end_x=width * 0.1,
end_y=height * 0.5)
time.sleep(3)
=============================================================
函数封装 swipe 滑动
def swipe_left(driver, offset=0.9):
"""封装 swipe"""
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x=width * offset,
start_y=height * 0.5,
end_x=width * (1-offset),
end_y=height * 0.5)
def swipe_right(driver, offset=0.9):
"""封装 swipe"""
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x=width * (1-offset),
start_y=height * 0.5,
end_x=width * offset,
end_y=height * 0.5)
def swipe_up(driver, offset=0.9):
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x=width * 0.5,
start_y=height * offset,
end_x=width * 0.5,
end_y=height * (1 - offset))
import time
from appium.webdriver import Remote
caps = {
'platformName': 'Android',
'deviceName': 'emulator-5554',
'app': r'D:\data\柠檬班环境\app测试环境\应用apk包\Future-release-2018.apk',
'noReset': False,
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=caps)
# swipe(driver)
# swipe(driver)
# swipe(driver)
# swipe(driver)
# swipe(driver)
====================================================
类封装 swipe 滑动
class BaseOpera:
def swipe_to_left(self,driver, base=0.1):
'''从右向左滑动'''
self.driver = driver
self.size = self.driver.get_window_size()
self.height = self.size["height"]
self.width = self.size["width"]
return self.driver.swipe(self.width*(1-base),
self.height*0.5,
self.width*base,
self.height*0.5
)
def swipe_to_right(self,driver, base=0.1):
'''从左向右滑动'''
self.driver = driver
self.size = self.driver.get_window_size()
self.height = self.size["height"]
self.width = self.size["width"]
return self.driver.swipe(self.width*base,
self.height*0.5,
self.width*(1-base),
self.height*0.5
)
def swipe_to_top(self,driver,base=0.9):
'''从下向上滑动'''
self.driver = driver
self.size = self.driver.get_window_size()
self.height = self.size["height"]
self.width = self.size["width"]
return self.driver.swipe(self.width * 0.5,
self.height * base,
self.width * 0.5,
self.height * (1 - base)
)
def swipe_to_bottom(self,driver, base=0.9):
'''从上向下滑动'''
self.driver = driver
self.size = self.driver.get_window_size()
self.height = self.size["height"]
self.width = self.size["width"]
return self.driver.swipe(self.width*0.5,
self.height*(1-base),
self.width*0.5,
self.height*base
)