Skip to content

[RFC] Add UITest ViewId and ScreenProtocol to template for easy UITest setup  #397

@blyscuit

Description

@blyscuit

Issue

  • Default UITest implication is hard to manage and not readable.

  • Affecting developer to not wanting to start writing UITest or not liking UITest.

Solution

  • Include a boilerplate for easy adoption of UITest.

Example of Boilerplate code

enum ViewId {
    case general(General)

    func callAsFunction() -> String {
        switch self {
        case let .general(general): return general.rawValue
        }
    }
}

extension ViewId {
    enum General: String {
        case keyboard = "general.keyboard"
        case loadingSpinner = "general.loading.spinner"
    }
}

// UIKit

extension UIView {
    func setAccessibilityId(_ viewId: ViewId) {
        accessibilityIdentifier = viewId()
    }
}


// SwiftUI
extension View {

    func accessibility(_ viewId: ViewId) -> some View {
        accessibilityIdentifier(viewId())
    }
}


protocol ScreenProtocol: AnyObject {

    associatedtype Identifier: RawRepresentable where Identifier.RawValue == String

    var application: XCUIApplication { get }
}

extension ScreenProtocol {

    var keyboard: KeyboardScreen {
        KeyboardScreen(in: application)
    }

    func find(
        _ elementKey: KeyPath<XCUIApplication, XCUIElementQuery>,
        with identifier: Identifier
    ) -> XCUIElement {
        return application[keyPath: elementKey][identifier.rawValue]
    }
}

Who Benefits?

Template users.

What next?

  • Vote if we want UITest boilerplate in Template.
  • Discuss on UITest pattern.
  • Include new UITest pattern to iOS Template as needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions