You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium测试在Amazon Device Farm执行卡在npm install *.tgz步骤

问题概述

基于WebdriverIO、Appium框架开发的安卓端自动化测试脚本在本地环境可正常运行,部署到Amazon Device Farm自定义测试环境执行时,流程卡在npm install *.tgz步骤无进展。
当前选用的环境版本为Node 14.19.1、Appium 1.22.2,使用的测试环境配置文件内容如下:

# Phases are collection of commands that get executed on Device Farm.
phases:
  # The install phase includes commands that install dependencies that your tests use.
  # Default dependencies for testing frameworks supported on Device Farm are already installed.
  install:
    commands:
      # By default the Node.js version installed is 11.4.0, however we enable you to change it
      # using the Node version manager (nvm). An example "nvm" command below changes the version to 14.19.1
      - export NVM_DIR=$HOME/.nvm
      - . $NVM_DIR/nvm.sh
      - nvm install 14.19.1

      # Unpackage and install the node modules that you uploaded in the test phase.
      - echo "Navigate to test package directory"
      - cd $DEVICEFARM_TEST_PACKAGE_PATH
      - npm install *.tgz
      
      # This test execution environment uses Appium version 1.9.1 by default, however we enable you to change it using
      # the Appium version manager (avm). An example "avm" command below changes the version to 1.22.2.
      # For your convenience, we have preinstalled the following versions: 1.22.2, 1.19.0, 1.18.3, 1.18.1, 1.18.0, 1.17.1, 1.16.0, 1.15.1, 1.14.2, 1.14.1, and 1.13.0.
      # To use one of these Appium versions, change the version number in the "avm" command below to your desired version:
      - export APPIUM_VERSION=1.22.2
      - avm $APPIUM_VERSION
      - ln -s /usr/local/avm/versions/$APPIUM_VERSION/node_modules/.bin/appium  /usr/local/avm/versions/$APPIUM_VERSION/node_modules/appium/bin/appium.js

  # The pre-test phase includes commands that setup your test environment.
  pre_test:
    commands:
      # We recommend starting appium server process in the background using the command below.
      # Appium server log will go to $DEVICEFARM_LOG_DIR directory.
      # The environment variables below will be auto-populated during run time.
      - echo "Start appium server"
      - >-
        appium --log-timestamp
        --default-capabilities "{\"deviceName\": \"$DEVICEFARM_DEVICE_NAME\", \"platformName\":\"$DEVICEFARM_DEVICE_PLATFORM_NAME\",
        \"app\":\"$DEVICEFARM_APP_PATH\", \"udid\":\"$DEVICEFARM_DEVICE_UDID\", \"platformVersion\":\"$DEVICEFARM_DEVICE_OS_VERSION\",
        \"chromedriverExecutable\":\"$DEVICEFARM_CHROMEDRIVER_EXECUTABLE\"}"
        >> $DEVICEFARM_LOG_DIR/appiumlog.txt 2>&1 &

      - >-
        start_appium_timeout=0;
        while [ true ];
        do
            if [ $start_appium_timeout -gt 60 ];
            then
                echo "appium server never started in 60 seconds. Exiting";
                exit 1;
            fi;
            grep -i "Appium REST http interface listener started on 0.0.0.0:4723" $DEVICEFARM_LOG_DIR/appiumlog.txt >> /dev/null 2>&1;
            if [ $? -eq 0 ];
            then
                echo "Appium REST http interface listener started on 0.0.0.0:4723";
                break;
            else
                echo "Waiting for appium server to start. Sleeping for 1 second";
                sleep 1;
                start_appium_timeout=$((start_appium_timeout+1));
            fi;
        done;

  # The test phase includes commands that start your test suite execution.
  test:
    commands:
      # Go into the root folder containing your source code and node_modules
      - echo "Navigate to test source code"
      # Change the directory to node_modules folder as it has your test code and the dependency node modules.
      - cd $DEVICEFARM_TEST_PACKAGE_PATH/node_modules/*

      - echo "Start Appium Node test"
      # Enter the command below to start the tests . The comamnd should be similar to what you use to run the tests locally.
      # For e.g. assuming you run your tests locally using command "node YOUR_TEST_FILENAME.js.", enter the same command below:
      - npm run test:adf:android

  # The post test phase includes are commands that are run after your tests are executed.
  post_test:
    commands:

# The artifacts phase lets you specify the location where your tests logs, device logs will be stored.
# And also let you specify the location of your test logs and artifacts which you want to be collected by Device Farm.
# These logs and artifacts will be available through ListArtifacts API in Device Farm.
artifacts:
  # By default, Device Farm will collect your artifacts from following directories
  - $DEVICEFARM_LOG_DIR
排查思路与解决方案

npm install *.tgz步骤卡住通常由网络、版本兼容、编译阻塞、包结构错误四类原因导致,按以下顺序排查即可:

  • 网络访问超时导致挂起
    Device Farm测试实例默认访问公网npm源时存在网络限制、连接超时问题,且默认npm执行时不输出详细日志,会表现为无进展卡住。
    处理方式:
    1. 安装前配置可稳定访问的npm源,同时增加日志输出参数、关闭非必要交互逻辑,示例命令:
      npm config set registry https://registry.npmmirror.com
      npm install *.tgz --no-audit --no-fund --loglevel verbose
      
    2. 优先采用离线打包方式:本地在对应Node版本下执行npm install --production完成所有依赖安装后,再将代码、node_modules一并打包为tgz上传,安装时增加--offline参数完全走本地依赖,避免云端拉取。
  • Node版本切换未生效导致npm版本不兼容
    现有配置仅执行了nvm install 14.19.1,未主动切换到对应版本,可能仍在使用默认Node 11自带的npm版本,和本地打包tgz时的npm版本不兼容触发进程挂起。
    处理方式:在nvm安装版本后增加版本切换、校验逻辑:
    nvm install 14.19.1
    nvm use 14.19.1
    node -v
    npm -v
    
  • 原生模块编译阻塞
    如果项目依赖包含需要node-gyp编译的原生模块,Device Farm默认环境缺少python、make、g++等编译依赖,编译过程会无提示卡住。
    处理方式:
    1. 优先在本地和Device Farm一致的Linux x64环境、Node 14.19.1版本下提前编译好所有原生模块,再打包上传,避免云端编译。
    2. 若需要云端编译,在install阶段先执行apt-get update && apt-get install -y python make g++安装编译依赖,必须加-y参数避免交互确认阻塞。
  • tgz包结构不符合要求
    如果当前路径下存在多个tgz包、或者上传的tgz包缺失package.json、路径嵌套错误,会触发npm安装通配符匹配的已知bug导致进程挂起。
    处理方式:在执行安装命令前增加目录检查,确认包路径正确:
    cd $DEVICEFARM_TEST_PACKAGE_PATH
    ls -la *.tgz
    
    确保当前路径下仅存在一个上传的测试tgz包,无其他冗余压缩文件。
修正后install阶段配置示例
install:
  commands:
    - export NVM_DIR=$HOME/.nvm
    - . $NVM_DIR/nvm.sh
    - nvm install 14.19.1
    - nvm use 14.19.1
    # 校验node、npm版本
    - node -v
    - npm -v
    # 配置npm源
    - npm config set registry https://registry.npmmirror.com
    - echo "Navigate to test package directory"
    - cd $DEVICEFARM_TEST_PACKAGE_PATH
    # 检查tgz包
    - ls -la *.tgz
    # 带详细日志执行安装
    - npm install *.tgz --no-audit --no-fund --loglevel verbose
    
    - export APPIUM_VERSION=1.22.2
    - avm $APPIUM_VERSION
    - ln -s /usr/local/avm/versions/$APPIUM_VERSION/node_modules/.bin/appium  /usr/local/avm/versions/$APPIUM_VERSION/node_modules/appium/bin/appium.js

内容的提问来源于stack exchange,提问作者Varun Bhardwaj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 07:15:40