美文网首页
Mac下搭建appium+android+python自动化测试

Mac下搭建appium+android+python自动化测试

作者: 开心爸爸 | 来源:发表于2019-02-01 15:11 被阅读5次

    Mac本系统:10.14.3 (Mojave)
    appium版本:1.10.0
    jdk:11.0.2
    python:3.6

    一、简单介绍

    (一)测试对象

    appium 是一个自动化测试开源工具,支持iOS和 Android平台上的原生应用,web应用和混合应用。
    ● 移动原生应用:单纯用ios或者android开发语言编写的、针对具体某类移动设备、可直接被安装到设备里的应用,一般可通过应用商店获取,比如某个游戏app;
    ● 移动web应用:使用移动浏览器访问的应用(appium支持iOS上的Safari和Android上的 Chrome),不需要下载到设备上,而是通过浏览器直接访问,比如H5九曲封神游戏;
    ● 混合应用:同时使用网页语言与程序语言开发,通过应用商店区分移动操作系统分发,用户需要安装使用的移动应用,比如手机上的淘宝客户端

    (二)支持平台及语言

    ● appium是跨平台的,支持OSX,Windows以及Linux系统。它允许测试人员在不同的平台(iOS,Android)使用同一套API来写自动化测试脚本,这样大大增加了iOS和Android测试套件间代码的复用性
    ● appium支持多语言,采用C/S设计模式,只要满足client能够发送http请求给server即可

    (三)工作原理

    以下图片引用网址:http://www.testclass.net/appium/appium-base-summary/

    image

    如图所示,appium主要分为三个点:client端,server端,及测试移动设备
    ● client端:用各类语言写的测试脚本
    ● server端:我们安装appium工具的那部分,专门用来监听并接收来自client端的请求,转发请求并控制移动设备执行测试,默认开启并监听4723接口
    ● 移动设备:支持三类设备,我们的最终目的:在设备上自动执行指定的操作
    整个过程:
    第一,在某台服务器上安装appium工具(server端),开启进程(默认4723接口)进行监听;
    第二,编写测试脚本,放置到服务器上,执行该测试脚本(相当于发送命令给server端)
    第三,移动设备接收到server端发送过来的命令,执行指定的操作

    (四)安装工具

    ● 测试语言,如python
    ● appium客户端
    ● appium服务端
    ● 移动设备,若使用虚拟机则需进行安

    二、环境搭建

    (一)首先安装JDK和下载SDK

    1、JDK安装参考如下文章:

    https://www.jianshu.com/p/506d7df216dd #Mac下如何安装JDK

    JDK路径:

    /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    
    2、安装Android SDK,直接访问官网下载的Android Studio(它自带)

    http://www.android-studio.org/index.php/download
    1、Android SDK(Software Development Kit,软件开发工具包)提供了 Android API 库和开发工具构建,测试和调试应用程序,可以看做用于开发和运行Android应用的一个软件
    2、提供小工具,比如adb、aapt、uiautomatorview
    3、测试设备使用安卓模拟器,这一步决不能跳过
    安装后SDK路径:

    /Users/kk/Library/Android/sdk
    
    3、环境变量配置:(具体参考https://www.jianshu.com/p/682a7fae88b2 #Mac下如何配置环境变量 jdk和sdk)
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    
    export PATH=$JAVA_HOME/bin:$PATH
    
    export ANDROID_HOME=/Users/xxx/Library/Android/sdk
    
    export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools/
    
    export PATH=$PATH:/usr/local/bin:/usr/local/sbin:${PATH}
    
    export AAPT_HOME=/Users/xxx/Library/Android/sdk/build-tools/28.0.3
    
    export AAPT_HOME
    
    export PATH=$PATH:$AAPT_HOME
    

    验证jdk环境是否配置ok:

    xxx@bogon  ~  java --version
    java 11.0.2 2019-01-15 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
    

    验证 android sdk环境是否配置成功可以简单的使用 adb 命令

    xxx@bogon  ~  adb devices
    List of devices attached
    d0f88a5b    device
    

    (二)安装 appium Server

    1、下载地址:https://github.com/appium/appium-desktop/releases
    2、下载appium客户端安装包(Appium-1.10.0.dmg等)
    3、安装appium服务端

    npm install -g appium
    

    4、再安装appium doctor

    npm install -g appium-doctor
    

    5、再安装 python-client
    先安装编程语言,比如python语言
    安装Appium-Client,python的话可使用pip安装:pip install Appium-Python-Client

    或者如下安装

    git clone git@github.com:appium/python-client.git
    cd python-client
    python setup.py install
    

    二、然后用appium-doctor来检测我们的所有的自动化环境

    xxx@bogon  ~  appium-doctor
    info AppiumDoctor Appium Doctor v.1.8.0
    info AppiumDoctor ### Diagnostic for necessary dependencies starting ###
    info AppiumDoctor  ✔ The Node.js binary was found at: /usr/local/bin/node
    info AppiumDoctor  ✔ Node version is 11.8.0
    info AppiumDoctor  ✔ Xcode is installed at: /Applications/Xcode.app/Contents/Developer
    info AppiumDoctor  ✔ Xcode Command Line Tools are installed in: /Applications/Xcode.app/Contents/Developer
    info AppiumDoctor  ✔ DevToolsSecurity is enabled.
    info AppiumDoctor  ✔ The Authorization DB is set up properly.
    info AppiumDoctor  ✔ Carthage was found at: /usr/local/bin/carthage
    info AppiumDoctor  ✔ HOME is set to: /Users/xxx
    info AppiumDoctor  ✔ ANDROID_HOME is set to: /Users/xxx/Library/Android/sdk
    info AppiumDoctor  ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    info AppiumDoctor  ✔ adb exists at: /Users/xxx/Library/Android/sdk/platform-tools/adb
    info AppiumDoctor  ✔ android exists at: /Users/xxx/Library/Android/sdk/tools/android
    info AppiumDoctor  ✔ emulator exists at: /Users/xxx/Library/Android/sdk/tools/emulator
    info AppiumDoctor  ✔ Bin directory of $JAVA_HOME is set
    info AppiumDoctor ### Diagnostic for necessary dependencies completed, no fix needed. ###
    info AppiumDoctor
    info AppiumDoctor ### Diagnostic for optional dependencies starting ###
    WARN AppiumDoctor  ✖ opencv4nodejs cannot be found.
    WARN AppiumDoctor  ✖ ffmpeg cannot be found
    WARN AppiumDoctor  ✖ fbsimctl cannot be found
    WARN AppiumDoctor  ✖ applesimutils cannot be found
    WARN AppiumDoctor  ✖ idevicelocation cannot be found
    WARN AppiumDoctor  ✖ bundletool.jar cannot be found
    info AppiumDoctor ### Diagnostic for optional dependencies completed, 6 fixes needed. ###
    info AppiumDoctor
    info AppiumDoctor ### Optional Manual Fixes ###
    info AppiumDoctor The configuration can install optionally. Please do the following manually:
    WARN AppiumDoctor  ➜ Why opencv4nodejs is needed and how to install it: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md
    WARN AppiumDoctor  ➜ ffmpeg is needed to record screen features. Please read https://www.ffmpeg.org/ to install it
    WARN AppiumDoctor  ➜ Why fbsimctl is needed and how to install it: http://appium.io/docs/en/drivers/ios-xcuitest/
    WARN AppiumDoctor  ➜ Why applesimutils is needed and how to install it: http://appium.io/docs/en/drivers/ios-xcuitest/
    WARN AppiumDoctor  ➜ idevicelocation is used to set geolocation for real device. Please read https://github.com/JonGabilondoAngulo/idevicelocation to install it
    WARN AppiumDoctor  ➜ bundletool.jar is used to handle Android App Bundle. Please read http://appium.io/docs/en/writing-running-appium/android/android-appbundle/ to install it
    info AppiumDoctor
    info AppiumDoctor ###
    info AppiumDoctor
    info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied!
    info AppiumDoctor
    

    下图这部分的错误,我暂时先忽略了,正常功能是可以用了。


    image.png

    环境就此搭建好了!

    开始我们的android脚本的编写

    打开appium客户端,开始配置参数


    image.png

    配置edit Environment


    image.png

    点击开始start server后再点击左上角的start inspector session配置参数如下


    image.png
    {
      "platformName": "Android",
      "platformVersion": "8.1.0",
      "deviceName": "OnePlus 5T",
      "app": "/Users/xx/Desktop/xxx.apk",
      "udid": "d0f88a5b",
      "AutomationName": "Appium",
      "newCommandTimeout": "60",
      "appWaitActivity": "com.xxx.xxx.activity.TabNewMainActivity"
    }
    

    常见参数解释:

    ● deviceName:指定启动设备的名称,比如Android Emulator、iPhone Simulator等(不重要)
    ● automationName:指定自动化引擎,默认appium
    ● platformName:指定移动平台,Android或者iOS
    ● platformVersion:指定平台的系统版本。例如指定Android系统版本为8.1.0
    ● appWaitActivity:待测试app的Activity
    ● appActivity:待测试app的Activity,注意,原生app的话要在activity前加个“.”
    ● appPackage:待测试app的包名(package)信息
    ● app:要测试apk包路径
    ● udid:手机device编码,连接上手机后使用adb devices可以查询到

    点击start session进入(appium自带的Inspector)
    查询被测试apk中元素


    image.png

    相关文章

      网友评论

          本文标题:Mac下搭建appium+android+python自动化测试

          本文链接:https://www.haomeiwen.com/subject/jtddsqtx.html