I need help with XCUITests for textfield clear text In Below code when i will try to call clearAndEnterText() method for // Wrong Password email test case it will start removing previous text from .com (Cursor starts it's backwork method beform .com letters) so from it's 23rd character it'll start deleting text. if i'll add "[email protected]" email instead of "[email protected]" this method will work properly i have checked in my textfield that my textfield don't have any min or max length. i can not set textfield.clearsOnBeginEditing = true as i wanted to edit text as well so i got stuck in this problem. Any help would be very greatful thankyou in adnvance . #helpinghand #iosdeveloper #uitestcases #XCUITests

import XCTest
import UIKit

final class LoginUITests: XCTestCase {
   
    var app: XCUIApplication!
    
    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        app = XCUIApplication()
        app.launch()
    }
    
    override func tearDownWithError() throws {
        app = nil
    }
   
    func testLoginScreenWithValidations() {
        app = XCUIApplication()
        
        let emailTextField = app.textFields["Email"]
        let passwordTextField = app.secureTextFields["Password"]
        let doneButtonTap = app.toolbars["Toolbar"].buttons["Done"] // Hide keyboard
        let loginButton = app.buttons["Login"]

        // Email Not Exist
        let emailInCorrect = "[email protected]"
        let passwordCorrect = "janki@123"
        
        emailTextField.clearAndEnterText(text: emailInCorrect)
        doneButtonTap.tap()
        
        passwordTextField.clearAndEnterText(text: passwordCorrect)
        doneButtonTap.tap()
        
        loginButton.tap()
        sleep(3)
        
        // Api - Wrong Password
        let emailCorrect = "[email protected]"
        let passwordInCorrect = "janki@1"
        
        emailTextField.clearAndEnterText(text: emailCorrect)
        doneButtonTap.tap()
        
        passwordTextField.clearAndEnterText(text: passwordInCorrect)
        doneButtonTap.tap()
        
        loginButton.tap()
        sleep(3)
    }
 }

extension XCUIElement {
    func clearAndEnterText(text: String) {
        guard let stringValue = self.value as? String else {
            XCTFail("Tried to clear and enter text into a non string value")
            return
        }
        
        self.tap()
        
        let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: stringValue.count)
        
        self.typeText(deleteString)
        self.typeText(text)
    }
    
}

0

There are 0 best solutions below