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:
Swift 4.2+
@bartłomiej-semańczyk answer, modified a little bit like this:
- Get your slider:
let app = XCUIApplication()
let timeSlider = app.sliders["timeSlider"]
- Adjust its value:
timeSlider.adjust(toNormalizedSliderPosition: 0.9)
Note: The number is CGFloat
and it is between 0 and 1.
If your UISlider minimum and maximum value is not 0 to 1 you have to convert it first like this:
let numberBetween01 = CGFloat((requestedVal - UISlider.minimumValue ) / UISlider.maximumValue - UISlider.minimumValue))
I wasn't able to get adjustToNormalizedSliderPosition
to work with my app's slider. I've seen some people posting online that it also didn't work for them.
Is it working for others here?
A less useful method to adjust a slider is to find the element, press for a duration, then drag to another element:
slider.pressForDuration(duration: 0.05, thenDragToElement: anotherElement)
How to move UISlider to Right ➡️
static func moveSliderToRight(element: XCUIElement){
_ = element.waitForExistence(timeout: 10)
element.adjust(toNormalizedSliderPosition: 1)
}
How to move UISlider to Left ⬅️
static func moveSliderToRight(element: XCUIElement){
_ = element.waitForExistence(timeout: 10)
element.adjust(toNormalizedSliderPosition: 0)
}
© 2022 - 2024 — McMap. All rights reserved.
adjustToNormalizedSliderPosition
. – Jonasjonathan