Skip to content

Commit

Permalink
✅ Improve accessibility for UI testing
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed Jun 21, 2024
1 parent 7897b5f commit ea58c11
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 16 deletions.
16 changes: 16 additions & 0 deletions PyeonHaeng-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
BAAF1D302BAFF1920001EA36 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BAAF1D2F2BAFF1920001EA36 /* Preview Assets.xcassets */; };
BAAF1D3A2BB01D910001EA36 /* NoticeDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAAF1D392BB01D910001EA36 /* NoticeDetailView.swift */; };
BAAF1D3C2BB01D980001EA36 /* NoticeDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAAF1D3B2BB01D980001EA36 /* NoticeDetailViewModel.swift */; };
BAAF367B2C2559B600C5BF44 /* View+accessibilityIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAAF367A2C2559B600C5BF44 /* View+accessibilityIdentifier.swift */; };
BAAF367E2C255CC400C5BF44 /* Subscript+AccessibilityIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAAF367D2C255CC400C5BF44 /* Subscript+AccessibilityIdentifier.swift */; };
BAB254A02C2315F900E74438 /* BottomSheetHeightKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB2549F2C2315F900E74438 /* BottomSheetHeightKey.swift */; };
BAB569612B639F3000D1E0F9 /* DesignSystem in Frameworks */ = {isa = PBXBuildFile; productRef = BAB569602B639F3000D1E0F9 /* DesignSystem */; };
BAB5CF252B6B7C5A008B24BF /* Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB5CF242B6B7C5A008B24BF /* Services.swift */; };
Expand Down Expand Up @@ -166,6 +168,8 @@
BAAF1D2F2BAFF1920001EA36 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
BAAF1D392BB01D910001EA36 /* NoticeDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeDetailView.swift; sourceTree = "<group>"; };
BAAF1D3B2BB01D980001EA36 /* NoticeDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeDetailViewModel.swift; sourceTree = "<group>"; };
BAAF367A2C2559B600C5BF44 /* View+accessibilityIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+accessibilityIdentifier.swift"; sourceTree = "<group>"; };
BAAF367D2C255CC400C5BF44 /* Subscript+AccessibilityIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Subscript+AccessibilityIdentifier.swift"; sourceTree = "<group>"; };
BAB2549F2C2315F900E74438 /* BottomSheetHeightKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomSheetHeightKey.swift; sourceTree = "<group>"; };
BAB5CF242B6B7C5A008B24BF /* Services.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Services.swift; sourceTree = "<group>"; };
BAB5CF262B6B7CF3008B24BF /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -430,6 +434,7 @@
BA4C95E92BEA263E00E80D08 /* PyeonHaeng-iOSUITests */ = {
isa = PBXGroup;
children = (
BAAF367C2C255CB500C5BF44 /* Utils */,
BA4C95EA2BEA263E00E80D08 /* PyeonHaengUITests.swift */,
BA4C95EC2BEA263E00E80D08 /* PyeonHaeng_iOSUITestsLaunchTests.swift */,
);
Expand All @@ -447,6 +452,7 @@
isa = PBXGroup;
children = (
BA7C1A602C24317A00869EB3 /* Array+subscript.swift */,
BAAF367A2C2559B600C5BF44 /* View+accessibilityIdentifier.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -545,6 +551,14 @@
path = "Preview Content";
sourceTree = "<group>";
};
BAAF367C2C255CB500C5BF44 /* Utils */ = {
isa = PBXGroup;
children = (
BAAF367D2C255CC400C5BF44 /* Subscript+AccessibilityIdentifier.swift */,
);
path = Utils;
sourceTree = "<group>";
};
BAB2549E2C2315E600E74438 /* PreferenceKeys */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -839,6 +853,7 @@
BA4C95ED2BEA263E00E80D08 /* PyeonHaeng_iOSUITestsLaunchTests.swift in Sources */,
BA9D943D2BF20ECB00E7C9F7 /* AccessibilityIdentifier.swift in Sources */,
BA4C95EB2BEA263E00E80D08 /* PyeonHaengUITests.swift in Sources */,
BAAF367E2C255CC400C5BF44 /* Subscript+AccessibilityIdentifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -852,6 +867,7 @@
E5028D5C2B96BA9400B36C16 /* SearchView.swift in Sources */,
BAE159DA2B65FC35002DCF94 /* HomeProductListView.swift in Sources */,
BA402F7E2B85E31800E86AAD /* ConvenienceSelectBottomSheetView.swift in Sources */,
BAAF367B2C2559B600C5BF44 /* View+accessibilityIdentifier.swift in Sources */,
BA19897C2BA95E8C0083B4BE /* DIContainer.swift in Sources */,
BA28F1852B6155810052855E /* OnboardingView.swift in Sources */,
BA7C1A612C24317A00869EB3 /* Array+subscript.swift in Sources */,
Expand Down
32 changes: 26 additions & 6 deletions PyeonHaeng-iOS/Sources/AccessibilityIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@
import Foundation

public enum AccessibilityIdentifier {
public enum Splash {
static let screen = "splash.screen"
case splash(Splash)
case onboarding(Onboarding)
case home(Home)

var stringValue: String {
switch self {
case let .splash(splash):
splash.rawValue
case let .onboarding(onboarding):
onboarding.rawValue
case let .home(home):
home.rawValue
}
}

public enum Splash: String {
case screen = "splash.screen"
}

public enum Onboarding: String {
case navigationBar = "onboarding.navigationBar"
case skip = "onboarding.skip"
}

public enum Home {
static let screen = "home.screen"
static let productCountLabel = "product.count.label"
static let sortButton = "product.sort.button"
public enum Home: String {
case screen = "home.screen"
case productCountLabel = "product.count.label"
case sortButton = "product.sort.button"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// View+accessibilityIdentifier.swift
// PyeonHaeng-iOS
//
// Created by 홍승현 on 6/21/24.
//

import SwiftUI

public extension View {
/// Uses the AccessibilityIdentifier you specify to identify the view.
///
/// Use this value for testing. It isn't visible to the user.
func accessibilityIdentifier(_ identifier: AccessibilityIdentifier) -> ModifiedContent<Self, AccessibilityAttachmentModifier> {
accessibilityIdentifier(identifier.stringValue)
}
}
6 changes: 3 additions & 3 deletions PyeonHaeng-iOS/Sources/PyeonHaengApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftUI
struct PyeonHaengApp: App {
@State private var networkMonitor: NetworkMonitor = .init()
#if !RELEASE
@AppStorage("isFirstLaunch") private var isFirstLaunch = false
@AppStorage("isFirstLaunch") private var isFirstLaunch = true
#endif
private let services = Services()

Expand All @@ -23,15 +23,15 @@ struct PyeonHaengApp: App {
setupNavigationBarAppearance()
FirebaseApp.configure()
#if !RELEASE
isFirstLaunch = false
isFirstLaunch = true
#endif
}

var body: some Scene {
WindowGroup {
RootView {
SplashView()
.accessibilityIdentifier(AccessibilityIdentifier.Splash.screen)
.accessibilityIdentifier(.splash(.screen))
.environment(\.injected, DIContainer(services: services))
// 네트워크 연결 모니터링
.onChange(of: networkMonitor.isSatisfied) { oldValue, newValue in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct HomeProductSorterView<ViewModel>: View where ViewModel: HomeViewModelRepr
var body: some View {
HStack {
Text(productCountString)
.accessibilityIdentifier(AccessibilityIdentifier.Home.productCountLabel)
.accessibilityIdentifier(.home(.productCountLabel))
.font(.title2)
Spacer()
Button {
Expand All @@ -35,7 +35,7 @@ struct HomeProductSorterView<ViewModel>: View where ViewModel: HomeViewModelRepr
}
.accessibilityLabel(accessibilityLabel(for: viewModel.state.productConfiguration.order))
.accessibilityHint("더블 탭하여 정렬 기준을 바꿔보세요")
.accessibilityIdentifier(AccessibilityIdentifier.Home.sortButton)
.accessibilityIdentifier(.home(.sortButton))
}
.padding(.all, 8)
}
Expand Down
4 changes: 2 additions & 2 deletions PyeonHaeng-iOS/Sources/Scenes/HomeScene/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftUI
struct HomeView<ViewModel>: View where ViewModel: HomeViewModelRepresentable {
@StateObject private var viewModel: ViewModel
@State private var isOnboardingSheetOpen = false
@AppStorage("isFirstLaunch") private var isFirstLaunch = false
@AppStorage("isFirstLaunch") private var isFirstLaunch = true
@Environment(\.injected) private var container

init(viewModel: @autoclosure @escaping () -> ViewModel) {
Expand Down Expand Up @@ -83,7 +83,7 @@ struct HomeView<ViewModel>: View where ViewModel: HomeViewModelRepresentable {
OnboardingView()
}
.onAppear {
if !isFirstLaunch {
if isFirstLaunch {
isOnboardingSheetOpen = true
}
viewModel.trigger(.fetchProducts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct OnboardingView: View {
// MARK: Properties

@Environment(\.dismiss) private var dismiss
@AppStorage("isFirstLaunch") private var isFirstLaunch = true

/// 온보딩 페이지 중에서 보여지고 있는 페이지의 ID
@State private var activeID: UUID? = pages.first?.id
Expand Down Expand Up @@ -61,6 +62,7 @@ struct OnboardingView: View {
activeID = nextID
}
} else {
isFirstLaunch = false
dismiss()
}
} label: {
Expand All @@ -80,12 +82,15 @@ struct OnboardingView: View {
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
isFirstLaunch = false
dismiss()
} label: {
Text("건너뛰기")
.font(.b2)
.foregroundStyle(Color.green500)
.accessibilityIdentifier(.onboarding(.skip))
}
.accessibilityIdentifier(.onboarding(.navigationBar))
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions PyeonHaeng-iOSUITests/PyeonHaengUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ final class PyeonHaengUITests: XCTestCase {
func testProductCountLabelDisplayedInSingleLine() {
let app = XCUIApplication()
app.launch()
XCTAssertTrue(app.otherElements[AccessibilityIdentifier.Splash.screen].exists, TestFailureMessage.splashScreenNotDisplayed)

let productCountLabel = app.staticTexts[AccessibilityIdentifier.Home.productCountLabel]
XCTAssertTrue(app.otherElements[.splash(.screen)].exists, TestFailureMessage.splashScreenNotDisplayed)

let button = XCUIApplication().navigationBars["_TtGC7SwiftUI32NavigationStackHosting"].buttons[.onboarding(.navigationBar)]
if button.exists { button.tap() }

let productCountLabel = app.staticTexts[.home(.productCountLabel)]
let initialLabelHeight = productCountLabel.frame.size.height

let productSortButton = app.buttons[AccessibilityIdentifier.Home.sortButton]
let productSortButton = app.buttons[.home(.sortButton)]
productSortButton.tap()

let labelHeightAfterTap = productCountLabel.frame.size.height
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Subscript+AccessibilityIdentifier.swift
// PyeonHaeng-iOSUITests
//
// Created by 홍승현 on 6/21/24.
//

import XCTest

extension XCUIElementQuery {
subscript(_ identifier: AccessibilityIdentifier) -> XCUIElement {
self[identifier.stringValue]
}
}

0 comments on commit ea58c11

Please sign in to comment.