2016-11-28 2 views
1

XCode UIテストを使用するiOSピッカーホイールを生成してインデックス値に調整する方法や、インデックス値のテキストを取得してピッカーホイールを調整する方法はありますか?以下のコードを使用してインデックス値を取得できました。しかし、インデックス値の値を取得することができません。iOSピッカーホイールをランダムに生成するように調整する方法

let picker = app.pickerWheels.element(boundBy: 1).otherElements.count 
print(picker) 
let randomNumber = arc4random_uniform((UInt32(picker))) 
print(randomNumber) 

ピッカーホイールのカウントを使用して、それからランダムな値を生成しています。 randomNumberのインデックス番号で、私は値を取得しようとしています。 インデックスの値を取得してピッカーホイールをその値に調整する方法はありますか。

答えて

0

これは、XCTest UIテストフレームワークでピッカーを処理する方法です。

ピッカーホイールの状態を表すことができるオブジェクト(PickerState)があります。このオブジェクトは、ピッカーホイールの値を使用して入力されます。フォーマットは"Some value, 4 of 5"です。 getPickerStateメソッドは、値StringをPickerStateオブジェクトに変換します。

generateRandomNewPickerPositionは、ピッカーの現在の位置が返されないように、ピッカーが移動するためのランダムインデックスを生成します。

次に、XCUIElementの拡張メソッドを使用して、ランダムに生成された値changePickerSelectionに移動します。

/// Object to represent the state of a picker wheel. 
class PickerState { 
    let selectedValue: String 
    let currentPosition: UInt 
    let maximumPosition: UInt 

    init(selectedValue: String, currentPosition: UInt, maximumPosition: UInt) { 
     self.selectedValue = selectedValue 
     self.currentPosition = currentPosition 
     self.maximumPosition = maximumPosition 
    } 
} 

/// Retrieve a PickerState object, given the value of a picker wheel. 
func getPickerState(_ text: String) { 
    // Separate value 
    let splitText = text.componentsSeparatedByString(",") 
    let selectedValue = splitText[0] 

    // Extract numbers 
    var numbers = splitText[1].componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet) 
    numbers = numbers.filter { $0 != "" } 
    let currentPosition = UInt(numbers[0])! 
    let maximumPosition = UInt(numbers[1])! 

    return PickerState(selectedValue: selectedValue, currentPosition: currentPosition, maximumPosition: maximumPosition) 
} 

/// Generate a new position (1-indexed) for the picker. 
func generateRandomNewPickerPosition(pickerState: PickerState) -> UInt { 
    let valueCount = pickerState.maximumPosition 
    let random = UInt(arc4random_uniform(UInt32(valueCount - 1))) 
    let newPosition = ((pickerState.currentPosition + random) % valueCount) + 1 

    return newPosition 
} 

/// Move up/down the picker options until the given `selectionPosition` is reached. 
func changePickerSelection(pickerWheel: XCUIElement, selectionPosition: UInt) { 
    // Select the new value 
    var valueSelected = false 
    while !valueSelected { 
     // Get the picker wheel's current position 
     if let pickerValue = pickerWheel.value { 
      let currentPosition = UInt(getPickerState(String(pickerValue)).currentPosition) 
      switch currentPosition.compared(to: selectionPosition) { 
      case .GreaterThan: 
       pickerWheel.selectPreviousOption() 
      case .LessThan: 
       pickerWheel.selectNextOption() 
      case .Equal: 
       valueSelected = true 
      } 
     } 
    } 
} 

/// Extend XCUIElement to contain methods for moving to the next/previous value of a picker. 
extension XCUIElement { 
    /// Scrolls a picker wheel up by one option. 
    func selectNextOption() { 
     let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5)) 
     let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: 30.0)) 
     endCoord.tap() 
    } 

    /// Scrolls a picker wheel down by one option. 
    func selectPreviousOption() { 
     let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5)) 
     let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -30.0)) 
     endCoord.tap() 
    } 
} 

let pickerWheel = app.pickerWheels.element(boundBy: 0) 
let newPosition = generateRandomNewPickerPosition(getPickerState(pickerWheel.value)) 
changePickerSelection(pickerWheel, selectionPosition: newIndex) 
関連する問題