forked from agentic-review-benchmarks/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA11yUtils.swift
More file actions
124 lines (111 loc) · 5.3 KB
/
A11yUtils.swift
File metadata and controls
124 lines (111 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import XCTest
@MainActor
class A11yUtils: XCTestCase {
public struct MissingAccessibilityElement {
public let elementType: String
public let identifier: String
public let screen: String
public init(elementType: String, identifier: String, screen: String) {
self.elementType = elementType
self.identifier = identifier
self.screen = screen
}
}
public static func checkMissingLabels(in elements: [XCUIElement],
screenName: String,
missingLabels: inout [MissingAccessibilityElement],
elementType: String) {
for element in elements {
let hasA11yLabel = !(element.accessibilityLabel?.isEmpty ?? true)
let hasLabel = !(element.label.isEmpty) // Checks visible UI label
guard element.exists else { continue }
if !hasA11yLabel && !hasLabel && !element.identifier.isEmpty {
missingLabels.append(A11yUtils.MissingAccessibilityElement(
elementType: elementType,
identifier: element.identifier,
screen: screenName
))
}
}
}
public static func checkButtonLabels(
in element: XCUIElement,
screenName: String,
missingLabels: inout [MissingAccessibilityElement]
) {
// If the element is a button, check its labels.
if element.elementType == .button {
let hasA11yLabel = !(element.accessibilityLabel?.isEmpty ?? true)
let hasLabel = !(element.label.isEmpty)
if !hasA11yLabel && !hasLabel && !element.identifier.isEmpty {
missingLabels.append(MissingAccessibilityElement(
elementType: "Button",
identifier: element.identifier,
screen: screenName
))
}
}
// Recursively check all children of the current element.
let children = element.children(matching: .any).allElementsBoundByIndex
for child in children where child.exists {
checkButtonLabels(in: child, screenName: screenName, missingLabels: &missingLabels)
}
}
// Generates a TXT report for missing accessibility labels.
public static func generateTxtReport(missingLabels: [MissingAccessibilityElement]) -> String {
var report = "⚠️ Missing Accessibility Labels or UI Labels:\n\n"
for (index, element) in missingLabels.enumerated() {
report += "\(index + 1). Type: \(element.elementType), " +
"Identifier: \(element.identifier), " +
"Screen: \(element.screen)\n"
}
return report
}
// Generates a CSV report for missing accessibility labels.
public static func generateCSVReport(missingLabels: [MissingAccessibilityElement]) -> String {
var csv = "ElementType,Identifier,Frame\n"
for element in missingLabels {
csv += "\"\(element.elementType)\",\"\(element.identifier)\",\"\(element.screen)\"\n"
}
return csv
}
// Saves the given report content to a file and returns the file path.
public static func saveReportToFile(report: String, fileName: String) -> URL {
let deployDir = ProcessInfo.processInfo.environment["BITRISE_DEPLOY_DIR"] ?? NSTemporaryDirectory()
let fileURL = URL(fileURLWithPath: deployDir).appendingPathComponent(fileName)
do {
try report.write(to: fileURL, atomically: true, encoding: .utf8)
print("📄 Report saved to: \(fileURL.path)")
} catch {
print("❌ Failed to save report: \(error)")
}
return fileURL
}
// Generates the report and attaches it to the XCUITest results.
public static func generateAndAttachReport(missingLabels: [MissingAccessibilityElement],
testName: String,
generateTxt: Bool = true,
generateCsv: Bool = true) {
XCTContext.runActivity(named: "Accessibility Report - \(testName)") { activity in
if missingLabels.isEmpty {
activity.add(XCTAttachment(string: "✅ All elements have accessibility labels 🎉"))
}
if generateTxt {
let reportText = generateTxtReport(missingLabels: missingLabels)
// Save to files
let txtFilePath = saveReportToFile(report: reportText, fileName: "AccessibilityReport_\(testName).txt")
// Attach reports to Xcode test results
let txtAttachment = XCTAttachment(contentsOfFile: txtFilePath)
txtAttachment.lifetime = .keepAlways
activity.add(txtAttachment)
}
if generateCsv {
let reportCSV = generateCSVReport(missingLabels: missingLabels)
let csvFilePath = saveReportToFile(report: reportCSV, fileName: "AccessibilityReport_\(testName).csv")
let csvAttachment = XCTAttachment(contentsOfFile: csvFilePath)
csvAttachment.lifetime = .keepAlways
activity.add(csvAttachment)
}
}
}
}