iOS自动化测试入门

1,715 阅读4分钟

写在前面:ios自动化测试环境搭建真是实力劝退,太难了。坑,无数的坑。

iOS自动化测试前置基础知识

开发者中心

  • 开发者中心:developer.apple.com/
    • 提升ios平台知识

    • Team ID

    • 申请苹果签发开发和发布证书

      • 开发者证书申请:
        • 创建证书
        • 使用keychain的证书助手创建CSR文件
        • 发创建的证书给苹果
        • 苹果进行二次加密, 生成系统文件,用于开发的app上
    • 证书体系

      • Certificates:苹果签发
        • 开发证书:开发者使用
        • 发布证书:对外分发到app store或者使用ad-hoc模式分发
      • Provisioning Profiles: 描述文件
        • App ID:bundleID相关设置
        • Device List 可内测的真机列表
        • 分为开发和发布两种大类型
        • 发布分为ad-hoc、app store、enterprise级别
      • .app文件:编译后生成的项目打包代码
      • .ipa文件:经过签名后的打包文件,本质是zip文件
    • 发布方式

      • 个人或公司的ad-hoc方式
      • 企业级的in house方式

XCODE

  • 下载Xcode

  • 新建项目

  • 模拟器 perference - componets 各种模拟器,需下载:

  • build

    • bulid
    • 创建模拟器

ios环境搭建

appium(前面安装过就忽略)

  • 安装node

    brew install node

  • 安装appium

    npm install -g appium

  • 检查appium是否安装成功

    appium &

替换brew源

官网:brew.sh/

brew在mac上是很好用的工具,建议安装。安装很简单,执行以下命令。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

已经安装了brew的直接执行下面命令,对brew源进行替换

# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换Homebrew Bottles源
# 对于bash用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

#对于zsh用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

其他一些工具包的安装

  • libimobiledevice 一个跨平台的软件协议库和工具来与iOS®设备进行本地通信

    • 安装 brew install libimobiledevice
  • ios-deploy 一个使用命令行安装ios app到连接的设备的工具。(设备ios9.3以上需安装ios-deploy)

    • 安装 npm install -g ios-deploy
    • 或者 brew install ios-deploy
  • brew install --HEAD ideviceinstaller

    • 安装完成以上东西后,会生成idevice工具
      • idevice_id 显示连接设备
      • idevice_name
      • idevicescreenshot 获取设备截图
  • 安装appium-doctor

    appium-doctor是一个用于验证appium安装环境的工具,可以诊断出Node/iOS/Android环境配置方面的常见问题。

    npm install appium-doctor -g 安装appium-doctor

    appium-doctor --ios 指定--ios时只针对iOS环境配置进行检测

  • 安装xcode命令行工具

    xcode-select --install

  • 安装gcc

    brew install gcc

  • 安装或者升级usbmuxd

    #升级usbmuxd
    brew uninstall --ignore-dependencies usbmuxd
    brew install --HEAD usbmuxd
    brew unlink usbmuxd
    brew link usbmuxd
    #如果你的libreadline是8.0版本。这里做一个trick,让后面的安装能找到动态库
    cd /usr/local/opt/readline/lib
    ln -s libreadline.8.0.dylib libreadline.7.dylib
    

WebDriverAgent

这里看完后面一段话再决定怎么执行?

github:github.com/facebookarc…

  • git clone xxxx
  • cd /WebDriverAgent
  • git pull 拉取最新代码

实际上,我们在安装appium时,/usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent这个目录下是已经安装了WebDriverAgent的,可以直接使用。可以忽略上面去github拉取代码的步骤。

  • 进入webdriveragent目录下,执行命令:./Scripts/bootstrap.sh,使用bash -x ./Scripts/bootstrap.sh可以看到执行命令的细节,一定要记得执行这条,否则编译WebDriverAgent会报错。

  • 连接上你的设备

  • 编译webdriveragent

    • 方式一:使用xcode运行

      • 在/appium-webdriveragent下 open WebDriverAgent.xcodeproj Xcode会自动打开新的项目

      • WebDriverAgentLib

      • WebDriverAgentRunner

      • xcode菜单栏->Product->Destination->你的设备

      • xcode菜单栏->Scheme->WebDriverAgentRunner

      • xcode菜单栏->Product->Test

      • 设备上成功安装上webdriveragent视为成功

    • 方式二:使用命令行运行

      xcodebuild -project WebDriverAgent.xcodeproj \
      -scheme WebDriverAgentRunner \
      -destination 'platform=iOS Simulator,name=iPhone 6' \
      test
      

      如果是真机设备,更改为destination 'id=xxxx'

      • 成功后,会有`ServerURLHere->http://[SOME_IP]:8100<-ServerURLHere
    • Using the Inspector

      http://localhost:8100/inspector 展示界面信息,列举屏幕中常用坐标

      http://localhost:8100/source json格式的

ios自动化测试

ios平台的特殊性

  • 封闭
  • 文档稀少
  • 行业交流少

主流移动测试框架

  • appium
  • Calabash-IOS
  • KIF
  • XCTest
  • WebDriverAgent
  • Uiautomation(在xcode8后废弃)

ios真机测试

appium.io/docs/en/dri…

  • 查看ios app 元素的工具
    • xcode

      • 选择对应的真机或者模拟器
      • 使用准星点击要查看的元素

    • app-inspector

      macacajs.github.io/app-inspect…

      app-inspector是一个在浏览器中使用移动UI查看器,以树形视图查看UI,并自动生成XPath

      • 安装macaca-cli

        npm install macaca-cli -g

      • 安装app-inspector

        npm install app-inspector -g

      • 编译XCTestWD

        • 打开XCTestWD.xcodeproj

          open /usr/local/lib/node_modules/app-inspector/node_modules/xctestwd/XCTestWD/XCTestWD.xcodeproj
          
        • XCTestWD

        • XCTestWDUITests

        • xcode菜单栏->Product->Test

        • 安装XCTestWD在你的设备上视为成功