# UI Testing the Clean Way

DevFeed: [UI Testing the Clean Way](<https://devfeed.tech/articles/ui-testing-the-clean-way-22309.md>)

Original publisher: [Read original article](<https://www.thecodedself.com/UI-Testing-The-Clean-Way/>)

Author: Keegan Rush

Published: 2018-10-23T00:00:00Z

Content type: tutorial

Language: en

Sources: [The Coded Self](<https://devfeed.tech/sources/the-coded-self.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [Xcode](<https://devfeed.tech/topics/xcode.md>), [To-Do](<https://devfeed.tech/topics/todo.md>)

Tags: [bugs](<https://devfeed.tech/tags/bugs.md>), [reuse](<https://devfeed.tech/tags/reuse.md>), [swift](<https://devfeed.tech/tags/swift.md>), [testing](<https://devfeed.tech/tags/testing.md>), [ui-testing](<https://devfeed.tech/tags/ui-testing.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

## AI overview

This tutorial discusses keeping Swift UI tests clean and readable using a To-Do list app. It explains that Xcode's recorded UI tests can produce unreadable, brittle, duplicated code and may fail to capture some interactions, then introduces a cleaner testing approach.

## Source excerpt

I write software, and sometimes I write bugs. But, when I do, I catch them early with testing. I do manual tests before I commit, I write unit tests as I write my code. Lately, I've also been getting into writing UI tests. One critique of UI testing is how messy and brittle it can be. Building the tests can be complicated. You might spend hours on a test just for a design to change that breaks the test and ruins all your hard work. So, I want to tell you about some problems you might face in keeping your UI tests clean and readable, and how we can leverage Swift for this endeavour. I'll walk you through my approach to UI testing with everyone's favorite example project - a To-Do list app. I know, boring, right? Well, it works perfectly for the examples we'll be looking at, so... You can download the example project here. It's a simple app that allows us to create, edit, and delete a to-do. Just press record UI testing in Xcode is easy. Click inside the body of an empty UI test, click the Record button, and you're up and running. Let's start off by recording a UI test to add a new to-do. That was easy! And it sure did create a lot of code. That must be good, right? func testExample() { let app = XCUIApplication() app.navigationBars["Todo List"].buttons["Add"].tap() let textField = app.otherElements.containing(.navigationBar, identifier:"New Todo") .children(matching: .other).element.children(matching: .other).element .children(matching: .other).element.children(matching: .textField).element textField.tap() textField.tap() let datePickersQuery = app.datePickers datePickersQuery.pickerWheels["October"]/*@START_MENU_TOKEN@*/.press(forDuration: 0.6);/*[[".tap()",".press(forDuration: 0.6);"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/ datePickersQuery.pickerWheels["9"]/*@START_MENU_TOKEN@*/.press(forDuration: 0.5);/*[[".tap()",".press(forDuration: 0.5);"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/ app.buttons["Done"].tap() } I'll be honest, most of that is unreadable to me. Run