UI Test on UISegmented Control

GOAL: I want to UI test my SegmentedControl - check that each segment is selected when tapped on it

CONTEXT: The shoeType UISegmentedControl has 3 segments [City, Running, Baskets]

CODE: Here is my code:

ViewController.swift :

 @IBOutlet weak var shoeType: UISegmentedControl!

ViewControllerUITest.swift:

    func testSegmentedControl_WhenTapped_ChangeSegment() {
        app.launch()
        
        let shoeTypeSegmentedControl = app.segmentedControls["city"]
        app.segmentedControls["city"].tap()
        XCTAssertEqual(shoeTypeSegmentedControl.label, "City")
    }
Post not yet marked as solved Up vote post of DodoCodeLife Down vote post of DodoCodeLife
503 views

Replies

Here is some added code to ViewController.swift:

//    Updates the shoe image's shown
    func updateShoeImage() {
        shoeSelection.image = UIImage(named: getShoeParams(type: type, gender: genderLbl.text ?? "", color: color))
    }
    
//    Sets the value of the text displayed on the UI regarding the parameters
    func updateOrderResult() {
        if name == "" || name == "Mr." || name == "Miss" {
            if gender == "Boy" {
                name = "Mr."
            } else if gender == "Girl"{
                name = "Miss"
            }
        }
        orderResult.text = """
            Hello \(name) I found this pair of \(type.lowercased()) shoes in \(color.lowercased()) size \(sizeLbl.text?.lowercased() ?? "")
        """
    }