iOS 用Python脚本自动化打测试ipa包
1.在Mac上安装python环境
此部分不专门讲解,在网上能搜到相关的文章,比较简单
2. 编写Python代码
import subprocess
import requests
def build_ios_app():
# 设置项目路径,相对路径,脚本和文件放在同一目录下
project_path = "./iOS-ehom-app-International.xcworkspace"#注意替换
# 用户输入配置(Debug/Release)和版本号
configuration = input("Enter configuration (Debug/Release): ").strip()
new_version = input("Enter new version number(_Test/_Pre/_Dev): ").strip()
# 更新版本号
update_version_command = [
"xcrun",
"agvtool",
"new-marketing-version",
new_version
]
subprocess.run(update_version_command)
# 使用xcodebuild打包
build_command = [
"xcodebuild",
"-workspace", project_path,
"-scheme", "iOS-ehom-app-International", # 请将 YourScheme 替换为你项目中的有效 Scheme 名称 #注意替换
"-configuration", configuration,
"-archivePath", "./iOSPack/EHOMInternational.xcarchive", #注意替换
"archive"
]
subprocess.run(build_command)
# 使用xcodebuild导出IPA
export_command = [
"xcodebuild",
"-exportArchive",
"-archivePath", "./iOSPack/EHOMInternational.xcarchive",#注意替换
"-exportOptionsPlist", "./exportOptions.plist", # 指定导出选项
"-exportPath", "./iOSPack", # 导出IPA的路径,当前目录
# "-exportName", "CustomName" 自定义的文件名
]
subprocess.run(export_command)
# 调用蒲公英API上传IPA文件
pgyer_upload(api_key="e9f8563c95a396ecea6b6e597e2a3341", file_path="./iOSPack/iOS-ehom-app-International.ipa")#注意替换
def pgyer_upload(api_key, file_path):
upload_url = "https://www.pgyer.com/apiv2/app/upload"
# 设置上传的参数
data = {
"_api_key": api_key
}
# 使用 requests 发送 POST 请求
with open(file_path, "rb") as file:
files = {"file": (file_path, file)}
response = requests.post(upload_url, data=data, files=files)
# 打印上传结果
print(response.text)
if __name__ == "__main__":
# 执行构建和导出IPA
build_ios_app()
3.把py文件放到项目同级目录下
4.新建exportOptions.plist文件也放到项目同级目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>development</string> <!-- 或者 ad-hoc 或 app-store,根据需要选择 -->
<key>provisioningProfiles</key>
<dict>
<key>com.your.bundle.identifier</key>
<!DOCTYPE 类似于KCNSS22SRZ>
<string>Your Provisioning Profile Name</string>
</dict>
<!-- 其他选项根据需要添加 -->
</dict>
</plist>
5.cd 到项目目录下
执行
python3 iOSPack.py
iOSPack 为python项目名
然后就会进行打包ipa到指定文件夹