Python+Appium进行Android 应用程序自动化测试

Appium自动化测试

       Appium可以用于APP程序的爬取,本文介绍在windows下使用Appium和夜神模拟器连接的过程和主要注意的细节,供相关人员开发中参考。

首先介绍我使用到工具的版本情况。(不同软件版本会有少量差异)

工具或者软件

版本

Java

1.8.0_271

nodejs

v14.15.4

Android Studio

4.1.1

Appium-server

V1.19.1

夜神模拟器

6.6.1.2

逍遥模拟器

7.3.0

pycharm

2020.2.3

  • 1、环境安装和配置

序号

工具或者软件

注意事项

1

JDK开发环境

在系统变量path中增加java安装路径:

如:

……\Java\jdk1.8.0_271\bin

2

Android Studio安装(Android SDK)

  1. 根据实际需求选择安装的Android版本,本人使用的夜神模拟器是Android 5.0的,SDK选择5.0
  2. 在系统变量path中增加Android SDK安装路径3个路径:

…\Android\Sdk\platform-tools;

…\Android\Sdk\tools;

…\Android\Sdk\bin;

参考文档:

http://www.automationtestinghub.com/setup-android-environment-variables/

3

Nodejs安装

默认安装就可以

4

Appium-Server安装

 

5

Appium-Client安装

pip install Appium-Python-Client

 

  • 2.程序示例

下面以连接淘宝为例讲解下程序的连接主体逻辑:

# coding=utf-8

# This is a sample Python script.

 

# Press Shift+F10 to execute it or replace it with your code.

# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

from appium import webdriver

 

 

def app_test():

    desired_caps = {}

    # set Android system's information

    desired_caps['platformName'] = 'Android'

    desired_caps['platformVersion'] = '5.1'

    desired_caps['deviceName'] = '127.0.0.1:62001'

    desired_caps['appPackage'] = 'com.taobao.taobao'

    desired_caps['appActivity'] = 'com.taobao.tao.welcome.Welcome'

    # desired_caps['automationName'] = 'UiAutomator2'

    # send appium-server request

    # 'http://localhost:4723/wd/hub' appium server的地址和端口号

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

    # Use a breakpoint in the code line below to debug your script.

    print(f'OK')  # Press Ctrl+F8 to toggle the breakpoint.

# Press the green button in the gutter to run the script.

if __name__ == '__main__':

    app_test()

2.1程序运行前的准备工作

1、双击运行夜神模拟器;

2、进入夜神模拟器查询设备情况:

…\Nox\bin>nox_adb devices

List of devices attached

127.0.0.1:62001 device

3、使用夜神模拟器的adb进行连接

…\Nox\bin>nox_adb connect 127.0.0.1:62001

already connected to 127.0.0.1:62001

4、重启进行CMD启动目录查询是否已经连接成功;

C:\Users\…>adb devices

List of devices attached

127.0.0.1:62001 device

5、启动Appium server

6、进行夜神模拟器运行连接,

…\Nox\bin>adb connect 127.0.0.1: 62001

2.2各个参数的获取说明

1、安卓的版本信息

desired_caps['platformVersion'] = '5.1'

运行模拟器后,在设置系统中查询,截图如下:

2、设备名称

desired_caps['deviceName'] = '127.0.0.1:62001'

通过adb devices进行查询如下:

3、App名称

desired_caps['appPackage'] = 'com.taobao.taobao'

使用uiautomatorviewer进行获取,操作步骤如下截图:

4、正在运行的app

desired_caps['appActivity'] = 'com.taobao.tao.welcome.Welcome'

方法一:在CMD窗口中输入adb shell dumpsys activity activities,如下红色字体部分.

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)

Display #0 (activities from top to bottom):

  Stack #1:

    Task id #4

    * TaskRecord{13e91125 #4 A=com.taobao.taobao U=0 sz=1}

      userId=0 effectiveUid=u0a47 mCallingUid=u0a47 mCallingPackage=com.taobao.taobao

      affinity=com.taobao.taobao

      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.taobao.taobao/com.taobao.tao.welcome.Welcome}

      realActivity=com.taobao.taobao/com.taobao.tao.welcome.Welcome

……

方法二:adb shell dumpsys window|findstr mCurrentFocus

C:\Users\...>adb shell dumpsys window|findstr mCurrentFocus

  mCurrentFocus=Window{4ccc1c0 u0 wind.android/wind.android.TradeAndroidAppDelega

3常见问题

1、Android Studio已经进入如何配置Android SDK

在菜单File->Setting…->System Settings中进行配置

2、Android SDK连接夜神模拟器

  1. 夜神模拟器SDK版本和Android SDK版本保持一致.
  2. 如果不一致使用Android SDK的adb替换夜神模拟器的nox_adb;(注意做好备份)

参考方法:

https://www.douban.com/group/topic/111945732/

3、Android SDK连接逍遥模拟器

1、启动模拟器后,直接使用adb connect进行连接即可:

2、逍遥模拟器的端口查询方法

进入逍遥模拟器的安装目录,使用文件编辑器打开MEmu.memu-prev文件:…\Microvirt\MEmu\MemuHyperv VMs\MEmu\MEmu.memu-prev

截图如下:

参考:

https://www.cnblogs.com/Thenext/p/13488599.html

4、uiautomatorviewer 定位提示Error obtaining UI hierarchy

使用任务管理器中关闭adb.exe进行,重新启动并连接后进行定位

参考

https://blog.csdn.net/xiezhiming1234/article/details/82462892

    1.  uiautomatorviewer截图横屏处理
  1. 保存截图文件;
  2. 在电脑上进行旋转;
  3. 再次打开就可以了;

参考文档

https://blog.csdn.net/syousetu/article/details/110677595

 

5、 使用bounds进行定位后tap

为保持程序在不同尺寸大小程序都可以使用,可自行封装如下代码:

# 重新封装点击A点相对坐标的方法

def tapA(driver, positions, duration):

    # 获取新测试机屏幕宽、高

    """Perform a tap action on the element

 

            Args:

                driver: the element to driver, likes driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

                positions: an array of tuples representing the x/y coordinates of

                   the fingers to tap. Length can be up to five.

                duration: length of time to tap, in ms

            Usage:

                driver.tap([(100, 20), (100, 60), (100, 100)], 500)

            Returns:

                None

    """

    w = driver.get_window_size()['width']

    h = driver.get_window_size()['height']

    if len(positions) == 1:

        # 比例系数

        x = positions[0][0]

        y = positions[0][1]

        x = x/720

        y = y/1280

        # 屏幕宽高乘以A点的比例系数,即可得A点在新测试机上坐标

        driver.tap(x*w, y*h, duration)

    else:

        positions2 = []

        for position in positions:

            x = position[0] / 720

            y = position[1] / 1280

            positions2.append((x * w, y * h))

        driver.tap(positions2, duration)

6、Appium中使用taps点击没有响应

使用管理员运行pycharm

7、使用Appium进行定位分析操作指导

adb已经完成模拟器的连接

1、server端完成启动;

2、点击File->New Session windows..,

3、在新窗口输出连接的配置参考,如下:

注:配置完成后可以save下次连接时直接在”Saved Capability Sets 2”中直接打开就可以,节省时间。

4、点击Start Session

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值