-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathBaseUITestCase.swift
139 lines (114 loc) · 4.3 KB
/
BaseUITestCase.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
//
// BaseTestCase.swift
// MullvadVPNUITests
//
// Created by Niklas Berglund on 2024-01-12.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import Foundation
import XCTest
class BaseUITestCase: XCTestCase {
let app = XCUIApplication()
static let defaultTimeout = 5.0
// swiftlint:disable force_cast
let displayName = Bundle(for: BaseUITestCase.self)
.infoDictionary?["DisplayName"] as! String
let noTimeAccountNumber = Bundle(for: BaseUITestCase.self)
.infoDictionary?["NoTimeAccountNumber"] as! String
let hasTimeAccountNumber = Bundle(for: BaseUITestCase.self)
.infoDictionary?["HasTimeAccountNumber"] as! String
let fiveWireGuardKeysAccountNumber = Bundle(for: BaseUITestCase.self)
.infoDictionary?["FiveWireGuardKeysAccountNumber"] as! String
let iOSDevicePinCode = Bundle(for: BaseUITestCase.self)
.infoDictionary?["IOSDevicePinCode"] as! String
// swiftlint:enable force_cast
/// Handle iOS add VPN configuration permission alert - allow and enter device PIN code
func allowAddVPNConfigurations() {
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let alertAllowButton = springboard.buttons.element(boundBy: 0)
if alertAllowButton.waitForExistence(timeout: Self.defaultTimeout) {
alertAllowButton.tap()
}
if iOSDevicePinCode.isEmpty == false {
_ = springboard.buttons["1"].waitForExistence(timeout: Self.defaultTimeout)
springboard.typeText(iOSDevicePinCode)
}
}
// MARK: - Setup & teardown
/// Suite level teardown ran after test have executed
override class func tearDown() {
uninstallApp()
}
/// Test level setup
override func setUp() {
continueAfterFailure = false
app.launch()
}
/// Test level teardown
override func tearDown() {
app.terminate()
}
/// Check if currently logged on to an account. Note that it is assumed that we are logged in if login view isn't currently shown.
func isLoggedIn() -> Bool {
return !app
.otherElements[AccessibilityIdentifier.loginView.rawValue]
.waitForExistence(timeout: 1.0)
}
func agreeToTermsOfServiceIfShown() {
let termsOfServiceIsShown = app.otherElements[
AccessibilityIdentifier
.termsOfServiceView.rawValue
]
.waitForExistence(timeout: 1)
if termsOfServiceIsShown {
TermsOfServicePage(app)
.tapAgreeButton()
Alert(app) // Changes alert
.tapOkay()
LoginPage(app)
}
}
/// Login with specified account number. It is a prerequisite that the login page is currently shown.
func login(accountNumber: String) {
LoginPage(app)
.tapAccountNumberTextField()
.enterText(accountNumber)
.tapAccountNumberSubmitButton()
.verifyDeviceLabelShown()
}
func logoutIfLoggedIn() {
if isLoggedIn() {
HeaderBar(app)
.tapAccountButton()
AccountPage(app)
.tapLogOutButton()
LoginPage(app)
}
}
static func uninstallApp() {
let appName = "Mullvad VPN"
let timeout = TimeInterval(5)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let spotlight = XCUIApplication(bundleIdentifier: "com.apple.Spotlight")
springboard.swipeDown()
spotlight.textFields["SpotlightSearchField"].typeText(appName)
let appIcon = spotlight.icons[appName].firstMatch
if appIcon.waitForExistence(timeout: timeout) {
appIcon.press(forDuration: 2)
} else {
XCTFail("Failed to find app icon named \(appName)")
}
let deleteAppButton = spotlight.buttons["Delete App"]
if deleteAppButton.waitForExistence(timeout: timeout) {
deleteAppButton.tap()
} else {
XCTFail("Failed to find 'Delete App'")
}
let finalDeleteButton = springboard.alerts.buttons["Delete"]
if finalDeleteButton.waitForExistence(timeout: timeout) {
finalDeleteButton.tap()
} else {
XCTFail("Failed to find 'Delete'")
}
}
}