【问题标题】:How to correctly write a UI test for a Swift UI Toggle如何为 Swift UI Toggle 正确编写 UI 测试
【发布时间】:2022-06-12 08:17:41
【问题描述】:

有人知道如何正确地为 Toggle 编写 UI 测试吗?即使在一个只有一个 Toggle 而整个 UI 中没有其他内容的全新项目中,我也会不断收到这种错误:

Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x60000108c410>.
Sparse tree of matches:
→Application, pid: 26580, label: 'TestToggle'
 ↳Window (Main)
  ↳Other
   ↳Other
    ↳Other
     ↳Other
      ↳Switch, label: 'Test switch', value: 1
       ↳Switch, label: 'Test switch', value: 1

用户界面如下所示:

struct ContentView: View {
  @State private var toggleValue = true
  var body: some View {
    Toggle("Test switch", isOn: $toggleValue)
      .padding()
  }
}

测试看起来像这样(这两行中的任何一条都会给我同样的错误):

     func testExample() throws {
        let app = XCUIApplication()
        app.launch()
        
        XCTAssertTrue(app.switches["Test switch"].value as? String == "1")
//        XCTAssertTrue(app.switches["Test switch"].isEnabled)
    }

我肯定做错了什么。如果只有一个开关,怎么会出现两个开关?网上的文章似乎都没有提到我所见过的任何相关内容。任何帮助表示赞赏。谢谢:)

【问题讨论】:

    标签: ios swiftui xctest


    【解决方案1】:

    我在一些闲散的频道中收到了一些回复,结果发现由于某种原因使用 Toggle 我不能依赖默认标签进行测试(与 Text 和 Button 不同)并且需要创建自定义可访问性标识符。

    所以只需添加如下内容:

        Toggle("Test switch", isOn: $toggleValue)
            .padding()
            .accessibilityIdentifier("testSwitch")
    

    然后像这样测试:

    func testExample() throws {
        let app = XCUIApplication()
        app.launch()
        XCTAssertTrue(app.switches["testSwitch"].isEnabled)
        XCTAssertTrue(app.switches["testSwitch"].value as? String == "1")
    }
    

    【讨论】:

    • 我不确定您的测试是否断言您想要测试的内容。 isEnabled 返回“该元素是否为用户交互启用”。如果用户可以与之交互,isEnabled 将返回 true,即使切换已关闭。
    • 是的,你是对的,我试图测试它是否已打开。谢谢 :) 但这不是我面临的问题。问题出在可访问性标识符上。我已经更新了答案以包含两者。
    【解决方案2】:

    我不确定您为什么会收到您遇到的错误,但您可以尝试将 accessibilityIdentifier 添加到 Toggle 并以这种方式找到它。

    例如:

    struct ContentView: View {
      @State private var toggleValue = true
      var body: some View {
        Toggle("Test switch", isOn: $toggleValue)
          .padding()
          .accessibilityIdentifier("testSwitch")
      }
    }
    

    然后在您的测试中,您可以通过以下方式找到元素:

    func testExample() throws {
            let app = XCUIApplication()
            app.launch()
            
            XCTAssertTrue(app.switches["testSwitch"].value as? String == "1")
        }
    

    顺便说一句,如果你想测试 Toggle 是打开还是关闭,你应该使用isEnabled。这将检查“是否为用户交互启用了元素。”

    你应该使用

    app.switches["testSwitch"].value as? String == "1" // on
    

    app.switches["testSwitch"].value as? String == "0" // off
    

    取决于您要测试的内容。

    【讨论】:

    • 我已经回答了 3.5 个月前使用 accessibilityIdentifier 的问题,如上所示。不知道您为什么再次发布相同的答案。
    • @MichaelVescovo 您的回答使用了isEnabled,我不确定这是否是您尝试测试的内容。我以为你想测试开关是打开还是关闭。
    • 它不起作用的原因是因为我没有添加accessibilityIdentifier。这在您的答案中已经得到了回答,然后又再次得到了回答。如果您的答案不需要使用“accessibilityIdentifier”,那么肯定会有所帮助,但事实并非如此。
    • @MichaelVescovo 我明白,我可能应该省略accessibilityIdentifier 部分。老实说,我没有注意到你的回答已经包含了那部分。我想指出的是你在测试中使用了.isEnabled.isEnabled 部分将始终返回 true,除非您指定了 .disabled(true)。我不确定您是否意识到这一点,因为您可能正在测试错误的东西。
    • 我有机会对此进行测试。是的,你是对的;我想测试它是否已启用和打开。谢谢你的提示!我试图用我的问题解决的问题是accessibilityIdentifier 的问题,但您在我的测试中发现了一个错误。谢谢 :) 我已经更新了答案以包含两者。
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多