pEpForiOSUITests/NewAccountSetupUITest.swift
author Dirk Zimmermann <dz@pep.security>
Thu, 21 Mar 2019 12:44:24 +0100
branchIOS-1480
changeset 8033 bf94050222a1
parent 7161 0464791a0e21
child 8491 79e316b1974c
permissions -rw-r--r--
IOS-1480 Close branch.
dirk@213
     1
//
dirk@213
     2
//  NewAccountSetupUITest.swift
dirk@213
     3
//  pEpForiOS
dirk@213
     4
//
dirk@213
     5
//  Created by Dirk Zimmermann on 20/05/16.
dirk@213
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@213
     7
//
dirk@213
     8
dirk@213
     9
import XCTest
dirk@816
    10
dirk@213
    11
class NewAccountSetupUITest: XCTestCase {
dirk@5300
    12
    // MARK: - Setup
dirk@3846
    13
dirk@213
    14
    override func setUp() {
dirk@213
    15
        super.setUp()
dirk@213
    16
dirk@213
    17
        // In UI tests it is usually best to stop immediately when a failure occurs.
dirk@213
    18
        continueAfterFailure = false
dirk@213
    19
    }
dirk@213
    20
dirk@5300
    21
    // MARK: - Tests
dirk@5300
    22
dirk@5300
    23
    func testInitialAccountSetup1() {
dirk@5555
    24
        app().launch()
dirk@5496
    25
dirk@5496
    26
        dismissInitialSystemAlerts()
dirk@5496
    27
dirk@5300
    28
        let account = SecretUITestData.workingAccount1
dirk@5300
    29
        newAccountSetup(account: account)
dirk@5300
    30
        waitForever()
dirk@5300
    31
    }
dirk@5300
    32
dirk@5300
    33
    func testInitialAccountSetup2() {
dirk@5454
    34
        app().launch()
dirk@5496
    35
dirk@5496
    36
        dismissInitialSystemAlerts()
dirk@5496
    37
dirk@5300
    38
        let account = SecretUITestData.workingAccount2
dirk@5300
    39
        newAccountSetup(account: account)
dirk@5300
    40
        waitForever()
dirk@5300
    41
    }
dirk@5300
    42
dirk@5300
    43
    func testAdditionalAccount() {
dirk@5454
    44
        app().launch()
dirk@5591
    45
        addAccount()
dirk@6415
    46
        let account = SecretUITestData.workingAccount3
dirk@5300
    47
        newAccountSetup(account: account)
dirk@5300
    48
        waitForever()
dirk@5300
    49
    }
dirk@5300
    50
dirk@5573
    51
    func testAdditionalManualAccount() {
dirk@5573
    52
        app().launch()
dirk@5573
    53
        addAdditionalManual(account: SecretUITestData.manualAccount)
dirk@5573
    54
    }
dirk@5573
    55
dirk@5573
    56
    func testAutoAccountPlusManual() {
dirk@5573
    57
        app().launch()
dirk@5573
    58
dirk@5573
    59
        dismissInitialSystemAlerts()
dirk@5573
    60
dirk@5573
    61
        let account1 = SecretUITestData.workingAccount1
dirk@5573
    62
        newAccountSetup(account: account1)
dirk@5573
    63
dirk@5573
    64
        addAdditionalManual(account: SecretUITestData.manualAccount)
dirk@5573
    65
    }
dirk@5573
    66
dirk@5300
    67
    func testTwoInitialAccounts() {
dirk@5454
    68
        app().launch()
dirk@5496
    69
dirk@5496
    70
        dismissInitialSystemAlerts()
dirk@5496
    71
dirk@5300
    72
        let account1 = SecretUITestData.workingAccount1
dirk@5300
    73
        newAccountSetup(account: account1)
dirk@5300
    74
dirk@5591
    75
        addAccount()
dirk@5300
    76
dirk@5300
    77
        let account2 = SecretUITestData.workingAccount2
dirk@5300
    78
        newAccountSetup(account: account2)
dirk@5300
    79
        waitForever()
dirk@5300
    80
    }
dirk@5300
    81
dirk@5455
    82
    func testNewAccountSetupManual() {
dirk@5300
    83
        let theApp = app()
dirk@5462
    84
dirk@5462
    85
        theApp.launch()
dirk@5462
    86
dirk@5463
    87
        dismissInitialSystemAlerts()
dirk@5462
    88
dirk@5300
    89
        let account = SecretUITestData.manualAccount
dirk@5300
    90
        newAccountSetup(account: account)
dirk@5300
    91
dirk@5573
    92
        switchToManualConfig()
dirk@5455
    93
dirk@5455
    94
        manualNewAccountSetup(account)
dirk@5300
    95
dirk@5300
    96
        waitForever()
dirk@5300
    97
    }
dirk@5300
    98
dirk@5300
    99
    func testTriggerGmailOauth2() {
dirk@5454
   100
        app().launch()
dirk@5496
   101
dirk@5496
   102
        dismissInitialSystemAlerts()
dirk@5496
   103
dirk@5300
   104
        let account = SecretUITestData.gmailOAuth2Account
dirk@5300
   105
        newAccountSetup(account: account, enterPassword: false)
dirk@5300
   106
        waitForever()
dirk@5300
   107
    }
dirk@5300
   108
dirk@5300
   109
    func testTriggerYahooOauth2() {
dirk@5454
   110
        app().launch()
dirk@5496
   111
dirk@5496
   112
        dismissInitialSystemAlerts()
dirk@5496
   113
dirk@5300
   114
        let account = SecretUITestData.yahooOAuth2Account
dirk@5300
   115
        newAccountSetup(account: account, enterPassword: false)
dirk@5300
   116
        waitForever()
dirk@5300
   117
    }
dirk@5300
   118
dirk@5300
   119
    // MARK: - Helpers
dirk@5300
   120
dirk@5300
   121
    func app() -> XCUIApplication {
dirk@5300
   122
        let app = XCUIApplication()
dirk@5300
   123
        app.launchEnvironment = ["ASAN_OPTIONS": "detect_odr_violation=0"]
dirk@5300
   124
        return app
dirk@5300
   125
    }
dirk@5300
   126
dirk@214
   127
    /*
dirk@214
   128
     Use if you want to wait forever. May be useful for debugging.
dirk@214
   129
     */
dirk@214
   130
    func waitForever() {
dirk@1938
   131
        let _ = expectation(description: "Never happens")
dirk@784
   132
        waitForExpectations(timeout: 3000, handler: nil)
dirk@214
   133
    }
dirk@214
   134
dirk@5573
   135
    func typeTextIfEmpty(textField: XCUIElement, text: String) {
dirk@2309
   136
        if (textField.value as? String ?? "") == "" {
dirk@2309
   137
            textField.typeText(text)
dirk@2309
   138
        }
dirk@2309
   139
    }
dirk@2309
   140
dirk@3706
   141
    func manualNewAccountSetup(_ account: UIAccount) {
dirk@3846
   142
        let theApp = app()
dirk@3846
   143
        let tablesQuery = theApp.tables
dirk@216
   144
dirk@5573
   145
        var tf = tablesQuery.cells.textFields["username"]
dirk@2309
   146
        typeTextIfEmpty(textField: tf, text: account.nameOfTheUser)
dirk@364
   147
dirk@364
   148
        tf = tablesQuery.cells.textFields["email"]
dirk@364
   149
        tf.tap()
dirk@2309
   150
        typeTextIfEmpty(textField: tf, text: account.email)
dirk@216
   151
dirk@216
   152
        tf = tablesQuery.cells.secureTextFields["password"]
dirk@216
   153
        tf.tap()
dirk@2309
   154
        typeTextIfEmpty(textField: tf, text: account.password)
dirk@216
   155
dirk@3846
   156
        theApp.navigationBars.buttons["Next"].tap()
dirk@216
   157
dirk@216
   158
        tf = tablesQuery.textFields["imapServer"]
dirk@951
   159
        tf.typeText(account.imapServerName)
dirk@216
   160
        tf = tablesQuery.textFields["imapPort"]
dirk@216
   161
        tf.tap()
dirk@5457
   162
        tf.clearAndEnter(text: String(account.imapPort))
dirk@229
   163
dirk@229
   164
        tablesQuery.buttons["imapTransportSecurity"].tap()
dirk@3846
   165
        let sheet = theApp.sheets["Transport protocol"]
dirk@814
   166
        sheet.buttons[account.imapTransportSecurityString].tap()
dirk@229
   167
dirk@3846
   168
        theApp.navigationBars.buttons["Next"].tap()
dirk@216
   169
dirk@216
   170
        tf = tablesQuery.textFields["smtpServer"]
dirk@951
   171
        tf.typeText(account.smtpServerName)
dirk@216
   172
        tf = tablesQuery.textFields["smtpPort"]
dirk@216
   173
        tf.tap()
dirk@5457
   174
        tf.clearAndEnter(text: String(account.smtpPort))
dirk@229
   175
dirk@229
   176
        tablesQuery.buttons["smtpTransportSecurity"].tap()
dirk@814
   177
        sheet.buttons[account.smtpTransportSecurityString].tap()
dirk@229
   178
dirk@3846
   179
        let nextButton = theApp.navigationBars.buttons["Next"]
dirk@227
   180
        nextButton.tap()
dirk@228
   181
    }
dirk@227
   182
dirk@3706
   183
    func newAccountSetup(account: UIAccount, enterPassword: Bool = true) {
dirk@5573
   184
        signIn(account: account, enterPassword: enterPassword)
dirk@2231
   185
    }
dirk@2231
   186
dirk@5497
   187
    /**
dirk@5497
   188
     Dismisses the initial system alerts (access to contacts, allow notifications).
dirk@5497
   189
     */
dirk@5497
   190
    func dismissInitialSystemAlerts() {
dirk@5497
   191
        dismissSystemAlert(buttonTitle: "Allow")
dirk@5497
   192
        dismissSystemAlert(buttonTitle: "OK")
dirk@5497
   193
    }
dirk@5497
   194
dirk@5497
   195
    func dismissSystemAlert(buttonTitle: String) {
dirk@5497
   196
        let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
dirk@5497
   197
        let button = springboard.buttons[buttonTitle]
dirk@5497
   198
        if button.exists {
dirk@5497
   199
            button.tap()
dirk@5497
   200
        } else {
dirk@5497
   201
            let exists = NSPredicate(format: "enabled == true")
dirk@5497
   202
            expectation(for: exists, evaluatedWith: button, handler: nil)
dirk@5497
   203
            waitForExpectations(timeout: 2, handler: nil)
dirk@5497
   204
            button.tap()
dirk@5497
   205
        }
dirk@5497
   206
    }
dirk@5573
   207
dirk@5573
   208
    func addAdditionalManual(account: UIAccount) {
dirk@5591
   209
        addAccount()
dirk@5573
   210
dirk@5573
   211
        signIn(account: account, enterPassword: true)
dirk@5573
   212
        switchToManualConfig()
dirk@5573
   213
        manualNewAccountSetup(account)
dirk@5573
   214
dirk@5573
   215
        waitForever()
dirk@5573
   216
    }
dirk@5573
   217
dirk@5573
   218
    func signIn(account: UIAccount, enterPassword: Bool = true) {
dirk@5573
   219
        let theApp = app()
dirk@5573
   220
        let textFieldsQuery = theApp.textFields
dirk@5573
   221
dirk@5573
   222
        var tf = textFieldsQuery["username"]
dirk@5573
   223
        tf.tap()
dirk@5573
   224
        tf.typeText(account.nameOfTheUser)
dirk@5573
   225
dirk@5573
   226
        tf = textFieldsQuery["email"]
dirk@5573
   227
        tf.tap()
dirk@5573
   228
        tf.typeText(account.email)
dirk@5573
   229
dirk@5573
   230
        if enterPassword {
dirk@5573
   231
            tf = theApp.secureTextFields["password"]
dirk@5573
   232
            tf.tap()
dirk@5573
   233
            tf.typeText(account.password)
dirk@5573
   234
        }
dirk@5573
   235
dirk@5573
   236
        theApp.buttons["Sign In"].tap()
dirk@5573
   237
    }
dirk@5573
   238
dirk@5573
   239
    func switchToManualConfig() {
dirk@5573
   240
        let theApp = app()
dirk@5573
   241
dirk@7161
   242
        let alertOkButton = theApp.buttons["OK"]
dirk@5573
   243
        let exists = NSPredicate(format: "enabled == true")
dirk@5573
   244
        expectation(for: exists, evaluatedWith: alertOkButton, handler: nil)
dirk@5573
   245
        waitForExpectations(timeout: 5, handler: nil)
dirk@5573
   246
dirk@5573
   247
        alertOkButton.tap()
dirk@5573
   248
        theApp.buttons["Manual configuration"].tap()
dirk@5573
   249
    }
dirk@5591
   250
dirk@5591
   251
    func addAccount() {
dirk@5591
   252
        let theApp = app()
dirk@5591
   253
        theApp.navigationBars["All"].buttons["Folders"].tap()
dirk@5591
   254
        theApp.tables.buttons["Add Account"].tap()
dirk@5591
   255
    }
hernani@783
   256
}