欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java自动化测试(app自动化环境搭建 31)

程序员文章站 2022-04-13 11:42:53
...

Appium

官网:http://appium.io/

github:https://github.com/appium/appium

Appium is an open source test automation framework for use with native, hybrid and mobile web apps.
It drives iOS, Android, and Windows apps using the WebDriver protocol.

第三方自动化工具,扩充了Selenium WebDriver协议,在原有的基础上添加了移动端测试API

是一个开源的,跨平台的自动化测试工具,可以用于app的自动化测试

支持androidiosfirefox os等操作系统下的app测试

Appium组件

Appium Server

Appium的核心,实质上是一个web接口服务器,使用node.js实现

Appium GUI

Appium Desktop前身,已经停止维护,封装的Appium Server 是1.4.16

Java自动化测试(app自动化环境搭建 31)
Appium GUI

Appium Desktop

内嵌了 Appium Server,提供GUI界面,还包括其他工具的整合(如Appium Inspector)

Appium Client

客户端向Appium Server发起请求,可以用不同语言来实现

例如java:https://github.com/appium/java-client

App自动化测试环境搭建

配置Android sdk环境

  1. SDK:软件开发套件

  2. adb:是android sdk的一个工具,用来连接安卓手机和PC端端桥梁

在mac上安装:

$ brew cask install android-platform-tools
$ echo 'export PATH=$PATH:~/.android-sdk-macosx/platform-tools/' >> ~/.bash_profile
$ source ~/.bash_profile

检查一下是否安装完成:

$ adb version
Java自动化测试(app自动化环境搭建 31)
检查环境

模拟器下载

https://www.yeshen.com/

Java自动化测试(app自动化环境搭建 31)
模拟器

Appium 桌面版下载

https://github.com/appium/appium-desktop/releases/tag/v1.18.0-2

Java自动化测试(app自动化环境搭建 31)
Appium 桌面版

启动

Java自动化测试(app自动化环境搭建 31)
启动

编写代码

配置Maven

下载 java-client

https://mvnrepository.com/artifact/io.appium/java-client

配置设备信息

  1. 设备平台

  2. 设备名称

  3. APP包名

  4. App activity名称

使用adb connect 127.0.0.1:62001连接设备

其中62001是夜神模拟器的端口

连接设备

Java自动化测试(app自动化环境搭建 31)
连接设备

查看包名

$ aapt dump badging xxxx
$ aapt dump badging /Users/zhongxin/Downloads/app自动化工具包/APK包/lemonban_release_v2.1.2_finally.apk
$ /Applications/NoxAppPlayer.app/Contents/MacOS/aapt dump badging /Users/zhongxin/Downloads/app自动化工具包/APK包/lemonban_release_v2.1.2_finally.apk
package: name='com.lemon.lemonban' versionCode='20181113' versionName='2.1.2' platformBuildVersionName='7.1.1'
sdkVersion:'15'
targetSdkVersion:'25'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.READ_EXTERNAL_STORAGE'
uses-permission: name='android.permission.CAMERA'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.READ_PHONE_STATE'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.CHANGE_WIFI_STATE'
uses-permission: name='android.permission.ACCESS_WIFI_STATE'
uses-permission: name='android.permission.WAKE_LOCK'
uses-permission: name='android.permission.RECEIVE_BOOT_COMPLETED'
uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.VIBRATE'
uses-permission: name='android.permission.GET_TASKS'
uses-permission: name='android.permission.BLUETOOTH'
uses-permission: name='android.permission.BLUETOOTH_ADMIN'
uses-permission: name='android.permission.ACCESS_FINE_LOCATION'
uses-permission: name='android.permission.ACCESS_COARSE_LOCATION'
uses-permission: name='android.permission.SYSTEM_ALERT_WINDOW'
uses-permission: name='getui.permission.GetuiService.com.lemon.lemonban'
uses-permission: name='android.permission.MOUNT_UNMOUNT_FILESYSTEMS'
application-label:'柠檬班'
application-label-af:'柠檬班'
application-label-am:'柠檬班'
application-label-ar:'柠檬班'
application-label-az-AZ:'柠檬班'
application-label-be-BY:'柠檬班'
application-label-bg:'柠檬班'
application-label-bn-BD:'柠檬班'
application-label-bs-BA:'柠檬班'
application-label-ca:'柠檬班'
application-label-cs:'柠檬班'
application-label-da:'柠檬班'
application-label-de:'柠檬班'
application-label-el:'柠檬班'
application-label-en-AU:'柠檬班'
application-label-en-GB:'柠檬班'
application-label-en-IN:'柠檬班'
application-label-es:'柠檬班'
application-label-es-US:'柠檬班'
application-label-et-EE:'柠檬班'
application-label-eu-ES:'柠檬班'
application-label-fa:'柠檬班'
application-label-fi:'柠檬班'
application-label-fr:'柠檬班'
application-label-fr-CA:'柠檬班'
application-label-gl-ES:'柠檬班'
application-label-gu-IN:'柠檬班'
application-label-hi:'柠檬班'
application-label-hr:'柠檬班'
application-label-hu:'柠檬班'
application-label-hy-AM:'柠檬班'
application-label-in:'柠檬班'
application-label-is-IS:'柠檬班'
application-label-it:'柠檬班'
application-label-iw:'柠檬班'
application-label-ja:'柠檬班'
application-label-ka-GE:'柠檬班'
application-label-kk-KZ:'柠檬班'
application-label-km-KH:'柠檬班'
application-label-kn-IN:'柠檬班'
application-label-ko:'柠檬班'
application-label-ky-KG:'柠檬班'
application-label-lo-LA:'柠檬班'
application-label-lt:'柠檬班'
application-label-lv:'柠檬班'
application-label-mk-MK:'柠檬班'
application-label-ml-IN:'柠檬班'
application-label-mn-MN:'柠檬班'
application-label-mr-IN:'柠檬班'
application-label-ms-MY:'柠檬班'
application-label-my-MM:'柠檬班'
application-label-nb:'柠檬班'
application-label-ne-NP:'柠檬班'
application-label-nl:'柠檬班'
application-label-pa-IN:'柠檬班'
application-label-pl:'柠檬班'
application-label-pt:'柠檬班'
application-label-pt-BR:'柠檬班'
application-label-pt-PT:'柠檬班'
application-label-ro:'柠檬班'
application-label-ru:'柠檬班'
application-label-si-LK:'柠檬班'
application-label-sk:'柠檬班'
application-label-sl:'柠檬班'
application-label-sq-AL:'柠檬班'
application-label-sr:'柠檬班'
application-label-sr-Latn:'柠檬班'
application-label-sv:'柠檬班'
application-label-sw:'柠檬班'
application-label-ta-IN:'柠檬班'
application-label-te-IN:'柠檬班'
application-label-th:'柠檬班'
application-label-tl:'柠檬班'
application-label-tr:'柠檬班'
application-label-uk:'柠檬班'
application-label-ur-PK:'柠檬班'
application-label-uz-UZ:'柠檬班'
application-label-vi:'柠檬班'
application-label-zh:'柠檬班'
application-label-zh-CN:'柠檬班'
application-label-zh-HK:'柠檬班'
application-label-zh-TW:'柠檬班'
application-label-zu:'柠檬班'
application-icon-120:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-160:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-240:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-320:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-480:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-640:'res/mipmap-anydpi-v26/ic_launcher.xml'
application-icon-65534:'res/mipmap-anydpi-v26/ic_launcher.xml'
application: label='柠檬班' icon='res/mipmap-anydpi-v26/ic_launcher.xml'
launchable-activity: name='com.lemon.lemonban.activity.WelcomeActivity'  label='' icon=''
feature-group: label=''
  uses-feature: name='android.hardware.bluetooth'
  uses-implied-feature: name='android.hardware.bluetooth' reason='requested android.permission.BLUETOOTH permission, requested android.permission.BLUETOOTH_ADMIN permission, and targetSdkVersion > 4'
  uses-feature: name='android.hardware.camera'
  uses-implied-feature: name='android.hardware.camera' reason='requested android.permission.CAMERA permission'
  uses-feature: name='android.hardware.faketouch'
  uses-implied-feature: name='android.hardware.faketouch' reason='default feature for all apps'
  uses-feature: name='android.hardware.location'
  uses-implied-feature: name='android.hardware.location' reason='requested android.permission.ACCESS_COARSE_LOCATION permission, and requested android.permission.ACCESS_FINE_LOCATION permission'
  uses-feature: name='android.hardware.screen.portrait'
  uses-implied-feature: name='android.hardware.screen.portrait' reason='one or more activities have specified a portrait orientation'
  uses-feature: name='android.hardware.wifi'
  uses-implied-feature: name='android.hardware.wifi' reason='requested android.permission.ACCESS_WIFI_STATE permission, and requested android.permission.CHANGE_WIFI_STATE permission'
main
other-activities
other-receivers
other-services
supports-screens: 'small' 'normal' 'large' 'xlarge'
supports-any-density: 'true'
locales: '--_--' 'af' 'am' 'ar' 'az-AZ' 'be-BY' 'bg' 'bn-BD' 'bs-BA' 'ca' 'cs' 'da' 'de' 'el' 'en-AU' 'en-GB' 'en-IN' 'es' 'es-US' 'et-EE' 'eu-ES' 'fa' 'fi' 'fr' 'fr-CA' 'gl-ES' 'gu-IN' 'hi' 'hr' 'hu' 'hy-AM' 'in' 'is-IS' 'it' 'iw' 'ja' 'ka-GE' 'kk-KZ' 'km-KH' 'kn-IN' 'ko' 'ky-KG' 'lo-LA' 'lt' 'lv' 'mk-MK' 'ml-IN' 'mn-MN' 'mr-IN' 'ms-MY' 'my-MM' 'nb' 'ne-NP' 'nl' 'pa-IN' 'pl' 'pt' 'pt-BR' 'pt-PT' 'ro' 'ru' 'si-LK' 'sk' 'sl' 'sq-AL' 'sr' 'sr-Latn' 'sv' 'sw' 'ta-IN' 'te-IN' 'th' 'tl' 'tr' 'uk' 'ur-PK' 'uz-UZ' 'vi' 'zh' 'zh-CN' 'zh-HK' 'zh-TW' 'zu'
densities: '120' '160' '240' '320' '480' '640' '65534'
(base) zhongxindeMacBook-Pro:~ zhongxin$ 

代码上连接设备

package com.zhongxin.demo;

import org.openqa.selenium.remote.DesiredCapabilities;

public class AppDemo {
    public static void main(String[] args) {
        //配置设备信息
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //1.设备平台
        capabilities.setCapability("platformName", "Android");
        //2.设备名称
        capabilities.setCapability("deviceName", "127.0.0.1:62001");
        //3.APP包名
        capabilities.setCapability("appPackage", "com.lemon.lemonban");
        //4.App activity名称
        capabilities.setCapability("appActivity", "com.lemon.lemonban.activity.WelcomeActivity");
    }
}