Skip to content

[iOS][go-ios]WDA 长时间自动化测试保活 #35

@nzcv

Description

@nzcv

如何让 WDA 在长时间自动化测试中长驻后台(go-ios 方案)

场景:使用 go-ios 进行长时间(跨小时/跨天)的 iOS 自动化测试,需要 WebDriverAgent (WDA) 保持存活,断连后自动恢复。

一、go-ios 启动 WDA

ios runwda \
  --bundleid=com.facebook.WebDriverAgentRunner.xctrunner \
  --testrunnerbundleid=com.facebook.WebDriverAgentRunner.xctrunner \
  --xctestconfig=WebDriverAgentRunner.xctest \
  --udid=<UDID>

WDA 默认监听设备端 8100,本机访问需端口转发:

ios forward 8100 8100 --udid=<UDID>

二、iOS 17+ 必读:隧道是命根子

iOS 17 及以上,所有服务(含 runwda)都要走 RSD 隧道,隧道断 = WDA 全挂。需要先用 root 权限常驻隧道守护进程:

sudo ios tunnel start
ios tunnel ls

长跑务必把 ios tunnel start 当成独立常驻服务来保活,它比 WDA 本身更基础。iOS 16 及以下不需要隧道。

三、长跑守护脚本(go-ios 版看门狗)

管三件事:保活隧道(iOS17+)→ 拉起 WDA → 健康检查自动重启

#!/usr/bin/env bash
# wda_guard_goios.sh —— go-ios WDA 长跑守护
set -u

UDID="<你的设备UDID>"
WDA_PORT=8100
BUNDLE="com.facebook.WebDriverAgentRunner.xctrunner"
IOS17_PLUS=true          # iOS17+ 设为 true,16及以下设为 false
LOG="wda_guard.log"

log() { echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }

start_tunnel() {
  $IOS17_PLUS || return 0
  if ! ios tunnel ls 2>/dev/null | grep -q "$UDID"; then
    log "启动隧道 (需要 sudo)..."
    sudo ios tunnel start >> "$LOG" 2>&1 &
    sleep 5
  fi
}

start_wda() {
  log "拉起 WDA..."
  ios runwda \
    --bundleid=$BUNDLE \
    --testrunnerbundleid=$BUNDLE \
    --xctestconfig=WebDriverAgentRunner.xctest \
    --udid="$UDID" >> "$LOG" 2>&1 &
  sleep 3
  log "建立端口转发..."
  ios forward $WDA_PORT $WDA_PORT --udid="$UDID" >> "$LOG" 2>&1 &
  sleep 2
}

cleanup_wda() {
  pkill -f "ios runwda.*$UDID" 2>/dev/null
  pkill -f "ios forward.*$UDID" 2>/dev/null
}

# 初始化
start_tunnel
start_wda

fail=0
while true; do
  start_tunnel   # iOS17+ 先确保隧道在
  if curl -s --max-time 5 "http://localhost:${WDA_PORT}/status" | grep -q '"state"'; then
    fail=0
  else
    fail=$((fail+1))
    log "WDA 健康检查失败 ($fail/3)"
    if [ "$fail" -ge 3 ]; then
      log "WDA 失联,重启..."
      cleanup_wda
      sleep 3
      start_wda
      fail=0
    fi
  fi
  sleep 10
done

用法:

chmod +x wda_guard_goios.sh
nohup ./wda_guard_goios.sh &   # 或放进 tmux

四、设备侧配合(缺一不可)

  • 自动锁定 → 永不关低电量模式全程充电
  • 引导式访问锁定 WDA 前台(若测试不需切到被测 App,这是单机最稳的招)。
  • WDA 用付费/企业签名安装为常驻 App,避免 7 天临时签名过期中断长跑。

五、Appium(如果套了 Appium)

即使底层用 go-ios,Appium 侧也建议:

{
  "appium:newCommandTimeout": 0,
  "appium:usePrebuiltWDA": true,
  "appium:waitForQuiescence": false
}

推荐落地组合

  1. 单机长跑、不需切前台被测 App → 引导式访问 + 付费签名 + Appium newCommandTimeout:0,最省心。
  2. 需要操作被测 App / 真机农场 → go-ios 拉起 + 守护脚本看门狗 + 付费签名 + usePrebuiltWDA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions