Is there a way to move UISlider
under UITests
target in Xcode? I really need to move it from code. Is it possible?
Get your slider:
let app = XCUIApplication()
let timeSlider = app.sliders["timeSlider"]
Adjust its value:
timeSlider.adjustToNormalizedSliderPosition(0.9)
I made this extension based on this answer to fix the adjustToNormalizedSliderPosition
problem.
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 have to call instead:
app.sliders["Your Slider Identifier"].adjust(toNormalizedSliderValue: 0.5)
Here you can find some other useful tips for Xcode UITesting:
https://github.com/joemasilotti/UI-Testing-Cheat-Sheet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With