【问题标题】:UI Testing with Xcode: How to find a button that has no title?使用 Xcode 进行 UI 测试:如何找到没有标题的按钮?
【发布时间】:2017-06-04 16:15:36
【问题描述】:

我的用户界面中有一些按钮只显示图像而没有标题。在 UI 测试期间如何访问它们? window.buttons["delete"].click() 由于缺少标题而找不到按钮。而且我无法设置标题,因为图像具有一定的透明度。

【问题讨论】:

    标签: macos xcode-ui-testing


    【解决方案1】:

    您应该设置一个accessibilityIdentifier。这是一个非面向用户的属性,旨在允许您识别 UI 测试中的元素。引入它是为了阻止人们滥用accessibilityLabel,人们以前会在测试中使用它来识别事物,但这会影响 Voiceover 用户的体验,他们在选择元素时会听到 accessibilityLabel 的内容。

    // app code
    let myButton: UIButton!
    myButton.accessibilityIdentifier = "deleteButton"
    
    // test code
    let app = XCUIApplication()
    let deleteButton = app.buttons["deleteButton"]
    

    【讨论】:

      【解决方案2】:

      您可以通过accessibilityLabel 找到按钮。所以,首先设置标签:

      deleteButton.setAccessibilityLabel("delete")
      

      然后您可以正常访问它:

       untitledWindow.buttons["delete"].click()
      

      【讨论】:

      • 这确实有效;但是,请注意可访问性标签是面向用户的。可访问性标识符更可取,因为它不会影响用户。在代码中设置标识符,如@Oletha's answer 所示,或通过 Interface Builder。但是,如果出于某种原因确实想要(或需要)使用该标签,请确保将其命名为对使用画外音的人来说有意义的名称。
      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 2020-11-03
      • 2016-04-26
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 2023-03-30
      • 2022-11-16
      相关资源
      最近更新 更多