Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode UITesting - how to move UISlider?

Is there a way to move UISlider under UITests target in Xcode? I really need to move it from code. Is it possible?

like image 554
Bartłomiej Semańczyk Avatar asked Feb 09 '16 17:02

Bartłomiej Semańczyk


2 Answers

  1. Get your slider:

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

    timeSlider.adjustToNormalizedSliderPosition(0.9)
    
like image 170
Bartłomiej Semańczyk Avatar answered Nov 09 '22 20:11

Bartłomiej Semańczyk


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

like image 3
odm Avatar answered Nov 09 '22 22:11

odm