• Python+Appium自动化测试-编写自动化脚本


    一,连接测试手机,获取测试机及被测APP配置

    配置信息如下:

    1. {
    2. "platformName": "Android",
    3. "platformVersion": "10",
    4. "deviceName": "PCT_AL10",
    5. "appPackage": "com.ss.android.article.news",
    6. "appActivity": ".activity.MainActivity"
    7. }

    二,编写Python脚本启动app

    1,编辑器推荐大家使用PyCharm,下载pycharm社区版本,免费无需破解。

    2,因为登陆需要输入账号、密码,所以这里新增了两个参数。参数unicodeKeyboard即是否启用Unicode格式输入字符串,默认值为False,设置为True则表示启用。参数resetKeyboard即使用unicodeKeyboard功能进行Unicode输入后,是否将键盘重置为原始状态,默认False。

    1. desired_caps = {
    2. "platformName": "Android",
    3. "platformVersion": "10",
    4. "deviceName": "PCT_AL10",
    5. "appPackage": "com.ss.android.article.news",
    6. "appActivity": ".activity.MainActivity",
    7. "unicodeKeyboard": True,
    8. "resetKeyboard": True,
    9. }

    3,传入desired_caps通过appium启动app,构造driver对象(即创建一个session)。

    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    

    appium在本机开启的话则IP为127.0.0.1,端口默认为4723

    4,电脑连接手机,开启appium服务,运行脚本,调试是否能启动app。

    1. from appium import webdriver
    2. desired_caps = {
    3. "platformName": "Android",
    4. "platformVersion": "10",
    5. "deviceName": "PCT_AL10",
    6. "appPackage": "com.ss.android.article.news",
    7. "appActivity": ".activity.MainActivity",
    8. "unicodeKeyboard": True,
    9. "resetKeyboard": True,
    10. }
    11. # 启动app
    12. driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    我们会看到手机上今日头条app被打开。

    三,通过appium Inspector定位元素,登陆APP

    1,登陆操作步骤为:

    启动今日头条app --> 点击【我知道了】--> 确定管理权限 --> 点击底部tab“未登陆” 进入未登录页面 --> 点击 “登陆” --> 点击“...” --> 选择密码登陆 --> 输入账号密码 --> 点击登陆
    

    2,根据操作步骤,使用appium的Inspector获取登陆相关操作元素的属性

    [进入appium Inspector页面](#),先点击选择元素按钮,然后在窗口左侧展示的手机页面上,点击选择需要定位的元素,窗口右侧会展示该元素的属性值。手机app页面切换后,点击窗口刷新页面按钮,左侧页面会刷新。
    

    appium Inspector获取不到的元素属性的话,还可以用Android SDK里自带的工具uiautomatorviewer获取。后面会专门介绍怎样获取元素属性,这里不详述。

    3,登陆操作脚本

    1. # -*- coding:utf-8 -*-
    2. import time
    3. from appium import webdriver
    4. desired_caps = {
    5. "platformName": "Android",
    6. "platformVersion": "10",
    7. "deviceName": "PCT_AL10",
    8. "appPackage": "com.ss.android.article.news",
    9. "appActivity": ".activity.MainActivity",
    10. "unicodeKeyboard": True,
    11. "resetKeyboard": True,
    12. }
    13. # 启动app
    14. driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    15. # 登陆操作
    16. driver.find_element_by_id("com.ss.android.article.news:id/chj").click() # 点击【我知道了】
    17. time.sleep(1)
    18. driver.find_element_by_id("android:id/button1").click() # 点击权限管理-确定按钮
    19. time.sleep(1)
    20. driver.find_element_by_xpath("//android.widget.TabWidget/android.widget.RelativeLayout[@index=3]").click() # 点击未登录
    21. time.sleep(1)
    22. driver.find_element_by_id("com.ss.android.article.news:id/a1c").click() # 未登录页点击登录按钮
    23. time.sleep(1)
    24. driver.find_element_by_id("com.ss.android.article.news:id/bfm").click() # 登录页点击“。。。”
    25. time.sleep(1)
    26. driver.find_element_by_xpath("//android.widget.LinearLayout[@index=4]").click() # 选择密码登录
    27. time.sleep(1)
    28. driver.find_element_by_id("com.ss.android.article.news:id/c7").send_keys("********") # 输入账号
    29. time.sleep(1)
    30. driver.find_element_by_id("com.ss.android.article.news:id/ch").send_keys("********") # 输入密码
    31. time.sleep(1)
    32. driver.find_element_by_id("com.ss.android.article.news:id/a31").click() # 点击登录
    33. time.sleep(5)

    我们查看手机,会发现手机正在自动做登录今日头条的app操作,且登录成功。

    至此,我们通过编写一个简单的Python脚本完成了登录app的操作,如果加上断言,就是一条完整的用例。

    Python接口自动化测试零基础入门到精通(2023最新版)

  • 相关阅读:
    2022美团秋招java面试流程,技术面题解析 看完吊打面试官
    0基础跟我学python---进阶篇(3)python中的ORM框架(1)
    K8S的调度算法在仿真实验的实现
    抽象类和接口
    pthread使用
    idea如何设置jvm大小
    推荐莹莹API管理系统PHP源码
    R语言使用dplyr包的arrange函数进行dataframe排序、arrange函数基于一个字段(变量)进行升序排序实战(默认升序)
    二、工业方案推荐系统
    [活动(深圳)] .NET Love AI 之 .NET Conf China 2023 Party 深圳
  • 原文地址:https://blog.csdn.net/dad22211/article/details/134341527