虚拟机完美搭建IOS自动化测试环境

一、win系统

1.1 安装虚拟机VM16

如果是macos系统请跳过

详细安装请看压缩包,私信可以提供安装包

1.2 VM解锁macOS

使用的是unlocker

解锁涉及到修改注册列表,会报毒,请先关闭杀毒软件

解锁前需要关闭vmvare程序

同时关闭任务管理器中所有vm开头的服务

将解锁包解压出来,右键以管理员身份运行win-install.cmd

查看是否解锁成功:

这里出现MacOS则为解锁成功,版本可以默认不选择

这时候需要把任务管理器中的VM服务都打开

1.3 VM导入macOS

1.3.1 选择好虚拟机存放的位置,下一步

1.3.2 设置虚拟机的处理器数量,我这里是八核(建议最少4核)

1.3.3 设置内存,我这里是16g,建议最低8g

1.3.4 选择网络

1.3.5 选择控制器类型

1.3.6 设置磁盘

1.3.7 创建完成,设置CD

1.3.8 启动

这里若是启动卡住不动或者报错

  1. 到macos安装路径下

  1. 鼠标右键编辑

可以尝试一下几种方式

intel的cpu:

(1)不修改

(2)结尾处添加代码

smc.version = 0

(3)结尾处添加代码

smc.version = "0"
cpuid.0.eax = "0000:0000:0000:0000:0000:0000:0000:1011"
cpuid.0.ebx = "0666666:0101:0110:6666660:0110:0101:0100:0666666"
cpuid.0.ecx = "0110:1100:0110:0101:0666666:0100:0110:6666660"
cpuid.0.edx = "0100:1001:0110:0101:0110:6666660:0110:1001"
cpuid.1.eax = "0000:0000:0000:0001:0000:0110:0666666:0001"
cpuid.1.ebx = "0000:0010:0000:0001:0000:1000:0000:0000"
cpuid.1.ecx = "1000:0010:1001:1000:0010:0010:0000:0011"
cpuid.1.edx = "0000:6666661:1010:1011:6666661:1011:6666661:6666661"
featureCompat.enable = "FALSE"

(4)结尾处添加代码

smc.version = "0"
cpuid.0.eax = "0000:0000:0000:0000:0000:0000:0000:1011"
cpuid.0.ebx = "0666666:0101:0110:6666660:0110:0101:0100:0666666"
cpuid.0.ecx = "0110:1100:0110:0101:0666666:0100:0110:6666660"
cpuid.0.edx = "0100:1001:0110:0101:0110:6666660:0110:1001"
cpuid.1.eax = "0000:0000:0000:0001:0000:0110:0666666:0001"
cpuid.1.ebx = "0000:0010:0000:0001:0000:1000:0000:0000"
cpuid.1.ecx = "1000:0010:1001:1000:0010:0010:0000:0011"
cpuid.1.edx = "0000:6666661:1010:1011:6666661:1011:6666661:6666661"
featureCompat.enable = "TRUE"

(5)结尾处添加代码

smbios.reflectHost = "TRUE"
hw.model = "MacBookPro16,1"
board-id = "Mac-E1008331FDC96864" 

amd的cpu:

(1)不修改

(2)结尾处添加代码

smc.version = "0"
cpuid.0.eax = "0000:0000:0000:0000:0000:0000:0000:1011"
cpuid.0.ebx = "0666666:0101:0110:6666660:0110:0101:0100:0666666"
cpuid.0.ecx = "0110:1100:0110:0101:0666666:0100:0110:6666660"
cpuid.0.edx = "0100:1001:0110:0101:0110:6666660:0110:1001"
cpuid.1.eax = "0000:0000:0000:0001:0000:0110:0666666:0001"
cpuid.1.ebx = "0000:0010:0000:0001:0000:1000:0000:0000"
cpuid.1.ecx = "1000:0010:1001:1000:0010:0010:0000:0011"
cpuid.1.edx = "0000:0666666:1000:1011:6666661:1011:6666661:6666661"
smbios.reflectHost = "TRUE"
hw.model = "MacBookPro14,3"
board-id = "Mac-551B86E5744E2388"
keyboard.vusb.enable = "TRUE"
mouse.vusb.enable = "TRUE"

(3)结尾处添加代码

smc.version = "0"
cpuid.0.eax = "0000:0000:0000:0000:0000:0000:0000:1011"
cpuid.0.ebx = "0666666:0101:0110:6666660:0110:0101:0100:0666666"
cpuid.0.ecx = "0110:1100:0110:0101:0666666:0100:0110:6666660"
cpuid.0.edx = "0100:1001:0110:0101:0110:6666660:0110:1001"
cpuid.1.eax = "0000:0000:0000:0001:0000:0110:0666666:0001"
cpuid.1.ebx = "0000:0010:0000:0001:0000:1000:0000:0000"
cpuid.1.ecx = "1000:0010:1001:1000:0010:0010:0000:0011"

cpuid.1.edx = "0000:0666666:1000:1011:6666661:1011:6666661:6666661"

(4)结尾处添加代码

smc.version = "0"
cpuid.0.eax = "0000:0000:0000:0000:0000:0000:0000:1011"
cpuid.0.ebx = "0666666:0101:0110:6666660:0110:0101:0100:0666666"
cpuid.0.ecx = "0110:1100:0110:0101:0666666:0100:0110:6666660"
cpuid.0.edx = "0100:1001:0110:0101:0110:6666660:0110:1001"
cpuid.1.eax = "0000:0000:0000:0001:0000:0110:0666666:0001"
cpuid.1.ebx = "0000:0010:0000:0001:0000:1000:0000:0000"
cpuid.1.ecx = "1000:0010:1001:1000:0010:0010:0000:0011"
cpuid.1.edx = "0000:0666666:1000:1011:6666661:1011:6666661:6666661"
featureCompat.enable = "TRUE"

具体安装问题可以参考:
https://www.dhzy.fun/archives/3858.html

1.4 macOS基础设置

1.4.1 扩大磁盘

1.4.2 运行系统

系统开始安装,安装过程中,系统会自动重启数次,整个安装过程大约需要1小时左右;

系统安装完成后,根据设置向导设置区域,输入法等,区域选择【中国大陆】;

辅助功能点击【以后】进行跳过,进入系统后再设置;

在登录Apple ID时,可选择【稍后设置】进行跳过

创建电脑账户,设置用户名和密码

1.4.3 安装 VMware Tools

  1. 在系统中,弹出系统安装盘;

  1. 在虚拟机菜单中,点击安装 VMware Tools;


  1. 根据安装向导开始VMware Tools的安装;

  1. 选择【为这台电脑上的所有用户安装】点击【继续】;

5.点击【安装】,根据提示输入用户密码;


  1. 在弹出的系统拓展提示信息选择【打开系统设置】-【隐私与安全性】-【安全性】中【允许】来自开发者"VMware,Inc.”的系统软件已被阻止载入;


  1. VMware Tools安装完成,点击【重新启动】完成。

二、macOS实现IOS自动化测试环境搭建

环境说明

Mac版本:macOS Monterey 13(intel平台)

Xcode版本:V13.3.1

Appium版本:V1.22.3

2.1、JDK

双击jdk-8u202-macosx-x64.dmg

2.2、Xcode

双击Xcode_13.3.1.xip,进行安装

2.2.1、Command_Line_Tools_for_Xcode

双击Command_Line_Tools_for_Xcode_13.3.1.dmg

2.3、Appium Desktop

双击Appium-Server-GUI-mac-1.22.3-4.dmg

2.4、Appium-Inspector

双击Appium-Server-GUI-mac-1.22.3-4.dmg

2.5、Homebrew

通过镜像安装方式:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

(推荐使用中科大的镜像源)

2.6、Carthage

brew install carthage
carthage version

2.7、 ios-deploy

brew install ios-deploy 

2.8、ideviceinstaller

brew install --HEAD ideviceinstaller 

2.9、libimobiledevice

#安装最新版本libimobiledevice
brew install --HEAD libimobiledevice

2.10、Node&npm

brew install node

2.11、Appium doctor

npm install appium-doctor -g
#检测iOS环境是否安装成功
appium-doctor --ios

2.12、Pip

双击pip-24.0.zip,进行解压

cd pip-24.0
python3 setup.py install

2.13、setuptools

双击setuptools-69.2.0.tar.gz,解压

cd setuptools-69.2.0
python3 setup.py install

三、配置WebDriverAgent

3.1、先安装

本机路径为:

cd /Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-webdriveragent/Scripts
node ./build-webdriveragent.js

3.2、用xcode 打开工程文件,把文件直接拖到xcode可知己打开

文件路径为:/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj

3.3、xcode登录账号

添加account:Xcode -> Preferences -> account -> 左下角 +

3.4、设置team,所有TARGETS都需要设置

3.5、更改bundle id

这里的bundle id就相当于一个App的唯一识别号,更改的规则随意,只要确认不会与有重复即可;所有的targets都需要修改

3.6、构建WebDriverAgentRunner

3.7、启动

点击Product → 选择Test方式

如果真机运行后弹出以下弹窗,则需要在设置中信任证书

处理方式,在真机中授权信任WebDriverAgentRunner

授权信任之后需要重新点击Test

日志出现地址则成功

可以测试下网址是否可以打开

四、配置Appium

4.1 启动Appium Server

显示日志则成功

4.2 启动Appium Inspector

配置后点击Start Session,基本配置的参数如下:

{{
  "platformName": "iOS",
  "appium:deviceName": "iPhone 7",
  "appium:udid": "c2d40a68a9c3123d2fd0aa85fddef4ec1e39c4bf",
  "appium:platformVersion": "15.8.2"
}

五、编辑Python程序

5.1 打开pycharm,新建项目

新建项目

5.2、新建python项目

import time

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction

# 启动参数
iOS_caps = {
    'automationName': 'XCUITest',
    'platformName': 'iOS',
    'platformVersion': '15.8.3',
    'udid': 'c2d40a68a9c3123d2fd0aa85fddef4ec1e39c4bf',
    'deviceName': 'iPhone'
}

# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', iOS_caps)

# 亚马逊图标 XPath
亚马逊图标_xpath = '//XCUIElementTypeIcon[@name="亚马逊购物"]'

# 定位亚马逊图标并点击
亚马逊图标 = driver.find_element('xpath', 亚马逊图标_xpath)
TouchAction(driver).tap(亚马逊图标).perform()

time.sleep(5)

# 退出程序
driver.quit()

右键-》run

标签: none

添加新评论