Making locators dynamic in XCUITest

234 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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”)