无法访问XCTestCase UI测试中的SwiftUI文本

vmpqdwk3  于 2023-03-22  发布在  Swift
关注(0)|答案(2)|浏览(117)

我正在尝试向SwiftUI项目添加UI测试。
我有一个列表,其中包含视图-这些视图包含许多视图。
我似乎无法访问UI测试中最远的视图。
我想我可以为每个元素添加一个可访问性标识符,但是我仍然不能使我的测试通过。
一个非常简单的例子;

ContentView

struct ListModel: Identifiable {
    let id: String
    let text: String
}

struct ContentView: View {
    
    private var state = (0..<50).map { ListModel(id: "\($0)", text: "Row \($0)") }
    
    var body: some View {
        List(state, id: \.id) { item in
            ContentViewRow(text: item.text)
                .accessibility(identifier: "FEED_ITEM")
        }
        .accessibility(identifier: "FEED")
    }
}

struct ContentViewRow: View {
    let text: String
    var body: some View {
        Text(text)
            .accessibility(identifier: "CONTENT_ROW_TEXT")
    }
}

测试

class TestingSwiftUIUITests: XCTestCase {

    func testExample() throws {
        // UI tests must launch the application that they test.
        let app = XCUIApplication()
        app.launch()
        
        let feed = app.tables["FEED"]
        XCTAssert(feed.waitForExistence(timeout: 0.5))

        let row0 = feed.staticTexts["FEED_ITEM"].firstMatch
        XCTAssert(row0.waitForExistence(timeout: 0.5))
        
        let textView = row0.staticTexts["CONTENT_ROW_TEXT"].firstMatch
        XCTAssert(textView.waitForExistence(timeout: 0.5)) // <-- This fails.
    }
}

如何访问ContentViewRow中的视图-谢谢。

irlmq6kh

irlmq6kh1#

  • 标识符:“CONTENT_ROW_TEXT”* 似乎被 * 标识符覆盖:“FEED_ITEM”* 如果需要,您可以只留下 “FEED_ITEM” 并检查标签文本。
let row0 = feed.staticTexts["FEED_ITEM"].firstMatch
    XCTAssert(row0.waitForExistence(timeout: 0.5))
    XCTAssert(row0.label == "Row 0")
2vuwiymt

2vuwiymt2#

在SwiftUI中列出UITestCase

Swift UI View Code
==========================
      import SwiftUI
    
    struct ContentView: View {
    
        @StateObject var viewModel = ContentViewModel()
    
        var body: some View {
            NavigationView {
                List(viewModel.list, id: \.id) { item in
                    NavigationLink(destination: self.detailsView(content: item)) {
                        ContentViewRow(text: item.text)
                            .accessibility(identifier: "FEED_ITEM_\(item.id)")
                    }
                }
                .listStyle(.plain)
                .padding(.all, 20)
                .accessibility(identifier: "FEED")
                .onAppear {
                    self.viewModel.publishListModel()
                }
                .navigationTitle("List")
            }
        }
    
        func detailsView(content: ListModel) -> some View {
           return ContestDetailsView(content: content)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    struct ContentViewRow: View {
        let text: String
        var body: some View {
            Text(text)
                .accessibility(identifier: "CONTENT_ROW_TEXT")
        }
    }
struct ContestDetailsView: View {
  var content: ListModel
    
    var body: some View {
        Text(content.text)
            .navigationTitle("Details")
            .accessibility(identifier: "FEED_Details")
    }
}
====================================
     // UI Test Case 
      func testTableList() throws {
            // UI tests must launch the application that they test.
            let app = XCUIApplication()
            app.launch()
    
            let pred = NSPredicate(format: "identifier == 'FEED'")
            let tableView = app.descendants(matching: .any).matching(pred).firstMatch
            let arrOfTexts = tableView.staticTexts
            XCTAssertGreaterThan(arrOfTexts.count, 10)
           // let feed = app.tables["FEED"]
            XCTAssert(tableView.waitForExistence(timeout: 0.5))
    
            let row7 = tableView.staticTexts["FEED_ITEM_7"].firstMatch
            XCTAssert(row7.waitForExistence(timeout: 0.5))
    
    //        let textView = row7.staticTexts["Row 7"].firstMatch
    //        XCTAssert(textView.waitForExistence(timeout: 0.5)) // <-- This fails.
    
            row7.tap()
            let textView = app.staticTexts["Row 7"]
                    // If the text ran successfully, the the above textView will be != Nil
            XCTAssertEqual(textView.exists, true)
        }

相关问题