Xcode Cheat Sheet

  



14 minute read

(cheat sheets) 2020/04/26 by jean tran. I wanted to share some of Xcode common shortcuts, that could be time-saver especially while main tasks is coding.

Updated for Xcode 12

Shortly after Apple’s “Hey Siri” event the Xcode 7 GM was released to developers. Let’s put the betas behind us and take a look at some real world UI Testing examples.

  • You’re in luck if you have thought that same thing, because Colin Wheeler is a Cocoa Developer that has created a really handy XCode keyboard shortcut cheatsheet, it’s a great resource for anyone who is interested in improving their XCode workflow.
  • XCODE CHEAT SHEET presented by TOWER — the most powerful Git client for Mac. The most powerful Git client for Mac 30-day free trial available at www.git-tower.com.
  • I have made a cheatsheet below to compare between Xcode and Android Studio next to each other for easier reference. Before moving further, just to provide definition of keys as below.

The examples all use a test host that helps you manage your volleyball team. The app, Volley, lets the user manage the team roster, adjust the formation, and view game results. Feel free to take a look at the source code before continuing along to familiarize yourself with how it works.

Why not use recording?

You can certainly use the recording feature of UI Testing on its own. Recording makes sense when adding regression tests to an existing codebase. But you’ll miss out on the perks of writing your own tests. However, there are three reasons why recording isn’t always the best idea.

  1. With test-driven development you write your tests first, so you won’t have an app to record!
  2. Recording doesn’t always work. Recording interaction with some elements, such as web views, never works, and UISlider manipulation is finicky at best.
  3. The most important reason to learn UI Testing by hand is to, well, learn it yourself! At some point you will need to read the code generated by Xcode and debug it. If you can’t figure out what is going on you are going to have a hard time deciphering where something went wrong. Also, if you are using any custom controls you will need to write the tests yourself.

Basic functionality

These code snippets assume you are familiar with the basics of UI Testing. If you are just getting started, or would like a refresher, I suggest reading an overview of UI Testing in Xcode.

The examples also assume that your app is launched and you hold a reference to it, app. You can do this in your test class’ setUpWithFailure() method to ensure the app launches clean for every test.

How to assert an element exists

Let’s start off simple. The first thing you will do when validating your app with UI Testing is asserting if an element exists. This is easily done by querying for an element and asking if it exists.

Using Volley’s home screen we can assert that the app’s name exists like so. Note the wrapping of the call in an XCTest assertion. This ensures that the test would fail if the label were not to exist.

How to wait for an element to appear

Viewing our schedule in Volley shows us our last couple of game results. Alright, 2-1! Not a bad start to the season. But when’s our next game?

Tapping the Load More Games button simulates fetching data from a server. After two seconds pass, our fourth game comes back.

If five seconds pass before the expectation is met then the test will fail.

How to print the accessibility hierarchy

Printing the accessibility hierarchy is a good way to “see what the framework sees.” You can use this and the Accessibility Inspector to debug querying and selecting elements

Interacting with system controls

Beyond validating and verifying basic conditions, you will want to interact with your app. XCUIElement exposes a few crucial functions to help us tap around the interface. These method use a XCUIElementQuery to find the element by its accessibility label or identifier.

How to tap a button

How to type text into a text field

Typing into a text field requires that the target has focus. Simply tapping on it will ensure the text field is the first responder.

How to dismiss an alert

First find the alert by querying for its title. You can then reference its buttons by chaining on the selector and tapping which one you want.

You can try this in Volley by going to the Schedule screen and tapping on the “Finish Game” button.

How to interact with a system alert

Ah, system alerts, the bane of all iOS testers’ existence. Almost every other framework I’ve worked with has had issues with system alerts, whether it’s location services, push notifications, or access to your photos.

With UI Testing we can finally reliably interact with these pesky dialogs. There are, however, two small gotchas.

First, set up a “UI Interuption Handler” before presenting the alert. This is our way of telling the framework how to handle an alert when it appears.

Second, after presenting the alert you must interact with the interface. Simply tapping the app works just fine, but is required.

How to slide a slider

When adjusting a UISlider with UI Testing you don’t pass in the value you want to set. Instead, you normalize the value on a scale from 0 to 1. For example, the slider in Volley has a range of 0 to 10. To slide the scrubber to 7 we pass in 0.7, or, how I like to think of it, 70%.

How to select an item from a picker

XCUIElement has a special method for interacting with UIPickerViews. For a picker with only one wheel, we can access it via element() then just adjust to our specified value.

Picker with one wheel

Picker with multiple Wheels

If the picker is more complex with multiple wheels we have a little set up to do. We need a way to identify the different wheels to the framework. Conforming to the UIPickerViewAccessibilityDelegate provides the right interface to set accessibility text on each picker.

This then sets the first wheel’s accessibility title to “Attackers Formation, 4 attackers.” Notice how the selected value sneaks in? To actually select the element we will need to match it with a BEGINSWITH predicate.

How to tap links in a web view

There’s nothing too exciting going on here, which leads to a clean API. The tap() method can be used with both UIWebView and WKWebView.

Complex interactions

How to assert a view is on the screen

One weird behavior of exists() is that it returns true even if the element is off the screen. You have to jump through some hoops to make sure the element is actually visible to the user.

We can test this by asserting that the element’s frame is contained in the window’s frame with CGRectContainsRect().

How to reorder table cells

Open up the Accessibility Inspector and hover over the Manage Roster screen. The little reordering icons automatically get their own accessibility titles based on the cell that they are contained in. Here the cell’s label is “Adrienne,” so the icon’s identifier becomes “Reorder Adrienne.”

We can now use these to drag cells up and down.

Cheat

How to “pull to refresh”

To replicate this gesture we need to drop into the XCUICoordinate API. You can use these objects to interact with specific points on the screen, not necessarily tied to any particular element. This becomes very useful when we want to drag an element to a specific location.

First, grab a reference to the first cell in your table. Then create a coordinate with zero offset, CGVectorMake(0, 0). This will normalize a point right on top of the first cell.

Using this point we can create an imaginary coordinate farther down the screen. I’ve found that a dy of six is the smallest amount needed to trigger the pull-to-refresh gesture.

Xcode Cheat Sheet Pdf

We can then execute the gesture by grabbing the first coordinate and dragging it to the second one.

What else?

Looking for a specific technique or notice something is missing? Leave a comment below or reach out to me on Twitter. I’m more than happy to add it to the post!

Testing, development, and product sneak peeks.
Sign up for my monthly newsletter.

Editor Navigation

CMD+SHIFT+O

CMD+J

CMD+SHIFT+J

CTRL+`

CMD+SHIFT+{`

CMD+SHIFT+}`

CMD+CTRL+Arrow Up

CMD+CTRL+Arrow Down

CMD+OPTION+SHIFT+Z

CMD+CTRL+Arrow Left

CMD+CTRL+Arrow Right

CMD+Click

CMD+L

CMD+OPTION+Arrow Left

CMD+OPTION+Arrow Right

CTRL+1

CTRL+2

CTRL+3

CTRL+4

CTRL+5

CTRL+6

Editing

CMD+OPTION+Return

CMD+Return

CTRL+Space

Esc

CMD+CTRL+E

CMD+]

CMD+[

CTRL+I

CMD+/

CMD+OPTION+[

CMD+OPTION+]

Search

CMD+F

CMD+OPTION+F

CMD+SHIFT+F

CMD+OPTION+SHIFT+F

Navigator

CMD+0

CMD+1

CMD+2

CMD+3

CMD+4

CMD+5

CMD+6

CMD+7

CMD+8

OPTION+Click

OPTION+SHIFT+Click

Debugging

CMD+SHIFT+Y

CMD+'

CMD+'

CMD+

CMD+Y

CMD+K

Documentation

CMD+OPTION+?

OPTION+Click

CMD+CTRL+?

OPTION+Double Click

CMD+CTRL+OPTION+/

Build & Run

CMD+B

CMD+R

CMD+.

CMD+SHIFT+B

CMD+I

CMD+U

CMD+SHIFT+K

Utilities

Xcode Ui Test Cheat Sheet

CMD+OPTION+0

CMD+OPTION+1

CMD+OPTION+2

CMD+OPTION+3

CMD+OPTION+4

CMD+OPTION+5

CMD+OPTION+6

CMD+CTRL+OPTION+1

CMD+CTRL+OPTION+2

CMD+CTRL+OPTION+3

CMD+CTRL+OPTION+4