0

Is it possible to make locators dynamic in XCUITest? generally when defining a locator we tend to write as: let optionAbutton = app.buttons["option-a"]

And use it in func as optionAbutton.tap();

Now, if there are more buttons on the page like: option-b, option-c should we have separate locators for each like: let optionBbutton = app.buttons["option-b"] let optionCbutton = app.buttons["option-c"]

Or is it possible to keep the locator generic like app.buttons["PLACEHOLDER"] and replace it with option-a, option-b or option-c inside the func?

Siva2789
  • 79
  • 6

1 Answers1

0

Of course. The identifier is simply a string so any amount of string manipulation is permitted.

func tapAnOptionButton(suffix: String) {
  anOptionButton(suffix).tap()
}

private func anOptionButton(_ suffix: String) -> XCUIElement {
  return app.buttons[“option-“ + suffix]
}

And then to use it:

tapAnOptionButton(suffix: “a”)
Mike Collins
  • 4,108
  • 1
  • 21
  • 28