【问题标题】:Is there a way to use .selectedSegmentIndex instead of .buttons["name"] in UITest?有没有办法在 UITest 中使用 .selectedSegmentIndex 而不是 .buttons["name"]?
【发布时间】:2020-07-21 07:54:10
【问题描述】:

在我的 UITests 中进行元素查询时,为了截取不同分段控件值的屏幕截图,由于我使用 .buttons["name"],所以在翻译开始时会引发错误,并且名称是英文并且没有被翻译为预期的。 它是从故事板翻译过来的。所以我想找到一个更简单的解决方案。

我可以使用 .selectedSegmentIndex 代替 UITest 中出现翻译错误的按钮名称吗,或者有其他方法可以解决此问题吗?

XCUIApplication().scrollViews
  .children(matching: .other)
  .element
  .children(matching: .other)
  .element(boundBy: 0)
  .children(matching: .other)
  .otherElements
  .segmentedControls["MySegmentedControl"]
  .buttons["name"] // How to add this value to be .selectedSegmentIndex = 1?
  .tap()

【问题讨论】:

  • 你试过app.segmentedControls.buttons.element(boundBy: 1) 吗?
  • 它不起作用,我收到此错误:无法获取匹配的快照:没有找到匹配来自输入的匹配类型 SegmentedControl 的后代
  • 我的意思是.segmentedControls["MySegmentedControl"].buttons.element(boundBy: 1),而不是segmentedControls["MySegmentedControl"].buttons["name"]。你试过了,对吧?
  • 我尝试了这个解决方案,但触发了同样的错误。
  • 顺便说一句,您之前是否使用您共享的代码访问了段控制?您需要先启动,然后通过app 访问。不像XCUIApplication().segmentedControls

标签: swift localization screenshot ui-testing nssegmentedcontrol


【解决方案1】:

您可以使用element(boundBy:)访问指定索引处的元素

根据你的情况;

app.scrollViews
.segmentedControls
.buttons
.element(boundBy: 1)
.tap()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2021-07-02
    • 1970-01-01
    • 2014-06-07
    • 2018-07-24
    • 2011-10-11
    相关资源
    最近更新 更多