在模拟器中运行可重复测试的Android项目示例

过去几天,我一直在与Android命令行斗争,以建立一个简单的项目,该项目可以在模拟器内可靠地、可重复地运行自动化测试。
【在模拟器中运行可重复测试的Android项目示例】为了使测试可靠并独立于我机器上的任何其他东西,我希望将Android SDK和AVD文件存储在一个本地目录中。
为此,我必须定义许多相互关联的环境变量,并将这些工具打包到脚本中,以确保它们以正确的标志和设置运行。
您需要所有的实用程序脚本包括在该repo工作,但一些高亮包括:
我在每个脚本、脚本/路径中引用的环境变量:

PROJECT_ROOT=$(dirname $(dirname $(realpath ${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}))) export ANDROID_SDK_ROOT="${PROJECT_ROOT}/android_sdk" export ANDROID_SDK_HOME="${ANDROID_SDK_ROOT}" export ANDROID_EMULATOR_HOME="${ANDROID_SDK_ROOT}/emulator-home" export ANDROID_AVD_HOME="${ANDROID_EMULATOR_HOME}/avd"

创建一个本地。
属性文件告诉Gradle和Android Studio SDK在哪里,运行如下代码:
echo "# File created automatically - changes will be overwritten!" > local.properties echo "sdk.dir=${ANDROID_SDK_ROOT}" >> local.properties

Android工具的包装脚本,例如scripts/sdkmanager:
#!/bin/bashset -e set -usource scripts/paths"${ANDROID_SDK_ROOT}/tools/bin/sdkmanager" \ "--sdk_root=${ANDROID_SDK_ROOT}" \ "$@"

avdmanager的包装器是特别有趣的,因为它似乎我们需要覆盖它认为的工具目录是为了让它正常工作- scripts/avdmanager:
#!/bin/bashset -e set -usource scripts/paths# Set toolsdir to include "bin/" since avdmanager seems to go 2 dirs up # from that to find the SDK root? AVDMANAGER_OPTS="-Dcom.android.sdkmanager.toolsdir=${ANDROID_SDK_ROOT}/tools/bin/" \ "${ANDROID_SDK_ROOT}/tools/bin/avdmanager" "$@"

在使用项目脚本/install-android-tools之前必须运行一次的安装脚本:
#!/bin/bashset -e set -u set -xsource scripts/pathsmkdir -p "${ANDROID_SDK_ROOT}" mkdir -p "${ANDROID_AVD_HOME}" mkdir -p "${ANDROID_EMULATOR_HOME}"# Download sdkmanager, avdmanager etc. cd "${ANDROID_SDK_ROOT}" test -f commandlinetools-*.zip || \ wget -q 'https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip' unzip -q -u commandlinetools-*.zip cd ..# Ask sdkmanager to update itself ./scripts/sdkmanager --update# Install the emulator and tools yes | ./scripts/sdkmanager --install 'emulator' 'platform-tools'# Platforms ./scripts/sdkmanager --install 'platforms; android-21' ./scripts/sdkmanager --install 'platforms; android-29'# Install system images for our oldest and newest supported API versions yes | ./scripts/sdkmanager --install 'system-images; android-21; default; x86_64' yes | ./scripts/sdkmanager --install 'system-images; android-29; default; x86_64'# Create AVDs to run the system images echo no | ./scripts/avdmanager -v \ create avd \ -f \ -n "avd-21" \ -k "system-images; android-21; default; x86_64" \ -p ${ANDROID_SDK_ROOT}/avds/avd-21 echo no | ./scripts/avdmanager -v \ create avd \ -f \ -n "avd-29" \ -k "system-images; android-29; default; x86_64" \ -p ${ANDROID_SDK_ROOT}/avds/avd-29


    推荐阅读