DemoViewUITests
class DemoViewUITests: XCTestCase {
override func setUpWithError() throws {}
override func tearDownWithError() throws {}
func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()
// get all the elements in the app
let allTheElements = app.descendants(matching: .any)
// filter out the 'other' elements
// These seem to be the internal views wrapping the app
// as they all have a frame of the full window
// looking at the Window Inspector I think they are
// - UITransitionView
// - UIDropShadowView
// - HostingViewController
// - HostingView
let notOtherPredicate = NSPredicate(format: "elementType != %d",
// can't just use the enum value
// see stackoverflow.com/questions/32298645/enum-value-and-predicates
// so we pass in the NSUInteger version (other is 1)
// see developer.apple.com/documentation/xctest/xcuielementattributes/1500614-elementtype
argumentArray: [XCUIElement.ElementType.other.rawValue])
let elements = allTheElements.matching(notOtherPredicate)
// should have 5
// 1 - the Window
// 2 - the Text with no accessibility label
// 3 - the Text with an accessibility label
// 4 - the TextField
// 5 - the Button
XCTAssertEqual(elements.count, 5)
for elementIndex in 0..<elements.count{
let element = elements.element(boundBy: elementIndex)
let type = ELEMENT_TYPE[element.elementType.rawValue]!.padding(toLength: 10, withPad: " ", startingAt: 0)
let label = "\(element.label)".padding(toLength: 40, withPad: " ", startingAt: 0)
let value = "\(element.value ?? "NIL")"
print("Element Info - Type: \(type) Label: \(label) Value: \(value)")
}
}
}
// Rest is just for pretty formatting :)
// can't extract key representation
// from C Struct XCUIElementType
// (even though it's XCUIElement.ElementType it
// points to XCUIElementType weird...)
// so we provide a table here
var ELEMENT_TYPE : [UInt: String] = [
0 : "Any",
1 : "Other",
2 : "Application",
3 : "Group",
4 : "Window",
5 : "Sheet",
6 : "Drawer",
7 : "Alert",
8 : "Dialog",
9 : "Button",
10 : "RadioButton",
11 : "RadioGroup",
12 : "CheckBox",
13 : "DisclosureTriangle",
14 : "PopUpButton",
15 : "ComboBox",
16 : "MenuButton",
17 : "ToolbarButton",
18 : "Popover",
19 : "Keyboard",
20 : "Key",
21 : "NavigationBar",
22 : "TabBar",
23 : "TabGroup",
24 : "Toolbar",
25 : "StatusBar",
26 : "Table",
27 : "TableRow",
28 : "TableColumn",
29 : "Outline",
30 : "OutlineRow",
31 : "Browser",
32 : "CollectionView",
33 : "Slider",
34 : "PageIndicator",
35 : "ProgressIndicator",
36 : "ActivityIndicator",
37 : "SegmentedControl",
38 : "Picker",
39 : "PickerWheel",
40 : "Switch",
41 : "Toggle",
42 : "Link",
43 : "Image",
44 : "Icon",
45 : "SearchField",
46 : "ScrollView",
47 : "ScrollBar",
48 : "StaticText",
49 : "TextField",
50 : "SecureTextField",
51 : "DatePicker",
52 : "TextView",
53 : "Menu",
54 : "MenuItem",
55 : "MenuBar",
56 : "MenuBarItem",
57 : "Map",
58 : "WebView",
59 : "IncrementArrow",
60 : "DecrementArrow",
61 : "Timeline",
62 : "RatingIndicator",
63 : "ValueIndicator",
64 : "SplitGroup",
65 : "Splitter",
66 : "RelevanceIndicator",
67 : "ColorWell",
68 : "HelpTag",
69 : "Matte",
70 : "DockItem",
71 : "Ruler",
72 : "RulerMarker",
73 : "Grid",
74 : "LevelIndicator",
75 : "Cell",
76 : "LayoutArea",
77 : "LayoutItem",
78 : "Handle",
79 : "Stepper",
80 : "Tab",
81 : "TouchBar",
82 : "StatusItem",
]