如何在CircleCI 2.0上正确运行Android UI测试?

时间:2017-07-31 19:44:29

标签: android circleci

尝试将我的Android项目升级为 CircleCI 2.0 。一切都很好,但是无法使用模拟器运行Android UI测试。

  • 记录com.android.builder.testing.api.DeviceException: No connected devices!
  • 我实际上已经下载了一个模拟器图像并进行了设置(因为CircleCI默认的Android Docker图像不附带模拟器):sdkmanager "system-images;android-22;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-22;default;armeabi-v7a" && (emulator64-arm -avd test -noaudio -no-boot-anim -no-window -accel on) &
  • adb devices不返回任何设备:(

2 个答案:

答案 0 :(得分:17)

示例circle.yml

version: 2
jobs:
  build:
    docker:
      - image: circleci/android:api-26-alpha
    steps:
      - run:
          name: Setup emulator
          command: sdkmanager "system-images;android-22;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-22;default;armeabi-v7a"
      - run:
          name: Launch emulator
          command: export LD_LIBRARY_PATH=${ANDROID_HOME}/emulator/lib64:${ANDROID_HOME}/emulator/lib64/qt/lib && emulator64-arm -avd test -noaudio -no-boot-anim -no-window -accel on
          background: true
      - run:
          name: Run Tests
          command: ./gradlew :demo:connectedAndroidTest

注意

  • 某些特定的架构图片并非由Google提供,例如: API级别26没有ARM EABI v7a系统映像,这就是我选择上面system-images;android-22;default;armeabi-v7a的原因。要查看哪些图像可用,请运行命令sdkmanager --list --verbose | grep system-images

  • 您需要使用LD_LIBRARY_PATHlib64路径设置环境变量qt,否则您可能会遇到ERROR: Could not load OpenGLES emulation library [lib64OpenglRender]error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory Exited with code 127。这是由于a bug from Android SDK

  • 要在CircleCI的后台运行命令,它不像通常的方式只是将&附加到命令的末尾,最终会被挂断(HUP)信号杀死。正确的方法是说background: true

答案 1 :(得分:1)

以下是一个示例config.yml,通过该示例,我设法使我的Android espresso测试使用回圈ci进行了一段时间。可能对某人有用。

config.yml的位置:在项目根目录下创建.circleci目录,并将config.yml保留在其中。

version: 2
jobs:
  build:
    working_directory: ~/code
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
         name: Chmod permissions
         command: sudo chmod +x ./gradlew
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
          name: Lists installed targets
          command: android list target
          environment:
          TERM: dumb
      - run:
          name: Show list of system-images
          command: sdkmanager --list --verbose | grep system-images
      - run:
          name: Setup Emulator
          command: sdkmanager "system-images;android-21;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-21;default;armeabi-v7a"
      - run:
          name: Launch Emulator
          command: |
                  cd ${ANDROID_HOME}/emulator;ls
                  export LD_LIBRARY_PATH=${ANDROID_HOME}/emulator/lib64:${ANDROID_HOME}/emulator/lib64/qt/lib
                  emulator -avd test -no-window -noaudio -no-boot-anim -no-window -accel on
          background: true
      - run:
          name: Wait emulator
          command: |
              circle-android wait-for-boot
              adb shell input keyevent 82
      - run:
          name: Run Espresso UI Tests
          command: ./gradlew :app:connectedDebugAndroidTest
      - store_artifacts:
          path: app/build/reports
          destination: reports
      - store_test_results:
          path: app/build/test-results