Search code examples
swiftlocalizationscreenshotui-testingnssegmentedcontrol

Is there a way to use .selectedSegmentIndex instead of .buttons["name"] in UITest?


When doing my element query in my UITests, to take screenshots of the different segmented control value, an error is thrown when translation comes into play because I use .buttons["name"], and the name is in English and is not being translated as expected. It has been translated from the storyboard. So I would like to find a simpler solution.

Can I have the .selectedSegmentIndex instead of the button name that has translation error in UITest, or is there another way to fix this issue?

XCUIApplication().scrollViews
  .children(matching: .other)
  .element
  .children(matching: .other)
  .element(boundBy: 0)
  .children(matching: .other)
  .otherElements
  .segmentedControls["MySegmentedControl"]
  .buttons["name"] // How to add this value to be .selectedSegmentIndex = 1?
  .tap()

Solution

  • You can access an element at specified index using element(boundBy:)

    For your case;

    app.scrollViews
    .segmentedControls
    .buttons
    .element(boundBy: 1)
    .tap()