Python 官方文档:入门教程 => 点击学习
一、环境搭建及准备工作 Appium 2 环境搭建 请参考另一篇文章: Windows系统搭建Appium 2 和 Appium Inspector 环境 安装 Appium-python-Client,版本要求3.0及以上 和 selen
Appium 2 环境搭建
安装 Appium-python-Client,版本要求3.0及以上 和 selenium 版本要求4.0及以上
pip install Appium-Python-ClientVersion: 3.1.0pip install seleniumVersion: 4.15.2
手机连接电脑,并在dos窗口启动 Appium Server
演示环境APP软件:ES文件浏览器、随手记
查看元素唯一方法
from appium import WEBdriverfrom appium.options.common.base import AppiumOptionsfrom appium.webdriver.common.appiumby import AppiumBydef create_driver(): """ AppiumOptions(): 用于配置 Appium 测试的通用选项,可用于 Android 和 iOS 平台 可以设置通用的测试选项,如平台名称、版本、自动化引擎等 """ # 创建 AppiumOptions 对象 options = AppiumOptions() # 加载测试的配置选项和参数(Capabilities配置) options.load_capabilities({ # 自动化测试的引擎 "automationName": "uiautomator2", # 平台名称 "platfORMName": "Android", # 系统版本 "platformVersion": "11", # 设备的名称 "deviceName": "RK3399", # 待测试应用的包名 "appPackage": "com.estrongs.android.pop", # 待测试应用的活动(Activity)名称 "appActivity": ".app.openscreenad.NewSplashActivity", # 设置使用 Unicode 编码方式发送字符串到设备的键盘 "unicodeKeyboard": "true", # 设置重置设备的软键盘状态并隐藏键盘 "resTKEyboard": "true" }) # Appium服务器地址端口,本地用Http://127.0.0.1:4723 appium_host = 'http://192.168.100.15:4723' return webdriver.Remote(appium_host, options=options)def close_driver(driver): """关闭驱动""" if driver: driver.quit()if __name__ == "__main__": driver = create_driver() # 设置隐式等待时间为10秒 driver.implicitly_wait(10) # 元素定位代码... # 关闭驱动 close_driver(driver)
1、根据id定位
# ID 定位方法el = driver.find_element(AppiumBy.ID, "com.estrongs.android.pop:id/txt_grant")el.click()
2、根据xpath定位
# xpath 方法el1 = driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@resource-id="android:id/title" and @text="密码设置"]')el1.click()# xpath 简写方法el2 = driver.find_element(AppiumBy.XPATH, '//*[@text="密码设置"]')el2.click()
3、根据class定位 (建议少用,重复名称较多)
# 使用class name定位el3 = driver.find_element(AppiumBy.CLASS_NAME, "android.widget.ImageButton")el3.click()
4、根据Accessibility ID定位
# 使用Accessibility ID定位el4 = driver.find_element(AppiumBy.ACCESSIBILITY_ID, '转到上一层级')el4.click()
5、根据UIAutomator定位
# 使用UIAutomator定位元素 (id定位)el5 = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.estrongs.android.pop:id/txt_grant")')el5.click()# 使用UIAutomator定位元素 (test定位)el6 = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("搜索")')el6.click()# 使用UIAutomator定位元素 (class name定位)el7 = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("android.widget.ImageButton")')el7.click()
6、相同元素定位
如上图,三个输入框的class属性都是一样的,如果要根据class属性分别来获取这三个值,就使用driver.find_elements方式。代码实现如下(注意 driver.find_elements 多个 s):
# 使用class name和索引定位,查找的元素列表中的特定元素el8 = driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.EditText")# 输入邮箱el8[0].send_keys("123456789@qq.com")# 输入验证码el8[1].send_keys("654321")# 输入密码el8[2].send_keys("123456")
# 运行ES文件浏览器软件,并点击同意el = driver.find_element(AppiumBy.ID, "com.estrongs.android.pop:id/txt_grant")el.click()# 单机操作(相当于鼠标点击):click()el1 = driver.find_element(AppiumBy.XPATH, '//*[@text="搜索"]')el1.click()# 输入:send_keys()el2 = driver.find_element(AppiumBy.CLASS_NAME, "android.widget.EditText")el2.send_keys("Android自动化")# 清空: clear()el3 = driver.find_element(AppiumBy.CLASS_NAME, "android.widget.EditText")el3.clear()
简单示例:
# 获取屏幕宽度和高度width = driver.get_window_size()["width"]height = driver.get_window_size()["height"]# 从下向上滑动屏幕driver.swipe(width*0.5, height*0.9, width*0.5, height*0.1, duration=500)# 从上向下滑动屏幕driver.swipe(width*0.5, height*0.1, width*0.5, height*0.9, duration=500)# 从右向左滑动屏幕driver.swipe(width*0.9, height*0.5, width*0.1, height*0.5, duration=500)# 从左向右滑动屏幕driver.swipe(width*0.1, height*0.5, width*0.9, height*0.5, duration=500)
封装示例:
class ScreenSlider(): def __init__(self, driver): """初始化屏幕滑动器""" self.driver = driver def get_screen_size(self): """获取屏幕尺寸""" screen_size = self.driver.get_window_size() return screen_size["width"], screen_size["height"] def swipe_up(self, duration=500): """从下向上滑动屏幕 x轴不变,y轴变动""" width, height = self.get_screen_size() self.driver.swipe(width*0.5, height*0.9, width*0.5, height*0.1, duration=duration) def swipe_down(self, duration=500): """从上向下滑动屏幕 x轴不变,y轴变动""" width, height = self.get_screen_size() self.driver.swipe(width*0.5, height*0.1, width*0.5, height*0.9, duration=duration) def swipe_left(self, duration=500): """从右向左滑动屏幕 x轴变动,y轴不变""" width, height = self.get_screen_size() self.driver.swipe(width*0.9, height*0.5, width*0.1, height*0.5, duration=duration) def swipe_right(self, duration=500): """从左向右滑动屏幕 x轴变动,y轴不变""" width, height = self.get_screen_size() self.driver.swipe(width*0.1, height*0.5, width*0.9, height*0.5, duration=duration)
swipe滑动操作,一般是两点之间的滑动,而实际使用过程中用户可能要进行一些多点连续滑动操作。如手势密码操作,切西瓜等场景。那么在Appium2中该如何模拟这类操作呢?
方法:
# 导入所需模块,用于执行 W3C actions 操作from selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.actions import interactionfrom selenium.webdriver.common.actions.action_builder import ActionBuilderfrom selenium.webdriver.common.actions.pointer_input import PointerInput# 创建 ActionChains 对象actions = ActionChains(driver)# 将 w3c_actions 属性替换为使用触摸(touch)指针交互的 ActionBuilder 对象actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))# 移动到坐标 (start_x, start_y) 的位置actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)# 执行指针按下操作(类似按下鼠标左键)actions.w3c_actions.pointer_action.pointer_down()# 暂停1秒钟actions.w3c_actions.pointer_action.pause(1)# 移动到坐标 (end_x, end_y) 的位置actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)# 执行指针释放操作(类似松开鼠标左键)actions.w3c_actions.pointer_action.release()# 执行动作actions.perform()
方法一、在Appium Inspector获取坐标
方法二、在设备端开发者选项中开启指针位置获取坐标
from appium import webdriverfrom appium.options.common.base import AppiumOptionsfrom appium.webdriver.common.appiumby import AppiumByfrom time import sleep# For W3C actionsfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.actions import interactionfrom selenium.webdriver.common.actions.action_builder import ActionBuilderfrom selenium.webdriver.common.actions.pointer_input import PointerInputdef create_driver(): """连接Android手机,并运行随手记APP""" options = AppiumOptions() options.load_capabilities({ "automationName": "uiautomator2", "platformName": "Android", "platformVersion": "11", "deviceName": "RK3399", "appPackage": "com.mymoney", "appActivity": ".biz.home.HomeActivity", "unicodeKeyboard": "true", "restKeyboard": "true" }) appium_host = 'http://127.0.0.1:4723' return webdriver.Remote(appium_host, options=options)def close_driver(driver): """关闭驱动""" if driver: driver.quit()if __name__ == "__main__": driver = create_driver() # 设置隐式等待时间为10秒 driver.implicitly_wait(10) actions = ActionChains(driver) actions.w3c_actions = ActionBuilder( driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch")) # 定位到"我的",点击 driver.find_element(AppiumBy.ID, "com.mymoney:id/ll_user").click() # 从下往上滑动页面 actions.w3c_actions.pointer_action.move_to_location(950, 792) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(954, 432) actions.w3c_actions.pointer_action.release() actions.perform() # 定位到"设置",点击 driver.find_element(AppiumBy.XPATH, '//*[@text="设置"]').click() # 定位到"密码保护",点击 driver.find_element(AppiumBy.ID, 'com.mymoney:id/setting_right_tip').click() sleep(0.5) # 启用密码保护 driver.find_element(AppiumBy.ID, 'com.mymoney:id/right_switch').click() # 定位到"手势密码",点击 driver.find_element(AppiumBy.ID, 'com.mymoney:id/iv_gesture_psd').click() # 设置手势密码,需要设置两次 for i in range(2): actions.w3c_actions.pointer_action.move_to_location(806, 284) actions.w3c_actions.pointer_action.pointer_down() actions.w3c_actions.pointer_action.move_to_location(1107, 284) actions.w3c_actions.pointer_action.pause(1) actions.w3c_actions.pointer_action.move_to_location(806, 594) actions.w3c_actions.pointer_action.pause(1) actions.w3c_actions.pointer_action.move_to_location(1103, 585) actions.w3c_actions.pointer_action.pause(1) actions.w3c_actions.pointer_action.release() actions.perform() # 等待2秒,再次确认密码 sleep(2) close_driver(driver)
来源地址:https://blog.csdn.net/qq_45664055/article/details/134712607
--结束END--
本文标题: Python之Appium 2自动化测试(Android篇)
本文链接: http://www.lsjlt.com/news/551728.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0