summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNUITests/Pages/LoginPage.swift
blob: 42e78c71c343f4fbe75494f340240b893fcbd9bd (plain)
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
//
//  LoginPage.swift
//  MullvadVPNUITests
//
//  Created by Niklas Berglund on 2024-01-10.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import XCTest

class LoginPage: Page {
    @discardableResult override init(_ app: XCUIApplication) {
        super.init(app)

        self.pageElement = app.otherElements[.loginView]
        waitForPageToBeShown()
    }

    @discardableResult public func tapAccountNumberTextField() -> Self {
        app.textFields[AccessibilityIdentifier.loginTextField].tap()
        return self
    }

    @discardableResult public func waitForAccountNumberSubmitButton() -> Self {
        let submitButtonExist = app.buttons[AccessibilityIdentifier.loginTextFieldButton]
            .waitForExistence(timeout: BaseUITestCase.defaultTimeout)
        XCTAssertTrue(submitButtonExist, "Account number submit button shown")
        return self
    }

    @discardableResult public func tapAccountNumberSubmitButton() -> Self {
        app.buttons[AccessibilityIdentifier.loginTextFieldButton].tap()
        return self
    }

    @discardableResult public func tapCreateAccountButton() -> Self {
        app.buttons[AccessibilityIdentifier.createAccountButton].tap()
        return self
    }

    @discardableResult public func verifySuccessIconShown() -> Self {
        let isShown = getSuccessIconShown()

        XCTAssertTrue(isShown, "Success icon shown")

        return self
    }

    @discardableResult public func confirmAccountCreation() -> Self {
        app.buttons[AccessibilityIdentifier.createAccountConfirmationButton].tap()
        return self
    }

    @discardableResult public func verifyFailIconShown() -> Self {
        let predicate = NSPredicate(format: "identifier == 'statusImageView' AND value == 'fail'")
        let elementQuery = app.images.containing(predicate)
        let elementExists = elementQuery.firstMatch.waitForExistence(timeout: BaseUITestCase.veryLongTimeout)
        XCTAssertTrue(elementExists, "Fail icon shown")
        return self
    }

    /// Checks whether success icon is being shown
    func getSuccessIconShown() -> Bool {
        // Success icon is only shown very briefly, since another view is presented after success icon is shown.
        // Therefore we need to poll faster than waitForElement function.
        let successIconDisplayedExpectation = XCTestExpectation(description: "Success icon shown")
        nonisolated(unsafe) var isShown = false
        let timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { [self] _ in
            DispatchQueue.main.async {
                let statusImageView = self.app.images[.statusImageView]

                if statusImageView.exists {
                    if statusImageView.value as? String == "success" {
                        isShown = true
                        successIconDisplayedExpectation.fulfill()
                    }
                }
            }
        }

        _ = XCTWaiter.wait(for: [successIconDisplayedExpectation], timeout: BaseUITestCase.longTimeout)
        timer.invalidate()

        return isShown
    }
}