ios 自动化测试:解决 app 之间交互 之 使用 xcuitest

关键词: Xcodebuild ,xcuitest

问题:模拟手动点击 imessage 中的 https link,link 跳转到被测试app 的特定页面。

解决思路:

方法1,使用 url scheme link,即类似于 

  • 打开 Safari 浏览器
    x-web-search://
  • 备忘录
    mobilenotes://
  • 打开地图
    map: //
  • 给某人发送短信:
    sms://手机号

  通过 

ios_driver.execute_script('mobile: activateApp', {'bundleId': 'com.apple.MobileSMS'})
ios_driver.get(scheme_link_address)

  结果,app 是native app,不能按照预期打开link,跳转到指定页面。

 

方法2,appium 测试 ios app,是借用了 xcuitest。那么appium 可以调用uiautomater2 来驱动android 的测试,大概率也可以调用 xcuitest 来操作 ios app。

   第一步,创建 xcode project

       打开Xcode New-> Project, 选择 ios app;下一步输入 product name,勾选 Include tests 选项

      

 

 

   第二步,编写相应 code

      使用 swift 来编写代码,打开xxxxUITests,在 testExample 方法中添自己需要的代码

 

        或者可以使用 Xcode 底部的红色圆点脚本录制功能,选好了xcode运行的设备,或者模拟器后,xcode 会自动将屏幕的操作录制为脚本。

      

 

 

   第三步,配置可以灵活传递的 url 参数

      上一步中的变量 meeting_link 是一个需要用户灵活配置的可变参数,在project 中写成hard code 后,project 被编译后,就不能在实际测试中更改。所以最好是让这一些可以在测试脚本中指定。

      Manage scheme -> 添加 xxxUITests scheme。

      Edit scheme,编辑 xxxUITests scheme,选择 Test,添加 Environment variables, 取消 Use the run action‘s argument,选择 Expand variable based on 为当前的 xxxUITests

      

    此处的变量将由编译时从 command line 指定

 

 

   第四步,编译成可供脚本中调用运行的package

    首先确定写的脚本没有错误,可以使用Xcode 的 Product -Test 指定设备后,先跑一遍。下面将通过 xcode 的command line 来将 project 编译为 .xctestrun 文件,类似于 “xxxxUITests_iphoneos15.0-arm64.xctestrun”这样的文件,destination设置的不同,文件名也会有不同。

    xcodebuild command line 需要使用 .xcworkspace, 首先新建新一个 workspace,通过 File -> add file, 选择之前项目创建的 .xcodeproj 文件,将project 添加到workspace。

从terminal 中 编译:

$ xcodebuild build-for-testing -scheme xcTestSMSUITests -destination name=iPhoneXSMAX -workspace xcTestSMS.xcworkspace -derivedDataPath '/xxx/Auto/xctest/' -allowProvisioningUpdates MEETING_LINK='https://helloworld/meeting'

指定 -scheme 为 xxxUITests 即刚刚设置过变量的;指定 -workspace 即自己创建的workspace名字;-derivedDataPath  指定产生的 .xctestrun 文件的保存位置 ;-destination 脚本要测试的设备或模拟器。

编译后,在指定的 path 下,会生成 products -> “xxxxUITests_iphoneos15.0-arm64.xctestrun”这样的文件,将会在下一步中使用。

 

第五步,在command line 运行测试

$ xcodebuild test-without-building -xctestrun '/Auto/xyyxctest/Build/Products/xxxxUITests_iphoneos15.0-arm64.xctestrun' -destination 'id=00008000-012345E'

指定测试的设备或模拟器 -destination

 

 参考文档:https://www.jianshu.com/p/183f506414a7 

      https://www.iplaysoft.com/p/ios-url-scheme


posted @ 2022-04-02 17:07  xiaoyanxuzi  阅读(1004)  评论(0编辑  收藏  举报