使用Appium做iOS自动化测试

环境检查

终端执行

appium-doctor --ios

提示什么没装,都装上
使用Appium做iOS自动化测试_第1张图片
按照fix方案安装
使用Appium做iOS自动化测试_第2张图片
如果没有安装 libimobiledevice,会导致Appium无法连接到iOS的设备,所以必须要安装,如果要在iOS10+的系统上使用appium,则需要安装ios-deploy

brew install libimobiledevice --HEAD 
brew install ios-deploy

使用appium desktop测试模拟器

正常Appium desktop启动,输入

{
    "platformName": "iOS",
    "platformVersion": "13.3",
    "deviceName": "iPhone 11 Pro Max",
    "automationName": "XCUITest",
    "app": "/path/to/my.app"
}

即可启动,开始测试,录制用例。

appium server+pytest+pycharm跑用例

启动appium server

appium -g ~/temp/appium_log

pycharm新建test_ios_demo.py,改造成pytest用例:


# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from time import sleep

from appium import webdriver

class TestIOS:

    def setup(self):
        caps = {}
        caps["platformName"] = "ios"
        caps["automationName"] = "xcuitest"
        caps["app"] = "/Users/damon/Library/Developer/Xcode/DerivedData/MTGSDKSample-agwqvppzltizubcxuvtmcxwezzxd/Build/Products/Debug-iphonesimulator/MTGSDKSample.app"
        caps["deviceName"] = "iPhone 11 Pro Max"
        caps["platformVersion"] = "13.3"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def test_iv(self):
        el1 = self.driver.find_element_by_accessibility_id("IV Ad")
        el1.click()
        el2 = self.driver.find_element_by_xpath("//XCUIElementTypeStaticText[@name=\"Load IV\"]")
        el2.click()
        el3 = self.driver.find_element_by_xpath("//XCUIElementTypeStaticText[@name=\"Show IV\"]")
        el3.click()
        sleep(20)

    def teardown(self):
        self.driver.quit()

常见问题

在终端执行 instruments -s device 报
xcode-select: error: tool ‘instruments’ requires Xcode, but active developer directory ‘/Library/Developer/CommandLineTools’ is a command line tools instance

解决方案:

#终端输入
sudo xcode-select --reset
sudo xcode-select --switch /Applications/Xcode.app

appium desktop报
Could not determine iOS SDK version: Could not get Xcode version. /Library/Developer/Info.plist does not exist on disk.

重启电脑解决


pycharm建了个iosdemo.py,将appium desktop录制的用例代码粘过去,run了下, 改造成pytest用例后,跑测试出问题。
使用Appium做iOS自动化测试_第3张图片
原因可能是IDE的问题,我是之前在iosdemo这个文件里写的是正常代码,run了下,IDE就记住了这个状态。即使我改成pytest用例后,点击 Run ‘pytest for iosdemo.TestiOS.test_iv’, IDE仍是当做正常构建执行的状态显示的;而非测试状态。

解决方案:
新建个文件,代码粘过去,执行test正常。

正确的IDE提示:
使用Appium做iOS自动化测试_第4张图片

ios 真机测试

  1. caps[app]不能是为模拟器构建的APP
    caps[“app”] = “/Users/damon/Library/Developer/Xcode/DerivedData/MTGSDKSample-agwqvppzltizubcxuvtmcxwezzxd/Build/Products/Debug-iphonesimulator/MTGSDKSample.app” 可以看到路径当中有simulator
    解决方案:
  • xcode连接真机,然后构建,从build信息中获取真机构建的.app路径替换caps[app]的值
  • 直接应用安装到真机上,删除caps[app],添加caps[bundleId],这样appium根据bundleId能找到真机上的应用启动它。(对于已经安装有被测APP的测试设备,可以忽略app参数,用bundleId启动)
  1. 测试真机,需要caps[‘xcodeOrgId’]和caps[‘xcodeSigningId’]

原文说明:http://appium.io/docs/en/drivers/ios-xcuitest-real-devices/
使用Appium做iOS自动化测试_第5张图片
xcodeDrgId的值是team id,teamID登录developer.apple.com,然后进入Membership页面可以看到。
使用Appium做iOS自动化测试_第6张图片

  1. appium寻找真机设备 ,所以还需要caps[‘udid’]

及时加了 caps[deviceName]和caps[platformVersion],可能还找不到设备,所以把caps[udid]加上。
udid的值,可以通过xcrun instruments -s命令获取
使用Appium做iOS自动化测试_第7张图片
所需的caps基本就是这些:

		caps["platformName"] = "ios"
        caps["automationName"] = "xcuitest"
  
        caps["deviceName"] = '"Damon"的 iPhone'
        caps["platformVersion"] = "12.3.1"
        caps['udid'] = '79e0381019eb6b006503ddfb63024b89b8cf5c69'
        caps["bundleId"] = "com.mobvista.ui.test" #对于已经安装有被测APP的测试模拟器,可以忽略app参数,用bundleId启动
        caps['xcodeOrgId'] = 'DJRJZKDWWH'
        caps['xcodeSigningId'] = "iPhone Developer"

运行,结果还报了错:

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
E       xcodebuild error message:
E       
E       2020-05-26 15:22:55.586 xcodebuild[79410:4096992] Error Domain=NSCocoaErrorDomain Code=260 "The file “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/damon/Library/Developer/Xcode/DerivedData/WebDriverAgent-ciegwgvxzxdrqthilmrmczmqvrgu/Build/Products/Debug-iphoneos/WebDriverAgentRunner-Runner.app, NSUnderlyingError=0x7facf3a91b40 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
E       Test session results, code coverage, and logs:
E       	/Users/damon/Library/Developer/Xcode/DerivedData/WebDriverAgent-ciegwgvxzxdrqthilmrmczmqvrgu/Logs/Test/Test-WebDriverAgentRunner-2020.05.26_15-22-55-+0800.xcresult
E       Testing started on '“Damon”的 iPhone'
Testing failed:
E       	No profiles for 'com.facebook.WebDriverAgentRunner.xctrunner' were found: Xcode couldn't find any iOS App Development provisioning profiles matching 'com.facebook.WebDriverAgentRunner.xctrunner'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild.
E       	WebDriverAgentRunner:
E       		WebDriverAgentRunner-Runner.app encountered an error (Failed to install or launch the test runner. (Underlying error: The file “WebDriverAgentRunner-Runner.app” couldn’t be opened because there is no such file. The file doesn’t exist. (Underlying error: The operation couldn’t be completed. No such file or directory)))
E       ** TEST EXECUTE FAILED **. Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md. Try to remove the WebDriverAgentRunner application from the device if it is installed and reboot the device.

错误消息的意思是WebDriverAgentRunner-Runner.app这个APP没有被签名,所以要对这个APP进行签名,才能驱动测试。

从终端appium的log里ctrl+f搜索 webdriveragent.xcodeproj, 看到这样一条路径/usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj

而且这一连串xcodebuild命令语句保存下来,后面有用
xcodebuild build-for-testing test-without-building -project /usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination id=79e0381019eb6b006503ddfb63024b89b8cf5c69 IPHONEOS_DEPLOYMENT_TARGET=12.3 -xcconfig /var/folders/v2/znwg27cj2glg95v4769852k40000gn/T/2020426-79764-k2k2is.7rbx/appium-temp.xcconfig GCC_TREAT_WARNINGS_AS_ERRORS=0 COMPILER_INDEX_STORE_ENABLE=NO

终端输入open path,打开这个项目

open /usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj

xcode会自动打开,

首先点击 图中1处,显示项目配置页。 然后点击2处,target切换到WebDriverAgentRunner,工具菜单点击 Signing&Capabilities, 选择Team,xcode自动开始签名。可以看到Status处标红,提示“com.facebook.WebDriverAgentRunner这个bundleId无法注册”,建议更改bundleId。

难搞的一点是我们看到 bundle Identifier是死的,没法编辑。


更改方法:菜单切到 Build Setting, 找到Product Bundle Identifier,然后点击更改。
然后再回到 Signing&Capabilities,我们看到已经签好名了。
使用Appium做iOS自动化测试_第8张图片

还记得之前建议保存的 那一连串xcodebuild命令吗,末尾加上-allowProvisioningUpdates 手动执行一下。

xcodebuild build-for-testing test-without-building -project /usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination id=79e0381019eb6b006503ddfb63024b89b8cf5c69 IPHONEOS_DEPLOYMENT_TARGET=12.3 -xcconfig /var/folders/v2/znwg27cj2glg95v4769852k40000gn/T/2020426-79764-k2k2is.7rbx/appium-temp.xcconfig GCC_TREAT_WARNINGS_AS_ERRORS=0 COMPILER_INDEX_STORE_ENABLE=NO -allowProvisioningUpdates

当看到如下log,说明已经好使了。

再次打开pycharm跑我们的用例代码,终于pass.

你可能感兴趣的:(Appium,iOS,Appium)