1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import XCTest
final class ProfileEditUITests: XCTestCase {
func testEditProfileRetainsChangesAfterSaveFailureAndReloadsSavedValues() throws {
try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "Requires the iPhone layout")
let app = XCUIApplication()
app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-save-failure"]
app.launch()
app.openSettings()
let edit = app.buttons["settingsEditProfile"]
XCTAssertTrue(edit.waitForExistence(timeout: 5))
XCTAssertTrue(edit.isHittable)
XCTAssertTrue(edit.label.contains("Demo Account"))
XCTAssertLessThan(edit.frame.maxY, app.switches["showAvatars"].frame.minY)
capture(app, named: "Settings account row")
edit.tap()
let editor = app.navigationBars["Edit Profile"]
let form = app.collectionViews["profileEditForm"]
let name = app.textFields["profileEditDisplayName"]
XCTAssertTrue(name.waitForExistence(timeout: 5))
let avatar = app.buttons["profileEditAvatar"]
XCTAssertTrue(avatar.exists)
XCTAssertLessThan(avatar.frame.maxX, name.frame.minX)
XCTAssertEqual(avatar.frame.midY, name.frame.midY, accuracy: 20)
capture(app, named: "Profile editor")
let bio = app.textViews["profileEditBio"]
XCTAssertTrue(bio.exists)
let bioFrame = bio.frame
bio.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.7)).press(
forDuration: 0.05,
thenDragTo: bio.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.4)),
withVelocity: .slow,
thenHoldForDuration: 0)
XCTAssertEqual(bio.frame.minY, bioFrame.minY, accuracy: 2)
capture(app, named: "Scrolled bio")
name.tap()
name.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: "Demo Account".count) + "New Name")
XCTAssertTrue(form.exists)
form.swipeUp()
let link = form.staticTexts["GitHub"]
for _ in 0..<3 where !link.exists { form.swipeUp() }
XCTAssertTrue(link.exists)
link.tap()
let title = app.textFields["profileLinkTitle"]
XCTAssertTrue(title.waitForExistence(timeout: 5))
title.tap()
title.typeText(" Projects")
app.navigationBars["Edit Link"].buttons["Done"].tap()
let reordered = form.staticTexts["GitHub Projects"]
let first = form.staticTexts["My Photos!"]
let editLinks = app.buttons["profileEditReorderLinks"]
for _ in 0..<3 where !reordered.exists { form.swipeUp() }
XCTAssertTrue(first.exists && reordered.exists && editLinks.exists)
editLinks.tap()
let row = form.cells.containing(.staticText, identifier: "GitHub Projects").firstMatch
row.coordinate(withNormalizedOffset: CGVector(dx: 0.93, dy: 0.5)).press(
forDuration: 0.3, thenDragTo: first.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0)))
XCTAssertLessThan(reordered.frame.midY, first.frame.midY)
editLinks.tap()
let save = editor.buttons["Save"]
XCTAssertTrue(save.waitForExistence(timeout: 5))
save.tap()
let failure = app.alerts["Profile Editor"]
XCTAssertTrue(failure.waitForExistence(timeout: 5))
failure.buttons["OK"].tap()
XCTAssertTrue(reordered.exists)
save.tap()
XCTAssertTrue(edit.waitForExistence(timeout: 5))
XCTAssertTrue(edit.label.contains("New Name"))
edit.tap()
XCTAssertTrue(name.waitForExistence(timeout: 5))
XCTAssertEqual(name.value as? String, "New Name")
for _ in 0..<3 where !reordered.exists { form.swipeUp() }
XCTAssertTrue(reordered.exists)
XCTAssertLessThan(reordered.frame.midY, first.frame.midY)
}
func testAvatarMenuRemovesPhotoAndAppliesCropFromPhotoPicker() throws {
try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "Requires the iPhone layout")
let app = XCUIApplication()
app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-avatar"]
app.launch()
app.openSettings()
let edit = app.buttons["settingsEditProfile"]
edit.tap()
let avatar = app.buttons["profileEditAvatar"]
XCTAssertTrue(avatar.waitForExistence(timeout: 5))
avatar.tap()
let choose = app.buttons["Choose Photo"].firstMatch
let remove = app.buttons["Remove Photo"].firstMatch
XCTAssertTrue(choose.waitForExistence(timeout: 5))
XCTAssertTrue(remove.exists)
capture(app, named: "Avatar menu")
remove.tap()
app.navigationBars["Edit Profile"].buttons["Save"].tap()
edit.tap()
XCTAssertTrue(avatar.waitForExistence(timeout: 5))
avatar.tap()
XCTAssertTrue(choose.waitForExistence(timeout: 5))
XCTAssertFalse(remove.exists)
choose.tap()
let picker = app.navigationBars["Photos"]
XCTAssertTrue(picker.waitForExistence(timeout: 5))
let photo = app.images.matching(identifier: "PXGGridLayout-Info").firstMatch
XCTAssertTrue(photo.waitForExistence(timeout: 5))
photo.tap()
let crop = app.navigationBars["Crop Avatar"]
XCTAssertTrue(crop.waitForExistence(timeout: 10))
let cropArea = app.descendants(matching: .any)["avatarCropArea"].firstMatch
XCTAssertTrue(cropArea.exists)
XCTAssertEqual(cropArea.frame.width, cropArea.frame.height, accuracy: 1)
XCTAssertEqual(cropArea.value as? String, "100 percent")
app.buttons["Rotate"].tap()
app.buttons["Flip"].tap()
cropArea.pinch(withScale: 1.6, velocity: 1)
XCTAssertNotEqual(cropArea.value as? String, "100 percent")
capture(app, named: "Avatar crop")
crop.buttons["Use Photo"].tap()
XCTAssertTrue(avatar.waitForExistence(timeout: 5))
app.navigationBars["Edit Profile"].buttons["Save"].tap()
XCTAssertTrue(edit.waitForExistence(timeout: 5))
edit.tap()
XCTAssertTrue(avatar.waitForExistence(timeout: 5))
avatar.tap()
XCTAssertTrue(remove.waitForExistence(timeout: 5))
}
private func capture(_ app: XCUIApplication, named name: String) {
let screenshot = XCTAttachment(screenshot: app.screenshot())
screenshot.name = name
screenshot.lifetime = .keepAlways
add(screenshot)
}
}