import XCTest /// Comprehensive residence testing suite covering all scenarios, edge cases, and variations /// This test suite is designed to be bulletproof and catch regressions early final class ComprehensiveResidenceTests: XCTestCase { var app: XCUIApplication! // Test data tracking var createdResidenceNames: [String] = [] override func setUpWithError() throws { continueAfterFailure = false app = XCUIApplication() app.launch() // Ensure user is logged in UITestHelpers.ensureLoggedIn(app: app) // Navigate to Residences tab navigateToResidencesTab() } override func tearDownWithError() throws { createdResidenceNames.removeAll() app = nil } // MARK: - Helper Methods private func navigateToResidencesTab() { let residencesTab = app.tabBars.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Residences'")).firstMatch if residencesTab.waitForExistence(timeout: 5) { if !residencesTab.isSelected { residencesTab.tap() sleep(3) } } } private func openResidenceForm() -> Bool { let addButton = findAddResidenceButton() guard addButton.exists && addButton.isEnabled else { return false } addButton.tap() sleep(3) // Verify form opened let nameField = app.textFields.containing(NSPredicate(format: "placeholderValue CONTAINS[c] 'Name'")).firstMatch return nameField.waitForExistence(timeout: 5) } private func findAddResidenceButton() -> XCUIElement { sleep(2) let addButtonById = app.buttons[AccessibilityIdentifiers.Residence.addButton] if addButtonById.exists && addButtonById.isEnabled { return addButtonById } let navBarButtons = app.navigationBars.buttons for i in 0.. Bool { guard openResidenceForm() else { return false } // Fill name let nameField = app.textFields.containing(NSPredicate(format: "placeholderValue CONTAINS[c] 'Name'")).firstMatch nameField.tap() nameField.typeText(name) // Select property type selectPropertyType(type: propertyType) // Scroll to address section if scrollBeforeAddress { app.swipeUp() sleep(1) } // Fill address fields fillTextField(placeholder: "Street", text: street) fillTextField(placeholder: "City", text: city) fillTextField(placeholder: "State", text: state) fillTextField(placeholder: "Postal", text: postal) // Save let saveButton = app.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Save'")).firstMatch guard saveButton.exists else { return false } saveButton.tap() sleep(4) // Wait for API call // Track created residence createdResidenceNames.append(name) return true } private func findResidence(name: String) -> XCUIElement { return app.staticTexts.containing(NSPredicate(format: "label CONTAINS '\(name)'")).firstMatch } // MARK: - Basic Residence Creation Tests func testCreateResidenceWithMinimalData() { let timestamp = Int(Date().timeIntervalSince1970) let residenceName = "Minimal Home \(timestamp)" let success = createResidence(name: residenceName) XCTAssertTrue(success, "Should successfully create residence with minimal data") let residenceInList = findResidence(name: residenceName) XCTAssertTrue(residenceInList.waitForExistence(timeout: 10), "Residence should appear in list") } func testCreateResidenceWithAllPropertyTypes() { let timestamp = Int(Date().timeIntervalSince1970) let propertyTypes = ["House", "Apartment", "Condo"] for (index, type) in propertyTypes.enumerated() { let residenceName = "\(type) Test \(timestamp)_\(index)" let success = createResidence(name: residenceName, propertyType: type) XCTAssertTrue(success, "Should create \(type) residence") navigateToResidencesTab() sleep(2) } // Verify all residences exist for (index, type) in propertyTypes.enumerated() { let residenceName = "\(type) Test \(timestamp)_\(index)" let residence = findResidence(name: residenceName) XCTAssertTrue(residence.exists, "\(type) residence should exist in list") } } func testCreateMultipleResidencesInSequence() { let timestamp = Int(Date().timeIntervalSince1970) for i in 1...3 { let residenceName = "Sequential Home \(i) - \(timestamp)" let success = createResidence(name: residenceName) XCTAssertTrue(success, "Should create residence \(i)") navigateToResidencesTab() sleep(2) } // Verify all residences exist for i in 1...3 { let residenceName = "Sequential Home \(i) - \(timestamp)" let residence = findResidence(name: residenceName) XCTAssertTrue(residence.exists, "Residence \(i) should exist in list") } } // MARK: - Residence Editing Tests func testEditResidenceName() { let timestamp = Int(Date().timeIntervalSince1970) let originalName = "Original Name \(timestamp)" let newName = "Edited Name \(timestamp)" // Create residence guard createResidence(name: originalName) else { XCTFail("Failed to create residence") return } navigateToResidencesTab() sleep(2) // Find and tap residence let residence = findResidence(name: originalName) XCTAssertTrue(residence.waitForExistence(timeout: 5), "Residence should exist") residence.tap() sleep(2) // Tap edit button let editButton = app.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Edit'")).firstMatch if editButton.exists { editButton.tap() sleep(2) // Edit name let nameField = app.textFields.containing(NSPredicate(format: "placeholderValue CONTAINS[c] 'Name'")).firstMatch if nameField.exists { nameField.tap() // Clear existing text nameField.doubleTap() sleep(1) app.buttons["Select All"].tap() sleep(1) nameField.typeText(newName) // Save let saveButton = app.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Save'")).firstMatch if saveButton.exists { saveButton.tap() sleep(3) // Track new name createdResidenceNames.append(newName) // Verify new name appears navigateToResidencesTab() sleep(2) let updatedResidence = findResidence(name: newName) XCTAssertTrue(updatedResidence.exists, "Residence should show updated name") } } } } func testUpdateAllResidenceFields() { let timestamp = Int(Date().timeIntervalSince1970) let originalName = "Update All Fields \(timestamp)" let newName = "All Fields Updated \(timestamp)" let newStreet = "999 Updated Avenue" let newCity = "NewCity" let newState = "NC" let newPostal = "99999" // Create residence with initial values guard createResidence(name: originalName, street: "123 Old St", city: "OldCity", state: "OC", postal: "11111") else { XCTFail("Failed to create residence") return } navigateToResidencesTab() sleep(2) // Find and tap residence let residence = findResidence(name: originalName) XCTAssertTrue(residence.waitForExistence(timeout: 5), "Residence should exist") residence.tap() sleep(2) // Tap edit button let editButton = app.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Edit'")).firstMatch XCTAssertTrue(editButton.exists, "Edit button should exist") editButton.tap() sleep(2) // Update name let nameField = app.textFields.containing(NSPredicate(format: "placeholderValue CONTAINS[c] 'Name'")).firstMatch XCTAssertTrue(nameField.exists, "Name field should exist") nameField.tap() nameField.doubleTap() sleep(1) if app.buttons["Select All"].exists { app.buttons["Select All"].tap() sleep(1) } nameField.typeText(newName) // Update property type (if available) let propertyTypePicker = app.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Property Type'")).firstMatch if propertyTypePicker.exists { propertyTypePicker.tap() sleep(1) // Select Condo let condoOption = app.buttons["Condo"] if condoOption.exists { condoOption.tap() sleep(1) } else { // Try cells navigation let cells = app.cells for i in 0..