UI tests with XCUITest

Testing Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Fill the blank

Fill the blank so the test waits up to 5 seconds for the confirmation.

let app = XCUIApplication()
app.launchArguments = ["-uiTesting"]
app.launch()
app.buttons["checkoutButton"].tap()
XCTAssertTrue(app.staticTexts["orderConfirmation"].____(timeout: 5))
Options
Hint 1 of 1

Which API waits?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A waitForExistence

Correct. It polls until the element appears or the timeout passes.

Why not the others?
  • B exists. exists is a property that checks once and takes no timeout.
  • C isHittable. It's a property that checks once whether the element can be tapped.
  • D sleep. sleep isn't an element method, and fixed sleeps make tests flaky.

30-second answer

Launch XCUIApplication with launch arguments that switch on stubbed data, find elements by stable accessibilityIdentifier, interact with them, and wait with waitForExistence(timeout:) rather than sleeps. Keep UI tests few and focused on critical flows, and use page objects to keep them readable.

Practise saying it · 30 s. No recording, just a timer

Explain reliable UI tests.

Say your answer out loud, then check which of these points you covered:

  • Identifiers
  • Stubbed launch
  • Wait, don't sleep
  • Few critical flows

Deep dive

Identifiers are stable; labels change with copy and localization.

Disable animations or wait for elements instead of sleeping.

Reset app state for each test through launch arguments.

Record screenshots and use test plans for configurations.

Common mistakes

  • Finding elements by visible text in a localized app.
  • Hundreds of UI tests covering logic that unit tests could.

Senior take & follow-ups

Seniors keep UI tests few, stable and backed by stubs.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you stub the network in UI tests?

    Show hint

    Launch arguments or environment that switch the app to fake services.

  2. What is a page object?

    Show hint

    A type that wraps a screen's elements and actions.

Suggest a fix if something here is wrong or out of date.