-
Notifications
You must be signed in to change notification settings - Fork 27
/
L10nSnapshotTests.swift
375 lines (310 loc) · 15.8 KB
/
L10nSnapshotTests.swift
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import XCTest
/**
* IMPORTANT THING TO NOTE WHEN WRITING TESTS IN THIS CLASS:
* Does your test run in more than 1 language?
* If your test will only run successfully in 1 language then this test WILL NOT WORK
* for the l10n snapshots as they will be run in EVERY LANGUAGE we localise for.
* When writing your test, run against at least 2 different languages before submitting for Code Review.
* Thank You.
*/
class L10nSnapshotTests: XCTestCase {
override func setUp() {
super.setUp()
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
let app = XCUIApplication()
setupSnapshot(app)
app.launch()
}
// TODO Refactor this into a common superlcass for snapshots
func loadWebPage(url: String, waitForLoadToFinish: Bool = true) {
let LoadingTimeout: NSTimeInterval = 60
let exists = NSPredicate(format: "exists = true")
let loaded = NSPredicate(format: "value BEGINSWITH '100'")
let app = XCUIApplication()
UIPasteboard.generalPasteboard().string = url
app.textFields["url"].pressForDuration(2.0)
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
if waitForLoadToFinish {
let progressIndicator = app.progressIndicators.elementBoundByIndex(0)
expectationForPredicate(exists, evaluatedWithObject: progressIndicator, handler: nil)
expectationForPredicate(loaded, evaluatedWithObject: progressIndicator, handler: nil)
waitForExpectationsWithTimeout(LoadingTimeout, handler: nil)
}
}
func loadWebPage(url: String, waitForOtherElementWithAriaLabel ariaLabel: String) {
let app = XCUIApplication()
UIPasteboard.generalPasteboard().string = url
app.textFields["url"].pressForDuration(2.0)
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
sleep(3) // TODO Otherwise we detect the body in the currently loaded document, before the new page has loaded
let webView = app.webViews.elementBoundByIndex(0)
let element = webView.otherElements[ariaLabel]
expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(5.0) { (error) -> Void in
if error != nil {
XCTFail("Failed to detect element with ariaLabel=\(ariaLabel) on \(url): \(error)")
}
}
}
func loadWebPage(url: String, waitForLinkWithAriaLabel ariaLabel: String) {
let app = XCUIApplication()
UIPasteboard.generalPasteboard().string = url
app.textFields["url"].pressForDuration(2.0)
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
sleep(3) // TODO Otherwise we detect the body in the currently loaded document, before the new page has loaded
let webView = app.webViews.elementBoundByIndex(0)
let element = webView.links[ariaLabel]
expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(5.0) { (error) -> Void in
if error != nil {
XCTFail("Failed to detect link with ariaLabel=\(ariaLabel) on \(url): \(error)")
}
}
}
func test01Intro() {
let app = XCUIApplication()
snapshot("01Intro-1")
app.swipeLeft()
sleep(2)
snapshot("01Intro-2")
app.swipeLeft()
sleep(2)
snapshot("01Intro-3")
app.swipeLeft()
sleep(2)
snapshot("01Intro-4")
app.swipeLeft()
sleep(2)
snapshot("01Intro-5")
}
func test02DefaultTopSites() {
snapshot("02DefaultTopSites-01")
}
func test03Settings() {
let app = XCUIApplication()
app.buttons["URLBarView.tabsButton"].tap()
app.buttons["TabTrayController.settingsButton"].tap()
var index = 1
// Screenshot the settings by scrolling through it
snapshot("03Settings-\(index)")
let element = app.images["SettingsTableFooterView.logo"]
while !element.visible() {
app.swipeUp()
sleep(2)
index += 1
snapshot("03Settings-\(index)")
}
// Screenshot all the settings that have a separate page
for cellName in ["Search", "Logins", "TouchIDPasscode", "ClearPrivateData"] {
app.tables["AppSettingsTableViewController.tableView"].cells[cellName].tap()
index++
snapshot("03Settings-\(index)")
app.navigationBars.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
}
}
func test04PrivateBrowsingTabsEmptyState() {
let app = XCUIApplication()
app.buttons["URLBarView.tabsButton"].tap() // Open tabs tray
app.buttons["TabTrayController.togglePrivateMode"].tap() // Switch to private mode
snapshot("04PrivateBrowsingTabsEmptyState-01")
}
func test05PanelsEmptyState() {
let app = XCUIApplication()
app.textFields["url"].tap()
app.buttons["HomePanels.Bookmarks"].tap()
snapshot("05PanelsEmptyState-01")
app.buttons["HomePanels.History"].tap()
snapshot("05PanelsEmptyState-02")
app.buttons["HomePanels.SyncedTabs"].tap()
snapshot("05PanelsEmptyState-03")
app.buttons["HomePanels.ReadingList"].tap()
snapshot("05PanelsEmptyState-04")
}
func test06URLBar() {
let app = XCUIApplication()
app.textFields["url"].tap()
snapshot("06URLBar-01")
app.textFields["address"].typeText("moz")
snapshot("06URLBar-02")
}
func test07URLBarContextMenu() {
let app = XCUIApplication()
// Long press with nothing on the clipboard
app.textFields["url"].pressForDuration(2.0)
snapshot("07LocationBarContextMenu-01")
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
sleep(2)
// Long press with a URL on the clipboard
UIPasteboard.generalPasteboard().string = "https://www.mozilla.com"
app.textFields["url"].pressForDuration(2.0)
snapshot("07LocationBarContextMenu-02")
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
sleep(2)
}
func test08WebViewContextMenu() {
let app = XCUIApplication()
// Link
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/link.html", waitForOtherElementWithAriaLabel: "body")
app.webViews.elementBoundByIndex(0).links["link"].pressForDuration(2.0)
snapshot("08WebViewContextMenu-01")
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(app.sheets.elementBoundByIndex(0).buttons.count-1).tap()
// Image
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/image.html", waitForOtherElementWithAriaLabel: "body")
app.webViews.elementBoundByIndex(0).images["image"].pressForDuration(2.0)
snapshot("08WebViewContextMenu-02")
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(app.sheets.elementBoundByIndex(0).buttons.count-1).tap()
// Image inside Link
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/imageWithLink.html", waitForOtherElementWithAriaLabel: "body")
app.webViews.elementBoundByIndex(0).links["link"].pressForDuration(2.0)
snapshot("08WebViewContextMenu-03")
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(app.sheets.elementBoundByIndex(0).buttons.count-1).tap()
}
func test09WebViewAuthenticationDialog() {
loadWebPage("https://phonebook.mozilla.org", waitForLoadToFinish: false)
let predicate = NSPredicate(format: "exists == 1")
let query = XCUIApplication().alerts.elementBoundByIndex(0)
expectationForPredicate(predicate, evaluatedWithObject: query, handler: nil)
self.waitForExpectationsWithTimeout(3, handler: nil)
snapshot("09WebViewAuthenticationDialog-01", waitForLoadingIndicator: false)
}
func test10ReloadButtonContextMenu() {
let app = XCUIApplication()
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/index.html", waitForOtherElementWithAriaLabel: "body")
app.buttons["TabToolbar.stopReloadButton"].pressForDuration(2.0)
snapshot("10ContextMenuReloadButton-01", waitForLoadingIndicator: false)
app.sheets.elementBoundByIndex(0).buttons.elementBoundByIndex(0).tap()
app.buttons["TabToolbar.stopReloadButton"].pressForDuration(2.0)
snapshot("10ContextMenuReloadButton-02", waitForLoadingIndicator: false)
}
func test11LocationDialog() {
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/geolocation.html", waitForOtherElementWithAriaLabel: "body")
snapshot("11LocationDialog-01")
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/index.html", waitForOtherElementWithAriaLabel: "body")
}
// This is a fragile testcase because it depends on the specific position of items in the
// share sheet. This is pretty stable on the 9.2.1 simulator but may change with iOS releases.
func test12ShareSheetAndExtensions() {
let app = XCUIApplication()
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/index.html", waitForOtherElementWithAriaLabel: "body")
app.buttons["TabToolbar.shareButton"].tap()
app.collectionViews.elementBoundByIndex(0).swipeLeft()
app.collectionViews.elementBoundByIndex(0).buttons.elementBoundByIndex(app.collectionViews.elementBoundByIndex(0).buttons.count-1).tap()
app.tables.switches.elementBoundByIndex(app.tables.switches.count-1).tap()
snapshot("12ShareSheetAndExtensions-01") // Shows Share Extension in a list
app.navigationBars.elementBoundByIndex(0).buttons.elementBoundByIndex(1).tap()
app.collectionViews.elementBoundByIndex(1).swipeLeft()
app.collectionViews.elementBoundByIndex(1).buttons.elementBoundByIndex(app.collectionViews.elementBoundByIndex(1).buttons.count-1).tap()
app.tables.switches.elementBoundByIndex(app.tables.switches.count-1).tap()
app.tables.switches.elementBoundByIndex(app.tables.switches.count-2).tap()
snapshot("12ShareSheetAndExtensions-02") // Shows Action Extensions in a list
app.navigationBars.elementBoundByIndex(0).buttons.elementBoundByIndex(1).tap()
snapshot("12ShareSheetAndExtensions-03") // Shows all extensions in the share sheet
// At this point our extensions are all enabled. ShareTo has index 2, ViewLater 2, SendTab 3
// ShareTo
app.collectionViews.elementBoundByIndex(0).buttons.elementBoundByIndex(2).tap()
snapshot("12ShareSheetAndExtensions-03") // ShareTo
app.buttons["ShareDialogController.navigationItem.leftBarButtonItem"].tap()
sleep(2)
// SendTo- Depending on the locale, the Send To extension is in position 2 or 3
app.buttons["TabToolbar.shareButton"].tap()
app.collectionViews.elementBoundByIndex(1).buttons.elementBoundByIndex(2).tap()
sleep(2)
if !app.buttons["InstructionsViewController.navigationItem.leftBarButtonItem"].exists {
app.buttons["TabToolbar.shareButton"].tap()
app.collectionViews.elementBoundByIndex(1).buttons.elementBoundByIndex(3).tap()
}
snapshot("12ShareSheetAndExtensions-04") // SendTo - Empty state because not logged in
app.buttons["InstructionsViewController.navigationItem.leftBarButtonItem"].tap()
}
func test13PasswordSnackbar() {
let app = XCUIApplication()
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/password.html", waitForOtherElementWithAriaLabel: "body")
app.webViews.elementBoundByIndex(0).buttons["submit"].tap()
snapshot("13PasswordSnackbar-01")
app.buttons["SaveLoginPrompt.saveLoginButton"].tap()
// The password is pre-filled with a random value so second this this will cause the update prompt
loadWebPage("http://people.mozilla.org/~sarentz/fxios/testpages/password.html", waitForOtherElementWithAriaLabel: "body")
app.webViews.elementBoundByIndex(0).buttons["submit"].tap()
snapshot("13PasswordSnackbar-02")
}
func test50ClearPrivateData() {
let app = XCUIApplication()
var index = 1
func mySnapshot(name: String) {
snapshot(name)
index += 1
}
func clearPrivateData() {
let clearPrivateDataCell = app.tables.cells["ClearPrivateData"]
clearPrivateDataCell.tap()
mySnapshot("50ClearPrivateData-\(index)")
let clearPrivateDataButton = app.tables.cells["ClearPrivateData"]
clearPrivateDataButton.tap()
mySnapshot("50ClearPrivateData-\(index)")
let button = app.alerts.elementBoundByIndex(0).collectionViews.buttons.elementBoundByIndex(0)
button.tap()
let navBar = app.navigationBars.elementBoundByIndex(0)
navBar.buttons.elementBoundByIndex(0).tap()
}
let loginUsername = "testtesto@mockmyid.com"
let loginPassword = "testtesto@mockmyid.com"
app.buttons["URLBarView.tabsButton"].tap()
app.buttons["TabTrayController.settingsButton"].tap()
mySnapshot("50ClearPrivateData-\(index)")
let logOutCell = app.tables.cells["LogOut"]
if logOutCell.exists {
logOutCell.tap()
sleep(2)
app.alerts.elementBoundByIndex(0).collectionViews.buttons.elementBoundByIndex(1).tap()
}
sleep(2)
clearPrivateData()
app.tables.cells["SignInToFirefox"].tap()
let passwordField = app.webViews.secureTextFields.elementBoundByIndex(0)
let exists = NSPredicate(format: "exists == 1")
expectationForPredicate(exists, evaluatedWithObject: passwordField, handler: nil)
waitForExpectationsWithTimeout(10, handler: nil)
let usernameField = app.webViews.textFields.elementBoundByIndex(0)
if !usernameField.exists {
app.webViews.links.elementBoundByIndex(1).tap()
expectationForPredicate(exists, evaluatedWithObject: usernameField, handler: nil)
waitForExpectationsWithTimeout(10, handler: nil)
}
usernameField.tap()
usernameField.typeText(loginUsername)
passwordField.tap()
sleep(2)
passwordField.typeText(loginPassword)
addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
mySnapshot("50ClearPrivateData-\(index)")
alert.buttons.elementBoundByIndex(1).tap()
return true
}
app.webViews.buttons.elementBoundByIndex(0).tap()
mySnapshot("50ClearPrivateData-\(index)")
sleep(2)
clearPrivateData()
app.tables["AppSettingsTableViewController.tableView"].swipeUp()
app.tables["AppSettingsTableViewController.tableView"].swipeUp()
mySnapshot("50ClearPrivateData-\(index)")
app.tables["AppSettingsTableViewController.tableView"].cells["LogOut"].tap()
mySnapshot("50ClearPrivateData-\(index)")
app.alerts.elementBoundByIndex(0).collectionViews.buttons.elementBoundByIndex(1).tap()
mySnapshot("50ClearPrivateData-\(index)")
}
}
extension XCUIElement {
func scrollToElement(element: XCUIElement) {
while !element.visible() {
swipeUp()
}
}
func visible() -> Bool {
guard self.exists && !CGRectIsEmpty(self.frame) else { return false }
return CGRectContainsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
}
}