用于简化与 XCUITest 方法交互的实用工具。
XCUIElement
扩展
assertExists()
assertExists(waitForAppToIdle: true)
assertNotExists()
assertExists(false)
assertIsHittable()
assertIsNotHittable()
assertIsHittable(false)
assertIsEnabled()
assertIsDisabled()
assertIsEnabled(false)
assertIsInteractive()
(存在,可点击,已启用)assertHasLabel("label")
assertContainsText("label")
assertHasValue("equatable value")
assertIsOn()
assertIsOff()
assertIsOn(false)
assertHasPlaceholder("placeholder")
assertIsEmpty()
(value == placeholderValue)assertHasTitle("title")
assertIsSelected()
assertIsNotSelected()
assertIsSelected(false)
tapWhenReady()
waitForInteractivity()
slowTypeText("text")
tap(withNormalizedOffset: .center)
drag(from: .top, to: .bottom, pressDuration: 1)
press(forDuration: 1, withNormalizedOffset: .center)
pressWhenReady(forDuration: 2)
tapIfExists()
stringValue
XCUIApplication
扩展
XCUIApplication.safari
XCUIApplication.springboard
XCUIApplication.messages
XCUIApplication.settings
assertIsInForeground()
assertIsNotInForeground()
assertIsInForeground(false)
moveToBackground()
XCUIElementQuery
扩展
assertHasCount(2)
assertNotExists()
assertHasCount(2...5)
, assertHasCount(..<3)
assertExists()
lastMatch
self[2]
first(where: { $0.label == "a" })
bannerNotifications
assertMatchedElements(perform: { element in /*...*/ })
CGVector
扩展
CGVector.topLeft
, CGVector.top
, CGVector.topRight
CGVector.left
, CGVector.center
, CGVector.right
CGVector.bottomLeft
, CGVector.bottom
, CGVector.bottomRight
offset(x: 0.2, y: 0.5)
XCUIElement.ElementType
扩展
bannerNotification
XCTestCase
扩展
run { ... }
run("Activity name") { ... }
以上所有的断言函数都有可选的消息作为最后一个参数,可以用于配置断言失败时显示的内容。例如:element.assertExists("My element should be visible")
。 此外,您可以通过修改 XCAppTestConfig.defaultTimeout
或通过 timeout
参数为每次调用配置全局断言超时。 例如:XCAppTestConfig.defaultTimeout = 3
, element.assertExists(timeout: 3)
。 有关详细信息,请参阅文档。
这是一个来自我的应用程序的简短示例,使用了这个库。 在测试中,我检查是否可以导航到“高级功能”屏幕,验证最重要的数据是否可见,并检查是否可以离开该屏幕。
请注意,某些按钮是通过枚举案例而不是原始字符串标识的。 您可以查看 类型安全的标识符 提示,了解如何实现这一点。
func testOpenClosePremiumScreen() throws {
try launch(configuration: .init(premiumUnlocked: false))
app.buttons[.toggleBottomSheetButton].tap()
app.buttons["Unlock Premium"].tapWhenReady()
run("Verify screen content") {
assertPremiumScreenIsVisible()
app.buttons[.unlockFeaturesButton].assertIsEnabled().assertContainsText("Lifetime access")
app.buttons["Restore Purchase"].assertIsEnabled()
}
app.buttons["Dismiss"].tap()
app.staticTexts["Pipilo Premium"].assertNotExists()
}
Package.swift
文件中的 dependencies 数组中.package(url: "https://github.com/Tunous/XCAppTest.git", .upToNextMajor(from: "0.14.0")),
XCAppTest
添加为您的**tests**目标的依赖项.target(name: "MyAppTests", dependencies: ["XCAppTest"]),
import XCAppTest
。将 https://github.com/Tunous/XCAppTest.git 添加到 Xcode 项目的 Swift Packages 列表中,并将其作为您 **tests** 目标的依赖项包含在内。