【问题标题】:iOS UI Tests iMessage App/ExtensioniOS UI 测试 iMessage 应用/扩展
【发布时间】:2017-10-24 08:21:44
【问题描述】:

我目前正在使用Fastlane Snapshot 自动为我的应用程序截取屏幕截图。这一切都基于 UI 测试。

我正在尝试将相同的功能添加到 iMessage 应用程序/扩展。

所以目前我有一个通过点击按钮、填写文本字段、截取屏幕截图等的测试。

完成所有这些后,我希望它关闭应用程序(单击主页按钮),打开 iMessage,与我的 iMessage 应用程序交互并在那里截取一些屏幕截图。

这可能吗?如果是这样,我怎样才能做到这一点?为这个应用程序自动截屏真是太棒了,我也希望能够为 iMessage 应用程序做到这一点。

【问题讨论】:

    标签: ios swift screenshot xcode-ui-testing imessage-extension


    【解决方案1】:

    Xcode 目前没有针对 iMessage 应用扩展的 UI 测试。但是您可以通过自己启动 Messages 并在 Messages 应用程序中查找元素来执行它。首先,您必须启动 Message 应用并打开对话:

    let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
    messageApp.terminate()
    messageApp.activate()
    messageApp.cells.firstMatch.tap()
    

    然后,您可以通过以下方式访问您的 iMessage 应用程序:

    // Replace appIndex by the position of your app in the iMessage bottom bar
    let appIndex = 2
    messageApp.collectionViews.descendants(matching: .cell).element(boundBy: appIndex).tap()
    

    当您的 iMessage 应用程序以展开模式打开时,您可以访问关闭按钮:

    let closeButton = messageApp.buttons.element(boundBy: 1)
    

    如果您想在用户发送消息然后打开它时测试您的 iMessage 应用程序,您可以这样做:

    // Send your message after it is inserted in the Messages app text field
    let sendButton = messageApp.buttons["sendButton"]
    waitForElementToExists(sendButton)
    sendButton.tap()
    
    // Tap on the iMessage first bubble
    let firstBubble = messageApp.collectionViews["TranscriptCollectionView"].cells.element(boundBy: 2)
    waitForElementToExists(firstBubble)
    firstBubble.tap()
    
    private func waitForElementToExists(_ element: XCUIElement) {
        let exists = NSPredicate(format: "exists == 1")
    
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 5, handler: nil)
    }
    

    【讨论】:

      【解决方案2】:

      使用 Xcode 9,您可以轻松切换到消息等其他应用程序。以下代码切换到消息,与应用内的元素交互,然后切换回您自己的应用。

      let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
      messageApp.terminate()
      messageApp.activate()
      
      messageApp.cells.staticTexts["Kate Bell"].tap()
      
      XCUIApplication().activate()
      

      【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2017-04-19
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2017-01-14
      相关资源
      最近更新 更多