Skip to content

Commit 816d238

Browse files
committed
Improve assertion logs
Run all assertions in named contexts to improve xcresult logs in Xcode
1 parent 7e1bfe5 commit 816d238

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

Sources/XCAppTest/AssertionUtils.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ import XCTest
22

33
extension XCUIElement {
44
func assert(
5+
named assertionName: String,
56
condition: @escaping (XCUIElement) -> Bool,
67
_ message: @autoclosure () -> String,
78
file: StaticString,
89
line: UInt
910
) {
10-
XCAppTest.assert(condition: condition, on: self, message: message, file: file, line: line)
11+
XCTContext.runActivity(named: assertionName) { _ in
12+
XCAppTest.assert(condition: condition, on: self, message: message, file: file, line: line)
13+
}
1114
}
1215
}
1316

1417
extension XCUIElementQuery {
1518
func assert(
19+
named assertionName: String,
1620
condition: @escaping (XCUIElementQuery) -> Bool,
1721
_ message: @autoclosure () -> String,
1822
file: StaticString,
1923
line: UInt
2024
) {
21-
XCAppTest.assert(condition: condition, on: self, message: message, file: file, line: line)
25+
XCTContext.runActivity(named: assertionName) { _ in
26+
XCAppTest.assert(condition: condition, on: self, message: message, file: file, line: line)
27+
}
2228
}
2329
}
2430

Sources/XCAppTest/XCUIApplication+App.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ extension XCUIApplication {
2020
file: StaticString = #file,
2121
line: UInt = #line
2222
) {
23-
XCTAssertTrue(wait(for: .runningForeground, timeout: 8), message() ?? "Application should be in foreground", file: file, line: line)
23+
XCTContext.runActivity(named: "Assert \(self) is in foreground") { _ in
24+
XCTAssertTrue(wait(for: .runningForeground, timeout: 8), message() ?? "Application should be in foreground", file: file, line: line)
25+
}
2426
}
2527

2628
/// Asserts that the application is not currently in foreground.
@@ -34,7 +36,15 @@ extension XCUIApplication {
3436
file: StaticString = #file,
3537
line: UInt = #line
3638
) {
37-
XCAppTest.assert(condition: { $0.state != .runningForeground }, on: self, message: { message() ?? "Application should not be in foreground" }, file: file, line: line)
39+
XCTContext.runActivity(named: "Assert \(self) is NOT in foreground") { _ in
40+
XCAppTest.assert(
41+
condition: { $0.state != .runningForeground },
42+
on: self,
43+
message: { message() ?? "Application should not be in foreground" },
44+
file: file,
45+
line: line
46+
)
47+
}
3848
}
3949

4050
// MARK: - Performing actions
@@ -45,7 +55,9 @@ extension XCUIApplication {
4555
public func moveToBackground(
4656
_ message: @autoclosure () -> String? = nil
4757
) {
48-
XCUIDevice.shared.press(.home)
49-
_ = wait(for: .runningBackground, timeout: 3) || wait(for: .runningBackgroundSuspended, timeout: 3)
58+
XCTContext.runActivity(named: "Move \(self) to background") { _ in
59+
XCUIDevice.shared.press(.home)
60+
_ = wait(for: .runningBackground, timeout: 3) || wait(for: .runningBackgroundSuspended, timeout: 3)
61+
}
5062
}
5163
}

Sources/XCAppTest/XCUIElement+Actions.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ extension XCUIElement {
1616
file: StaticString = #file,
1717
line: UInt = #line
1818
) -> Self {
19-
assertExists(waitForAppToIdle: true, message() ?? "Element should exist to be interactive.", file: file, line: line)
20-
assertIsInteractive(message(), file: file, line: line)
19+
XCTContext.runActivity(named: "Wait for \(self) to be interactive") { _ in
20+
assertExists(waitForAppToIdle: true, message() ?? "Element should exist to be interactive.", file: file, line: line)
21+
assertIsInteractive(message(), file: file, line: line)
22+
}
2123
return self
2224
}
2325

@@ -34,8 +36,10 @@ extension XCUIElement {
3436
file: StaticString = #file,
3537
line: UInt = #line
3638
) -> Self {
37-
waitForInteractivity(message(), file: file, line: line)
38-
tap()
39+
XCTContext.runActivity(named: "Tap \(self) when ready") { _ in
40+
waitForInteractivity(message(), file: file, line: line)
41+
tap()
42+
}
3943
return self
4044
}
4145

Sources/XCAppTest/XCUIElement+Assertions.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ extension XCUIElement {
2323
file: StaticString = #file,
2424
line: UInt = #line
2525
) -> Self {
26-
if exists && !waitForAppToIdle { return self }
27-
XCTAssertTrue(waitForExistence(timeout: 8), message() ?? "Element should be visible", file: file, line: line)
26+
XCTContext.runActivity(named: "Assert \(self) exists") { _ in
27+
if !exists || waitForAppToIdle {
28+
XCTAssertTrue(
29+
waitForExistence(timeout: 8),
30+
message() ?? "Element should be visible",
31+
file: file,
32+
line: line
33+
)
34+
}
35+
}
2836
return self
2937
}
3038

@@ -42,6 +50,7 @@ extension XCUIElement {
4250
line: UInt = #line
4351
) -> Self {
4452
assert(
53+
named: "Assert \(self) NOT exists",
4554
condition: { !$0.exists },
4655
message() ?? "Element should NOT exist",
4756
file: file,
@@ -66,6 +75,7 @@ extension XCUIElement {
6675
line: UInt = #line
6776
) -> Self {
6877
assert(
78+
named: "Assert \(self) is hittable",
6979
condition: { $0.isHittable },
7080
message() ?? "Element should be hittable",
7181
file: file,
@@ -88,6 +98,7 @@ extension XCUIElement {
8898
line: UInt = #line
8999
) -> Self {
90100
assert(
101+
named: "Assert \(self) is not hittable",
91102
condition: { !$0.isHittable },
92103
message() ?? "Element should NOT be hittable",
93104
file: file,
@@ -110,6 +121,7 @@ extension XCUIElement {
110121
line: UInt = #line
111122
) -> Self {
112123
assert(
124+
named: "Assert \(self) is enabled",
113125
condition: { $0.isEnabled },
114126
message() ?? "Element should be enabled",
115127
file: file,
@@ -132,6 +144,7 @@ extension XCUIElement {
132144
line: UInt = #line
133145
) -> Self {
134146
assert(
147+
named: "Assert \(self) is NOT enabled",
135148
condition: { !$0.isEnabled },
136149
message() ?? "Element should NOT be enabled",
137150
file: file,
@@ -154,6 +167,7 @@ extension XCUIElement {
154167
line: UInt = #line
155168
) -> Self {
156169
assert(
170+
named: "Assert \(self) is interactive",
157171
condition: { $0.exists && $0.isEnabled && $0.isHittable },
158172
message() ?? "Element should be enabled and hittable",
159173
file: file,
@@ -180,6 +194,7 @@ extension XCUIElement {
180194
line: UInt = #line
181195
) -> Self {
182196
assert(
197+
named: "Assert \(self) has label '\(label)'",
183198
condition: { $0.label == label },
184199
message() ?? "Element has incorrect label. Expected: '\(label)' but found: '\(self.label)'",
185200
file: file,
@@ -204,6 +219,7 @@ extension XCUIElement {
204219
line: UInt = #line
205220
) -> Self {
206221
assert(
222+
named: "Assert \(self) label contains text '\(text)'",
207223
condition: { $0.label.contains(text) },
208224
message() ?? "Element doesn't contain substring '\(text)' in its label: '\(self.label)'",
209225
file: file,
@@ -228,6 +244,7 @@ extension XCUIElement {
228244
line: UInt = #line
229245
) -> Self {
230246
assert(
247+
named: "Assert \(self) has value '\(expectedValue)'",
231248
condition: { $0.value as? T == expectedValue },
232249
message() ?? "Element has incorrect value. Expected '\(expectedValue)' but found '\(self.value ?? "")'",
233250
file: file,
@@ -252,6 +269,7 @@ extension XCUIElement {
252269
line: UInt = #line
253270
) -> Self {
254271
assert(
272+
named: "Assert \(self) has placeholder '\(expectedPlaceholder)'",
255273
condition: { $0.placeholderValue == expectedPlaceholder },
256274
message() ?? "Element has incorrect placeholder value. Expected '\(String(describing: expectedPlaceholder))' but found '\(String(describing: self.value))'",
257275
file: file,
@@ -276,6 +294,7 @@ extension XCUIElement {
276294
line: UInt = #line
277295
) -> Self {
278296
assert(
297+
named: "Assert \(self) is selected",
279298
condition: { $0.isSelected },
280299
message() ?? "Element should be selected",
281300
file: file,
@@ -298,6 +317,7 @@ extension XCUIElement {
298317
line: UInt = #line
299318
) -> Self {
300319
assert(
320+
named: "Assert \(self) is NOT selected",
301321
condition: { !$0.isSelected },
302322
message() ?? "Element should NOT be selected",
303323
file: file,

Sources/XCAppTest/XCUIElementQuery+Assertions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extension XCUIElementQuery {
2020
line: UInt = #line
2121
) -> Self {
2222
assert(
23+
named: "Assert \(self) matches \(count) elements",
2324
condition: { $0.count == count },
2425
message() ?? "Element query should return \(count) results but returned \(self.count)",
2526
file: file,

0 commit comments

Comments
 (0)