Xcode UITesting - how to move UISlider?

Is there a way to move UISliderto UITeststarget in Xcode? I really need to move it from code. Is it possible?

+4
source share
3 answers
  • Get your slider:

    let app = XCUIApplication()
    let timeSlider = app.sliders["timeSlider"]
    
  • Adjust its value:

    timeSlider.adjustToNormalizedSliderPosition(0.9)
    
+6
source

I could not get adjustToNormalizedSliderPositionto work with my application slider. I saw some people post online that they also did not work for them. Does it work for others here?

A less useful way to customize the slider is to search for an element, its duration, and then drag it to another element:

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

, 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)
    }

}

Then you only need to call:

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

Here you can find other useful tips for Xcode UITesting:

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

0
source

Source: https://habr.com/ru/post/1627999/


All Articles