A test for this code:
func test_using_text_strings3() throws {
let app = XCUIApplication()
app.launch()
app.buttons["Hello, World!"].tap()
let heading2 = app.staticTexts["Two"]
XCTAssertEqual(heading2.label , "Two") // heading label
app.buttons["Hello, World #2!"].tap()
let heading3 = app.staticTexts["Three"]
XCTAssertEqual(heading3.label , "Three") // heading label
app.buttons["Pop to root"].tap()
let heading1 = app.staticTexts["Root"]
XCTAssertEqual(heading1.label , "Root") // heading label
}
A better test using accessibility Identifiers
func test_using_accessibilityID() throws {
let app = XCUIApplication()
app.launch()
app.buttons["button1"].tap()
let heading2 = app.staticTexts["Two"]
XCTAssertEqual(heading2.label , "Two") // heading label
app.buttons["button2"].tap()
let heading3 = app.staticTexts["Three"]
XCTAssertEqual(heading3.label , "Three") // heading label
app.buttons["button3"].tap()
let heading1 = app.staticTexts["Root"]
XCTAssertEqual(heading1.label , "Root") // heading label
}
Here's some code for simple testing of XCUI Testing.
//
// ContentView.swift
// Nav_Example_Test
//
// Created by David on 2/6/21.
//
import SwiftUI
struct ContentView: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(
destination: ContentView2(rootIsActive: self.$isActive),
isActive: self.$isActive
) {
Text("Hello, World!")
.accessibility(identifier: "button1")
}
.isDetailLink(false)
.navigationBarTitle("Root")
}
}
}
struct ContentView2: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
Text("Hello, World #2!")
.accessibility(identifier: "button2")
}
.isDetailLink(false)
.navigationBarTitle("Two")
}
}
struct ContentView3: View {
@Binding var shouldPopToRootView : Bool
var body: some View {
VStack {
Text("Hello, World #3!")
Button (action: { self.shouldPopToRootView = false } ){
Text("Pop to root")
.accessibility(identifier: "button3")
}
}.navigationBarTitle("Three")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here's a new implementation code:
//
// ContentView.swift
// Nav_Example_Test
//
// Created by David on 2/6/21.
//
import SwiftUI
struct ContentView: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
VStack {
Text("It's a small world, afterall...")
.accessibility(identifier: "smallworld")
Spacer()
NavigationLink(
destination: ContentView2(rootIsActive: self.$isActive),
isActive: self.$isActive
) {
Text("Hello, World!")
.accessibility(identifier: "button1")
}
.isDetailLink(false)
.navigationBarTitle("Tap-Root")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {}) {
Image(systemName: "bell.fill")
.padding(.horizontal)
}
.accessibility(identifier: "bell")
}
ToolbarItem(placement: .bottomBar) {
Button(action: {}) {
Text("Bing Me")
.padding(.horizontal)
}
.accessibility(identifier: "bingme")
}
}
Text("accessibilityIdentifier = button1")
.fontWeight(.light)
.font(.footnote)
.italic()
Spacer()
}
}
}
}
struct ContentView2: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
Text("Hello, World #2!")
.accessibility(identifier: "button2")
}
.isDetailLink(false)
.navigationBarTitle("Two")
}
}
struct ContentView3: View {
@Binding var shouldPopToRootView : Bool
var body: some View {
VStack {
Text("Hello, World #3!")
Button (action: { self.shouldPopToRootView = false } ){
Text("Pop to root")
.accessibility(identifier: "button3")
}
}.navigationBarTitle("Three")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I've not been using XCUI Test for quite some time... they were too difficult to get working. It seems in 2022 that the Accessibility Identifier issue has been fixed. I see all the cool kids using it now.