【问题标题】:Xcode UITesting - how to move UISlider?Xcode UITesting - 如何移动 UISlider?
【发布时间】:2016-02-09 17:25:09
【问题描述】:

有没有办法在 Xcode 中将 UISlider 移动到 UITests 目标下?我真的需要把它从代码中移走。有可能吗?

【问题讨论】:

    标签: ios swift xcode-ui-testing


    【解决方案1】:
    1. 获取您的滑块:

      let app = XCUIApplication()
      let timeSlider = app.sliders["timeSlider"]
      
    2. 调整其值:

      timeSlider.adjustToNormalizedSliderPosition(0.9)
      

    【讨论】:

      【解决方案2】:

      我根据this 的回答做了这个扩展来解决adjustToNormalizedSliderPosition 的问题。

      import XCTest
      
      extension XCUIElement {
      
          open func adjust(toNormalizedSliderValue normalizedSliderValue: CGFloat) {
              let start = coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
              let end = coordinate(withNormalizedOffset: CGVector(dx: normalizedSliderValue, dy: 0.0))
              start.press(forDuration: 0.05, thenDragTo: end)
          }
      
      }
      

      那么你只需要调用:

      app.sliders["Your Slider Identifier"].adjust(toNormalizedSliderValue: 0.5)
      

      在这里您可以找到一些其他有用的 Xcode UITesting 技巧:

      https://github.com/joemasilotti/UI-Testing-Cheat-Sheet

      【讨论】:

        【解决方案3】:

        我无法让 adjustToNormalizedSliderPosition 使用我的应用程序的滑块。我看到有些人在网上发帖说它也不适合他们。 它在这里为其他人工作吗?

        调整滑块的一个不太有用的方法是找到元素,按住一段时间,然后拖动到另一个元素:

        slider.pressForDuration(duration: 0.05, thenDragToElement: anotherElement)
        

        【讨论】:

        • 嗯。我确认我确实为滑块设置了可访问性标识符,但仍然无法使用您发布的解决方案调整滑块。关于我可能出了什么问题的任何其他想法?我能够通过accessibilityIdentifier 成功定位元素并对其执行其他操作,而不是adjustToNormalizedSliderPosition
        猜你喜欢
        • 1970-01-01
        • 2016-02-03
        • 2015-12-29
        • 2018-12-06
        • 2017-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多