|
| 1 | +import XCTest |
| 2 | + |
| 3 | +final class CardFields_UITests: XCTestCase { |
| 4 | + |
| 5 | + var app = XCUIApplication() |
| 6 | + |
| 7 | + override func setUp() { |
| 8 | + super.setUp() |
| 9 | + continueAfterFailure = false |
| 10 | + app.launchArguments.append("-EnvironmentSandbox") |
| 11 | + app.launchArguments.append("-ClientToken") |
| 12 | + app.launchArguments.append("-Integration:UIComponentsViewController") |
| 13 | + app.launchEnvironment["UITEST_DISABLE_ANIMATIONS"] = "YES" |
| 14 | + app.launch() |
| 15 | + } |
| 16 | + |
| 17 | + // MARK: - Pay Button State |
| 18 | + |
| 19 | + func testCardFields_payButtonInitiallyDisabled_enabledAfterValidInput() { |
| 20 | + let payButton = app.buttons["Pay"] |
| 21 | + XCTAssertTrue(payButton.waitForExistence(timeout: 5), "Pay button should be visible") |
| 22 | + XCTAssertFalse(payButton.isEnabled, "Pay button should be disabled when card fields are empty") |
| 23 | + |
| 24 | + let cardNumberField = app.textFields["Card Number"] |
| 25 | + waitForElementToBeHittable(cardNumberField) |
| 26 | + cardNumberField.tap() |
| 27 | + cardNumberField.typeText("4111111111111111") |
| 28 | + |
| 29 | + let expirationField = app.textFields["Expiration Date"] |
| 30 | + expirationField.tap() |
| 31 | + expirationField.typeText("1245") |
| 32 | + |
| 33 | + let cvvField = app.textFields["CVV"] |
| 34 | + cvvField.tap() |
| 35 | + cvvField.typeText("123") |
| 36 | + |
| 37 | + XCTAssertTrue(payButton.isEnabled, "Pay button should be enabled after valid card details are entered") |
| 38 | + } |
| 39 | + |
| 40 | + // MARK: - Validation Errors |
| 41 | + |
| 42 | + func testCardFields_showsError_whenInvalidDataIsEntered() { |
| 43 | + let cardNumberField = app.textFields["Card Number"] |
| 44 | + let expirationField = app.textFields["Expiration Date"] |
| 45 | + let cvvField = app.textFields["CVV"] |
| 46 | + |
| 47 | + // Enter incomplete card number (10 of 16 digits), then advance focus |
| 48 | + waitForElementToBeHittable(cardNumberField) |
| 49 | + cardNumberField.tap() |
| 50 | + cardNumberField.typeText("4111111111") |
| 51 | + |
| 52 | + expirationField.tap() |
| 53 | + XCTAssertTrue( |
| 54 | + app.staticTexts["Card number is invalid"].waitForExistence(timeout: 2), |
| 55 | + "Error should appear below card number field after leaving with incomplete number" |
| 56 | + ) |
| 57 | + |
| 58 | + // Advance to CVV without entering an expiration date |
| 59 | + cvvField.tap() |
| 60 | + XCTAssertTrue( |
| 61 | + app.staticTexts["Expiration date is required"].waitForExistence(timeout: 2), |
| 62 | + "Error should appear below expiration field after leaving it empty" |
| 63 | + ) |
| 64 | + |
| 65 | + // Enter only 2 CVV digits, then advance focus |
| 66 | + cvvField.typeText("12") |
| 67 | + expirationField.tap() |
| 68 | + XCTAssertTrue( |
| 69 | + app.staticTexts["CVV is invalid"].waitForExistence(timeout: 2), |
| 70 | + "Error should appear below CVV field after leaving with incomplete CVV" |
| 71 | + ) |
| 72 | + |
| 73 | + // Correct all fields and verify Pay button becomes enabled |
| 74 | + cardNumberField.tap(withNumberOfTaps: 3, numberOfTouches: 1) |
| 75 | + cardNumberField.typeText("4111111111111111") |
| 76 | + |
| 77 | + expirationField.typeText("1245") |
| 78 | + |
| 79 | + cvvField.tap(withNumberOfTaps: 3, numberOfTouches: 1) |
| 80 | + cvvField.typeText("123") |
| 81 | + |
| 82 | + waitForElementToBeHittable(app.buttons["Pay"]) |
| 83 | + |
| 84 | + XCTAssertTrue( |
| 85 | + app.buttons["Pay"].isEnabled, |
| 86 | + "Pay button should be enabled after correcting all fields" |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + // MARK: - Card Brand Detection |
| 91 | + |
| 92 | + func testCardFields_displaysVisaBrand_whenCardStartsWith4() { |
| 93 | + let cardNumberField = app.textFields["Card Number"] |
| 94 | + waitForElementToBeHittable(cardNumberField) |
| 95 | + cardNumberField.tap() |
| 96 | + cardNumberField.typeText("4") |
| 97 | + XCTAssertTrue(app.images["Visa"].waitForExistence(timeout: 2)) |
| 98 | + } |
| 99 | + |
| 100 | + func testCardFields_displaysMastercardBrand_whenCardStartsWith51() { |
| 101 | + let cardNumberField = app.textFields["Card Number"] |
| 102 | + waitForElementToBeHittable(cardNumberField) |
| 103 | + cardNumberField.tap() |
| 104 | + cardNumberField.typeText("51") |
| 105 | + XCTAssertTrue(app.images["Mastercard"].waitForExistence(timeout: 2)) |
| 106 | + } |
| 107 | + |
| 108 | + func testCardFields_displaysAmexBrand_whenCardStartsWith34() { |
| 109 | + let cardNumberField = app.textFields["Card Number"] |
| 110 | + waitForElementToBeHittable(cardNumberField) |
| 111 | + cardNumberField.tap() |
| 112 | + cardNumberField.typeText("34") |
| 113 | + XCTAssertTrue(app.images["American Express"].waitForExistence(timeout: 2)) |
| 114 | + } |
| 115 | + |
| 116 | + func testCardFields_displaysDiscoverBrand_whenCardStartsWith6011() { |
| 117 | + let cardNumberField = app.textFields["Card Number"] |
| 118 | + waitForElementToBeHittable(cardNumberField) |
| 119 | + cardNumberField.tap() |
| 120 | + cardNumberField.typeText("6011") |
| 121 | + XCTAssertTrue(app.images["Discover"].waitForExistence(timeout: 2)) |
| 122 | + } |
| 123 | + |
| 124 | + func testCardFields_displaysUnionPayBrand_whenCardStartsWith620() { |
| 125 | + let cardNumberField = app.textFields["Card Number"] |
| 126 | + waitForElementToBeHittable(cardNumberField) |
| 127 | + cardNumberField.tap() |
| 128 | + cardNumberField.typeText("620") |
| 129 | + XCTAssertTrue(app.images["UnionPay"].waitForExistence(timeout: 2)) |
| 130 | + } |
| 131 | + |
| 132 | + // MARK: - CVV Length Changes With Brand |
| 133 | + |
| 134 | + func testCardFields_showsCVVError_whenSwitchingFromAmexToVisa() { |
| 135 | + let cardNumberField = app.textFields["Card Number"] |
| 136 | + let expirationField = app.textFields["Expiration Date"] |
| 137 | + let cvvField = app.textFields["CVV"] |
| 138 | + |
| 139 | + waitForElementToBeHittable(cardNumberField) |
| 140 | + cardNumberField.tap() |
| 141 | + cardNumberField.typeText("378282246310005") |
| 142 | + |
| 143 | + expirationField.typeText("1245") |
| 144 | + |
| 145 | + cvvField.tap() |
| 146 | + cvvField.typeText("1234") |
| 147 | + |
| 148 | + cardNumberField.tap(withNumberOfTaps: 3, numberOfTouches: 1) |
| 149 | + cardNumberField.typeKey(.delete, modifierFlags: []) |
| 150 | + waitForElementToBeHittable(cardNumberField) |
| 151 | + cardNumberField.typeText("4111111111111111") |
| 152 | + |
| 153 | + cvvField.tap() |
| 154 | + expirationField.tap() |
| 155 | + |
| 156 | + XCTAssertTrue( |
| 157 | + app.staticTexts["CVV is invalid"].waitForExistence(timeout: 2), |
| 158 | + "CVV error should appear when a 4-digit Amex CVV is used after switching to a Visa card" |
| 159 | + ) |
| 160 | + |
| 161 | + cvvField.tap(withNumberOfTaps: 3, numberOfTouches: 1) |
| 162 | + cvvField.typeText("123") |
| 163 | + |
| 164 | + waitForElementToBeHittable(app.buttons["Pay"]) |
| 165 | + |
| 166 | + XCTAssertTrue( |
| 167 | + app.buttons["Pay"].isEnabled, |
| 168 | + "Pay button should be enabled after correcting CVV to 3 digits for Visa" |
| 169 | + ) |
| 170 | + } |
| 171 | +} |
0 commit comments