likes
comments
collection
share

Mac下用Python写iOS自动化测试

作者站长头像
站长
· 阅读数 5

安装环境:

  • MacOS Mojave 10.14
  • Xcode10.1

安装libimobiledevice

网上查到的教程

移除所有的 iOS 设备,然后在终端输入以下代码

brew uninstall ideviceinstaller
brew uninstall libimobiledevice
brew install --HEAD libimobiledevice
brew link --overwrite libimobiledevice
brew install --HEAD  ideviceinstaller
brew link --overwrite ideviceinstaller
sudo rm -rf /var/db/lockdown/*

连接一台iOS设备,并信任,然后输入以下代码

sudo chmod -R 777 /var/db/lockdown/

查看手机信息

ideviceinfo -d

查看手机上安装的所有APP的 BundleId

ideviceinstaller -l

实际安装过程

遇到问题

在运行brew install --HEAD libimobiledevice这段代码时遇到问题:

Last 15 lines from /Users/joy/Library/Logs/Homebrew/libimobiledevice/01.autogen.sh:
checking dynamic linker characteristics... darwin18.0.0 dyld
checking how to hardcode library paths into programs... immediate
checking for pkg-config... /usr/local/opt/pkg-config/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for libusbmuxd >= 1.1.0... no
configure: error: Package requirements (libusbmuxd >= 1.1.0) were not met:

Requested 'libusbmuxd >= 1.1.0' but version of libusbmuxd is 1.0.10

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables libusbmuxd_CFLAGS
and libusbmuxd_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

看上去是libusbmuxd的版本不对。在网上查了很多方案以后,发现只能编译安装1.1.0版本。

编译安装

编译安装的时候需要依赖于很多库,不过由于我一开始使用brew install --HEAD libimobiledevice的时候,自动给我安装好了依赖的库,所以就不需要我再一一编译安装了。

编译安装libusbmuxd-1.1.0
git clone https://github.com/libimobiledevice/libusbmuxd.git
cd libusbmuxd
./autogen.sh
make
sudo make install
编译安装libimobiledevice
git clone https://github.com/libimobiledevice/libimobiledevice.git
cd libimobiledevice
./autogen.sh
make
sudo make install

在运行 ./autogen.sh 又遇到问题:

checking for openssl >= 0.9.8... no
configure: error: OpenSSL support explicitly requested but OpenSSL could not be found

我确认我安装的openssl版本都是1.0以上,但还是报openssl版本错误。在网上查到,依赖库可以使用openssl GnuTLS,所以我决定用另一个试一试。

brew install GnuTLS

安装完依赖库以后,输入以下代码重新编译安装

./autogen.sh --disable-openssl

又遇到一个新的问题:

configure: error: libgcrypt is required to build libimobiledevice with GnuTLS

需要安装libgcrypt

brew install libgcrypt

安装完成后,重新编译安装,OK,No problem~

编译安装ideveceinstaller

我本来想用 HomeBrew 安装,结果发现在 HomeBrew 安装 ideveceinstaller 时,它依赖于libimobiledevice,会自动下载某个稳定版的 libimobiledevice ,所以最好还是只能编译安装。

git clone https://github.com/libimobiledevice/ideviceinstaller.git
cd ideviceinstaller
./autogen.sh
make
sudo make install

自动化测试

安装WebDriverAgent

真机安装WebDriverAgent

ATX 文档 - iOS 真机如何安装 WebDriverAgent

GitHub上下载代码

git clone https://github.com/facebook/WebDriverAgent

运行初始化脚本

./Scripts/bootstrap.sh

该脚本会使用Carthage下载所有的依赖,使用npm打包响应的js文件

执行完成后,直接双击打开WebDriverAgent.xcodeproj这个文件。

设置好TeamProfileSigning Certificate以后,选择真机,Product -> Test

一切正常的话,手机上会出现一个无图标的WebDriverAgent应用,启动之后,马上又返回到桌面。这是很正常的不要奇怪。

此时控制台界面可以看到设备的IP。

通过上面给出的IP和端口,加上/status合成一个url地址。例如http://10.0.0.1:8100/status,然后浏览器打开。如果出现一串JSON输出,说明WDA安装成功了。

而inspector的地址是http://localhost:8100/inspector, inspector 是用来查看UI的图层,方便写测试脚本用的

使用终端替代Xcode

通常来说为了持续集成,能够全部自动化比较好一些

# 解锁keychain,以便可以正常的签名应用,
PASSWORD="replace-with-your-password"
security unlock-keychain -p $PASSWORD ~/Library/Keychains/login.keychain

# 获取设备的UDID
UDID=$(idevice_id -l | head -n1)

# 运行测试
xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination "id=$UDID" test

安装Python3 wda

pip install --pre facebook-wda

这个时候就可以来写python脚本了

import time
import wda

c = wda.Client(url='http://169.254.16.231:8100')

index = 0

def main():
    # 打开测试APP
    with c.session('com.jifen.qukan') as s:
        # 此时的s就是当前打开app的回话对象,我们可以通过它来操作app的内容
        # 具体的语法可以在wda的README.md里面找到,这里就不说了
		if s(type='Button', name=u'视频').exists:
			s(type='Button', name=u'视频').tap()
		
		time.sleep(3)

		while(True):
			pushSubVC(s)
			
def pushSubVC(s):
	global index
	es = s(label='home content ext').find_elements()
	i = 1
	for e in es:
		print(i)
		i+=1
	d = es[index]
	rect = d.bounds
	x = rect.x
	y = rect.y
	index=index+1
	s.click(x,y-20)
	time.sleep(63)
	if s(label='home content back white').exists:
		d = s(label='home content back white')
		d.tap()
	if s(type='ScrollView').exists:
		d = s(type='ScrollView')
		d.scroll(direction='down',distance=0.4)

if __name__ == '__main__':
    main()

参考文章

安装libimobiledevice

获取手机已安装app的bundleid

安装和使用ideviceinstaller时遇到的坑

libimobiledevice安装(Mac)

编译安装libimobiledevice

自动化测试

使用Python写iOS自动化测试

iOS WebDriverAgent 环境搭建

iOS+Python+Appium真机自动化测试实战

Python + Appium+ IOS自动化测试

MAC下搭建 appium+ios+python 自动化测试环境

转载自:https://juejin.cn/post/6953433667041493006
评论
请登录