Python之Appium 2自动化测试(Android篇)

这篇具有很好参考价值的文章主要介绍了Python之Appium 2自动化测试(Android篇)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、环境搭建及准备工作

1、Appium 2 环境搭建

  • 请参考另一篇文章: Windows系统搭建Appium 2 和 Appium Inspector 环境

2、安装 Appium-Python-Client,版本要求3.0及以上 和 Selenium 版本要求4.0及以上

pip install Appium-Python-Client
Version: 3.1.0

pip install selenium
Version: 4.15.2

3、手机连接电脑,并在dos窗口启动 Appium Server
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

4、演示环境APP软件:ES文件浏览器、随手记

5、查看元素唯一方法

  • 复制id,点击搜索图标
    Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android
  • 选择id,粘贴内容,点击Search,查看

Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android


二、编写自动化脚本

from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy


def 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()

Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

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()

Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

3、根据class定位 (建议少用,重复名称较多)

# 使用class name定位
el3 = driver.find_element(AppiumBy.CLASS_NAME, "android.widget.ImageButton")
el3.click()

Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

4、根据Accessibility ID定位

# 使用Accessibility ID定位
el4 = driver.find_element(AppiumBy.ACCESSIBILITY_ID, '转到上一层级')
el4.click()

Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

5、根据UIAutomator定位

  • UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiautomator的类库去查找元素。 Appium元素定位方法其实也是基于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、相同元素定位
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android
如上图,三个输入框的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()

五、swipe()方法模拟滑动操作

  • 滑动操作是模拟用户在应用程序界面上进行手势滑动的操作。在Appium中,可以使用swipe()方法来执行滑动操作。它需要指定起始点和终止点的坐标,并且可以设置滑动的持续时间。滑动操作通常用于测试应用程序界面的可滚动性、页面切换和内容展示等功能。
  • swipe(起始横坐标,起始纵坐标,目标横坐标,目标纵坐标,时间)
  • 时间:指滑动使用多长时间,单位为毫秒,可为空(去掉duration=****)

简单示例:

# 获取屏幕宽度和高度
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 WebDriver actions
      Appium2进行了一次重大更新,其中最重要的变化是弃用了MultiAction和TouchAction,并全面采用了W3C WebDriver actions。过去,MultiAction和TouchAction被广泛用于模拟复杂手势和多点触控操作,但现在W3C WebDriver actions成为了首选解决方案。这个决策带来了更统一、标准化的操作方式,同时也提供了更好的跨平台兼容性和稳定性。

方法:

# 导入所需模块,用于执行 W3C actions 操作
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from 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()
  • 以下用随手记APP操作演示

方法一、在Appium Inspector获取坐标
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android

方法二、在设备端开发者选项中开启指针位置获取坐标
Python之Appium 2自动化测试(Android篇),# Android篇,python,appium,android文章来源地址https://www.toymoban.com/news/detail-760204.html

from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
from time import sleep

# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput


def 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)

到了这里,关于Python之Appium 2自动化测试(Android篇)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python +Appium 实现app自动化测试

    一、Appium简介 Appium是一款开源工具,用于自动化iOS、Android和Windows桌面平台上的本地、移动web和混合应用程序。原生应用是指那些使用iOS、Android或Windows sdk编写的应用。移动网页应用是通过移动浏览器访问的网页应用(appum支持iOS和Chrome上的Safari或Android上的内置“浏览器”应用

    2023年04月09日
    浏览(27)
  • python appium UI 自动化测试框架讨论

    目录 前言: 框架共性总结 Auto_Analysis 权限弹窗识别 前言:  Python Appium UI自动化测试框架是一种用于测试移动应用程序的工具,它结合了Python编程语言和Appium测试框架的功能。 框架共性总结 1 自动找设备 连接设备 2 自动启 appium server 3 用例框架 unittest pytest 4 用例组织 yml 读

    2024年02月16日
    浏览(44)
  • Python+Appium实现自动化测试的使用步骤

    这篇文章主要介绍了Python+Appium实现自动化测试的使用步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 1.脚本语言:Python3.x IDE:安装Pycharm 2.安装Java JDK 、Android SDK 3.adb环境,path添加E:

    2024年02月03日
    浏览(29)
  • python+appium+pytest自动化测试-参数化设置

    来自APP Android端自动化测试初学者的笔记,写的不对的地方大家多多指教哦。(所有内容均以微博V10.11.2版本作为例子) 在自动化测试用例执行过程中,经常出现执行相同的用例,但传入不同的参数,导致我们需要重复的写用例,这样会使我们的用例变得很长,冗余,很多地

    2023年04月08日
    浏览(34)
  • 【UI自动化测试】appium+python+unittest+HTMLRunner

    进阶Python接口自动化测试必备教程(2023全网最详细) 简介  获取AppPackage和AppActivity  定位UI控件的工具  脚本结构  PageObject分层管理  HTMLTestRunner生成测试报告  启动appium server服务  以python文件模式执行脚本生成测试报告 下载与安装 下载需要自动化测试的App并安装到手机

    2024年02月16日
    浏览(37)
  • python+appium微信小程序/公众号自动化测试

    环境准备 调试微信公众号时,查看进程名称,公众号要先进入文章或者内部程序,小程序就进入内部程序即可 公众号和小程序的自动化不是必须要做,一般我是作为数据脚本使用 步骤 开启手机上的USB调试功能(开发者模式) 打开Chrome浏览器,地址栏输入:Chrome://inspect 直接

    2024年02月09日
    浏览(34)
  • Python+Appium+Pytest+Allure实战APP自动化测试框架

    Hi,大家好。今天我们来聊聊Python+Appium+Pytest+Allure实战APP自动化测试,pytest只是单独的一个单元测试框架,要完成app测试自动化需要把pytest和appium进行整合,同时利用allure完成测试报告的产出。 编写常规的 线性 脚本具体的步骤如下: 1、设计待测试APP的 自动化测试 用例 2、

    2023年04月09日
    浏览(43)
  • 【编写UI自动化测试集】Appium+Python+Unittest+HTMLRunner​

    简介  获取AppPackage和AppActivity  定位UI控件的工具  脚本结构  PageObject分层管理  HTMLTestRunner生成测试报告  启动appium server服务  以python文件模式执行脚本生成测试报告 下载与安装 下载需要自动化测试的App并安装到手机 获取AppPackage和AppActivity 方法一 有源码的情况直接打开

    2024年02月11日
    浏览(38)
  • Python与Appium实现手机APP自动化测试的示例代码

    本文主要介绍了Python与Appium实现手机APP自动化测试的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 appium是一个开源的测试自动化框架,可以与原生的、混合的和移动的web应用程序一直使用。它使用WebDriver协议驱动IOS(内

    2024年01月24日
    浏览(39)
  • APP自动化测试-Python+Appium+Pytest+Allure框架实战封装(详细)

    pytest只是单独的一个单元测试框架,要完成app测试自动化需要把pytest和appium进行整合,同时利用allure完成测试报告的产出。 编写常规的线性脚本具体的步骤如下: 1、设计待测试APP的自动化测试用例 2、新建app测试项目 3、配置conftest.py文件等 4、编写整体app测试用例运行文件

    2024年02月14日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包