---
title: "#129: More UI Testing Tips 🚸"
url: https://littlebitesofcocoa.com/129-more-ui-testing-tips
author: Jake Marsh
published: 2015-11-19
topics: ["Testing","Tools"]
description: "Today we'll look at a couple more tips to get the most out of UI Testing in Xcode. (Covered previously in Bites #30, and #124)."
---

# #129: More UI Testing Tips 🚸

By Jake Marsh, November 19, 2015. HTML: https://littlebitesofcocoa.com/129-more-ui-testing-tips

Today we'll look at a couple more tips to get the most out of _UI Testing in Xcode_. (Covered previously in [Bites #30](https://littlebitesofcocoa.com/30), and [#124](https://littlebitesofcocoa.com/124)).

## Are We in a Test?

It'd be great if our **app** could know when it's running inside of a _UI Test_, then behave differently. We'll start by setting a flag that we can check for later. We'll add a `launchArgument` before we launch our **app** in our **tests**' `setUp` **functions**.

``` swift
let app = XCUIApplication()
app.launchArguments = [ "IS_UI_TESTING" ]
app.launch()
```

Then, we'll add a new helper **function** to our **app's** code. We could add this to a helper **class**, or just make it a global **function**:

``` swift
func isUITesting() -> Bool {
  return Process.arguments.contains("IS_UI_TESTING")
}
```

We can use this **function** throughout our code to do things like hit mock servers during testing, or simulate a camera preview when capturing screenshots with snapshot. ([Bite #110](https://littlebitesofcocoa.com/110)).

## Dismissing System Alerts

We have to jump through a couple of hoops here. First we'll need to set up what's called a "<i>UI Interruption handler</i>", which will execute a **closure** when the UI is interrupted by, for example, an alert being shown:

``` swift
addUIInterruptionMonitorWithDescription("Authorization Prompt") {
  $0.buttons["Allow"].tap()
  return true
}

app.tap()
```

Then, after our **app** presents the authorization prompt, we'll need to `.tap()` on it <i>once</i> before the UI Interruption handler will fire. Inside our handler, we'll accept the prompt then return true to tell **Xcode** we've handled the UI interruption.

Have a _UI Testing_ tip or trick of your own you'd like share? Send it along to [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com)!
