有没有办法在 Swift XCTest UI 的测试之间重置应用程序?

2024-05-26

XCTest 中是否有 API 调用,我可以将其放入 setUP() 或tearDown() 中以在测试之间重置应用程序?我查看了 XCUIApplication 的点语法,我看到的只是 .launch()

或者有没有办法在 Swift 中调用 shell 脚本?然后我可以在测试方法之间调用 xcrun 来重置模拟器。


You can add a "Run Script" phase to build phases in your test target to uninstall the app before running unit tests against it, unfortunately this is not between test cases, though.

/usr/bin/xcrun simctl uninstall booted com.mycompany.bundleId

Update


在测试之间,您可以删除该应用程序在拆卸阶段通过跳板。尽管如此,这确实需要使用 XCTest 的私有标头。 (标头转储可从Facebook 的 WebDriverAgent 在这里 https://github.com/facebook/WebDriverAgent/tree/master/PrivateHeaders/XCTest.)

以下是 Springboard 类中的一些示例代码,用于通过点击并按住从 Springboard 删除应用程序:

#斯威夫特 4:

import XCTest

class Springboard {

    static let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    
    /**
     Terminate and delete the app via springboard
     */
    class func deleteMyApp() {
        XCUIApplication().terminate()
        
         // Force delete the app from the springboard
        let icon = springboard.icons["Citizen"]
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.press(forDuration: 1.3)
        
            // Tap the little "X" button at approximately where it is. The X is not exposed directly
            springboard.coordinate(withNormalizedOffset: CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX, dy: (iconFrame.minY + 3) / springboardFrame.maxY)).tap()
        
            springboard.alerts.buttons["Delete"].tap()
        }
    }
 }

#斯威夫特 3-:

import XCTest

class Springboard {

    static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
    
    /**
     Terminate and delete the app via springboard
     */
    class func deleteMyApp() {
        XCUIApplication().terminate()
        
        // Resolve the query for the springboard rather than launching it
        springboard.resolve()
        
        // Force delete the app from the springboard
        let icon = springboard.icons["MyAppName"]
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.pressForDuration(1.3)
            
            if #available(iOS 13.0, *) {
                springboard.buttons["Remove App"].tap()
                springboard.alerts.buttons["Delete App"].tap()
                springboard.alerts.buttons["Delete"].tap()
            } else {
            
                // Tap the little "X" button at approximately where it is. The X is not exposed directly
                let xPosition = CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX,
                                     dy: (iconFrame.minY + 3) / springboardFrame.maxY)
                springboard.coordinate(withNormalizedOffset: xPosition).tap()
                springboard.alerts.buttons["Delete"].tap()
            }
        }
    }
 }

进而:

override func tearDown() {
    Springboard.deleteMyApp()
    super.tearDown()
}

私有标头已导入到 Swift 桥接标头中。您需要导入:

// Private headers from XCTest
#import "XCUIApplication.h"
#import "XCUIElement.h"

Note:从 Xcode 10 开始,XCUIApplication(bundleIdentifier:)现在由Apple公开,私有标头是不再需要.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法在 Swift XCTest UI 的测试之间重置应用程序? 的相关文章

  • 在 Swift 中向 UIButton 添加字符串属性

    如何将字符串属性与UIButton在斯威夫特 我不希望该字符串显示为按钮文本 而只是将其作为标识符或键分配给按钮 这是我到目前为止所拥有的 func createAnswerButtons var index Int for index 0
  • 动态增加UITableViewCell中UILabel的高度?

    我有一个 UITableView 其中显示一个自定义单元格 我的单元格有两个标签和一个视图 如下图所示 我已经像这样给出了左视图的约束 项目标签限制 中心视图约束 右视图的约束 I am using a bean class to stor
  • 调整 SKShapeNode 的大小

    如何调整 SKShapeNode 的大小 到目前为止我尝试过的 调整框架大小 box frame width 10 给出错误Cannot assign to the result of this expression SKAction le
  • 在 Android 上使用 opus 剪辑从 IOS 发送的声音

    我正在 IOS 中从 audioUnit 录制音频 用 opus 编码字节并通过 UDP 将其发送到 android 端 问题是播放的声音有点削波 我还通过将原始数据从 IOS 发送到 Android 来测试声音 效果非常完美 我的 Aud
  • tableView.dequeueReusableCellWithIdentifier() 导致应用程序挂起

    原帖 我们最近将我们的应用程序转换为 Swift 2 0 和 iOS9 我看到的一个奇怪的问题是调用 tableView dequeueReusableCellWithIdentifier 会导致应用程序挂在模拟器中 The code fu
  • 如何在 iOS 上压缩 Realm DB?

    我想定期压缩 iOS 上的 Realm 实例以回收空间 我认为该过程是将数据库复制到临时位置 然后将其复制回来并使用新的default realm 文件 我的问题是Realm 其行为就像单例并回收对象 因此我无法真正关闭它并告诉它打开新的
  • 如何使 SFSpeechRecognizer 在 macOS 上可用?

    我正在尝试使用 Apple 的语音框架在 macOS 10 15 1 上进行语音识别 在 macOS 10 15 之前 语音识别仅在 iOS 上可用 但根据文档 https developer apple com documentation
  • Firestore 更新后仅获取文档一次

    我有一个 tableView 它从 Firestore 集合中获取所有文档 并且我只想在用户刷新 tableView 后将最后一个文档添加到 Firestore 时获取一次 然后我想删除侦听器 以便当用户刷新 tableView 时仅获取文
  • 由 Xcode 机器人运行时,在文本视图中键入文本时 UI 测试失败

    我有以下 XCTest UI 测试 将文本键入文本视图 let textView app textViews elementBoundByIndex 0 textView tap textView typeText Hello world
  • 带闭包的默认属性值使编译器重新编译所有文件

    This source https developer apple com library content documentation Swift Conceptual Swift Programming Language Initiali
  • 我如何从子视图导航到 mainviewcontroller

    我刚刚开始使用 swift 我创建了一个子视图 上面有一个按钮 我想使用该按钮将我带到我的主视图控制器 我对不同的按钮使用了相同的功能 但是在同一文件中具有一个功能允许该按钮工作 代码如下 var playAgainButton UIBut
  • 当我输入字符时,SwiftUI 中的 TextField 失去焦点

    当我在文本字段中输入字符时遇到问题 在练习集视图 我必须重新单击文本框才能输入另一个字符 如果我从文本字段中删除绑定 我可以流畅地输入文本 我认为这与我的演讲者班级和更新集函数重新创建一个集合实例 因为我必须替换数组中两层深处的一些值 Co
  • Firebase 身份验证问题 - 通过电子邮件地址检查用户是否存在

    我在 Firebase 上创建了一个帐户 它有效 但现在我想阻止人们使用已存在的电子邮件地址创建帐户 这是代码 DatabaseManager shared userExists with email completion weak sel
  • 当 isUserInteractionEnabled false 时,SKSpriteNode 不会让触摸通过

    我正在尝试在 SpriteKit 中创建一个覆盖层 方法是使用SKSpriteNode 但是 我希望触摸穿过覆盖层 所以我设置isUserInteractionEnabled为假 然而 当我这样做时 SKSpriteNode似乎仍然吸收所有
  • 将 UIToolBar 添加到所有键盘(swift)

    我正在尝试以尽可能少的重复次数将自定义 UIToolBar 添加到我的所有键盘中 我目前的做法要求我将代码添加到所有 viewDidLoads 中 并将每个文本字段的委托分配给我正在使用的 viewController 我尝试创建自己的 U
  • 如何使用 CNContacts 快速获取手机号码?

    我有一些代码可以检索用户联系人中的所有电话号码 但只想过滤掉手机号码 目前 我只是通过将第一个数字为 或第二个数字为 7 的数字添加到数组中来实现此目的 如下所示 func findContacts gt CNContact let key
  • 从未调用过交互式委托方法

    我想在 ViewController 1 和 NavigationViewController 2 之间进行交互式转换 NavigationController 通过按钮调用 因此呈现时没有交互转换 它可以通过按钮或 UIPanGestur
  • Swift:长按手势识别器 - 检测轻击和长按

    我想连接一个动作 如果手势是点击 它会以特定的方式为对象设置动画 但如果按下持续时间超过 0 5 秒 它会执行其他操作 现在 我刚刚连接了动画 我不知道如何区分长按和点击 如何访问新闻持续时间以实现上述目的 IBAction func ta
  • AVAudioPlayer 无法从网站播放 m4a 或 mp3 文件类型

    我试图在我的应用程序中找到一个仅纯 m4a 声音的 URL 我有音频的 URL 理论上可以下载它 然后 使用下载的文件URL到声音 我尝试使用AVAudioPlayer播放它 但它不播放任何声音 这是我的代码 在 URL 检索函数中 我调用
  • Xcode 异步单元测试在主线程上等待

    我正在尝试使用 Xcode 中的单元测试来测试一些异步代码 但主线程被阻塞 问题在于 某些正在测试的代码期望从 iOS 类 AVFoundation 接收回调 但是 AVFoundation 类似乎只会在主线程上回调 问题是 如果我正在进行

随机推荐