This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUITests.swift
144 lines (128 loc) · 5.62 KB
/
UITests.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
//
// UITests.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import XCTest
/// Helper values for the UI tests
enum UITests {
/// Timeout constants for different test requirements
enum Timeouts {
/// Mostly, we use timeouts to wait for element existence. This is about 3x longer than needed, for CI resilience
static let elementExistence: Double = 5.0
/// The fire animation time has environmental dependencies, so we want to wait for completion so we don't try to type into it
static let fireAnimation: Double = 30.0
}
/// A page simple enough to test favorite, bookmark, and history storage
/// - Parameter title: The title of the page to match
/// - Returns: A URL that can be served by `tests-server`
static func simpleServedPage(titled title: String) -> URL {
return URL.testsServer
.appendingTestParameters(data: """
<html>
<head>
<title>\(title)</title>
</head>
<body>
<p>Sample text for \(title)</p>
</body>
</html>
""".utf8data)
}
static func randomPageTitle(length: Int) -> String {
return String(UUID().uuidString.prefix(length))
}
/// This is intended for setting an autocomplete checkbox state that extends across all test cases and is only run once in the class override
/// setup() of the case. Setting the autocomplete checkbox state for an individual test shouldn't start and terminate the app, as this function
/// does.
/// - Parameter requestedToggleState: How the autocomplete checkbox state should be set
static func setAutocompleteToggleBeforeTestcaseRuns(_ requestedToggleState: Bool) {
let app = XCUIApplication()
app.launchEnvironment["UITEST_MODE"] = "1"
app.launch()
let settings = app.menuItems["MainMenu.preferencesMenuItem"]
XCTAssertTrue(
settings.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Reset bookmarks menu item didn't become available in a reasonable timeframe."
)
settings.click()
let generalPreferencesButton = app.buttons["PreferencesSidebar.generalButton"]
let autocompleteToggle = app.checkBoxes["PreferencesGeneralView.showAutocompleteSuggestions"]
XCTAssertTrue(
generalPreferencesButton.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"The user settings appearance section button didn't become available in a reasonable timeframe."
)
generalPreferencesButton.click(forDuration: 0.5, thenDragTo: generalPreferencesButton)
let currentToggleState = try? XCTUnwrap(
autocompleteToggle.value as? Bool,
"It wasn't possible to get the \"Autocomplete\" value as a Bool"
)
switch (requestedToggleState, currentToggleState) { // Click autocomplete toggle if it is different than our request
case (false, true), (true, false):
autocompleteToggle.click()
default:
break
}
app.terminate()
}
/// A debug function that is going to need some other functionality in order to be useful for debugging address bar focus issues
static func openVanillaBrowser() {
let app = XCUIApplication()
let openVanillaBrowser = app.menuItems["MainMenu.openVanillaBrowser"]
openVanillaBrowser.clickAfterExistenceTestSucceeds()
app.typeKey("w", modifierFlags: [.command, .option])
}
/// Avoid some first-run states that we aren't testing.
static func firstRun() {
let notificationCenter = XCUIApplication(bundleIdentifier: "com.apple.UserNotificationCenter")
if notificationCenter.exists { // If tests-server is asking for network permissions, deny them.
notificationCenter.typeKey(.escape, modifierFlags: [])
}
let app = XCUIApplication()
app.launchEnvironment["UITEST_MODE"] = "1"
app.launch()
app.typeKey("n", modifierFlags: .command)
app.typeKey("w", modifierFlags: [.command, .option])
app.terminate()
}
}
class TestFailureObserver: NSObject, XCTestObservation {
func testCase(_ testCase: XCTestCase, didRecord issue: XCTIssue) {
print("Failed test with name: \(testCase.name)")
let screenshotName = "\(testCase.name)-failure"
testCase.takeScreenshot(screenshotName)
}
}
class UITestCase: XCTestCase {
private static let failureObserver = TestFailureObserver()
override class func setUp() {
super.setUp()
XCTestObservationCenter.shared.addTestObserver(failureObserver)
}
override class func tearDown() {
XCTestObservationCenter.shared.removeTestObserver(failureObserver)
super.tearDown()
}
}
extension XCTestCase {
func takeScreenshot(_ name: String) {
let fullScreenshot = XCUIScreen.main.screenshot()
let screenshot = XCTAttachment(screenshot: fullScreenshot)
screenshot.name = name
screenshot.lifetime = .keepAlways
add(screenshot)
}
}