UITesting for login in xcode8

237 views Asked by At

Am working with UITesting,in that i have to test email field and password using the saved the NSUserDefaults value.please give me the some piece of code.

NOTE:1.Check the email and password fields are valid or invalid or empty.

3

There are 3 answers

4
amit_donga On

Store Email and Password in NSUserDefaults :

[[NSUserDefaults standardUserDefaults] setObject:@"[email protected]" forKey:@"Email"];
[[NSUserDefaults standardUserDefaults] setObject:@"123456Sam@" forKey:@"Password"];

Validation For Email:

-(BOOL)NSStringIsValidEmail:(NSString *)checkString
{
    BOOL stricterFilter = NO;
    NSString *stricterFilterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
    NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}

Validation For Strong Password :

- (BOOL)strongPassword:(NSString *)yourText
{
    BOOL strongPwd = YES;

    //Checking length
    if([yourText length] < 8)
        strongPwd = NO;

    //Checking uppercase characters
    NSCharacterSet *charSet = [NSCharacterSet uppercaseLetterCharacterSet];
    NSRange range = [yourText rangeOfCharacterFromSet:charSet];
    if(range.location == NSNotFound)
        strongPwd = NO;

    //Checking lowercase characters
    charSet = [NSCharacterSet lowercaseLetterCharacterSet];
    range = [yourText rangeOfCharacterFromSet:charSet];
    if(range.location == NSNotFound)
        strongPwd = NO;

    //Checking special characters
    charSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    range = [yourText rangeOfCharacterFromSet:charSet];
    if(range.location == NSNotFound)
        strongPwd = NO;

    return strongPwd;
}

check validation Email or password if success then login else you can show alert :

-(IBAction)Login:(id)sender
{
    NSString *Email = [[NSUserDefaults standardUserDefaults]objectForKey:@"Email"];
    NSString *Password = [[NSUserDefaults standardUserDefaults]objectForKey:@"Password"];

    BOOL flagEmail,flagPassword = false;

    if(![Email isEqualToString:@""])
    {
        flagEmail = [self NSStringIsValidEmail:Email];
        if(flagEmail)
        {
            NSLog(@"Email is accepted");
        }
        else
        {
            NSLog(@"Email is not valid");
        }
    }
    else
    {
        NSLog(@"Email is empty");
    }

    if(![Password isEqualToString:@""])
    {
        flagPassword = [self strongPassword:Password];
        if(flagPassword)
        {
            NSLog(@"Password is accepted");
        }
        else
        {
            NSLog(@"Password is not valid");
        }
    }
    else
    {
        NSLog(@"Password is empty");
    }

    if(flagEmail && flagPassword)
    {
         NSLog(@"Email and password both are valid and not empty");
    }
}
1
G. Hazarath Reddy On

Use the below code to get text from text field and validate them.

 let app = XCUIApplication()
        let nameTextField = app.textFields.element(boundBy: 0)

        // based on index you can get text field.

        nameTextField.typeText("userName")

        let passwordTextField = app.textFields.element(boundBy: 1)

        // it returns the password field.

        passwordTextField.typeText("password")

        let userName : String = nameTextField.value

        // returns the text in text field.

        let password : String = passwordTextField.value

        if userName.isEmpty || password.isEmpty {

            print("Text fields are empty");

        }
        else {

            let validateButton = app.buttons.element(boundsBy: 0)

            validateButton.tap()

        }

Later check condition if any alert exist. If exist your creditials wrong. Otherwise wait for next XCUIElement. use this code for wait

 let existsPredicate = NSPredicate(format: "exists == true")
    expectation(for: existsPredicate,
                evaluatedWith: element, handler: nil)
    waitForExpectations(timeout: 20, handler: nil)
1
Developer1205 On
@interface NSString (emailValidation) 
  - (BOOL)isValidEmail;
@end

implementation

@implementation NSString (emailValidation)
-(BOOL)isValidEmail
{
  BOOL stricterFilter = NO; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
  NSString *stricterFilterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
  NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
  NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
  NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  return [emailTest evaluateWithObject:self];
}
@end

password valiation

-(void)textFieldDidEndEditing:(UITextField *)textField{
   int numberofCharacters = 0;
   BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
   if([textField.text length] >= 10)
   {
      for (int i = 0; i < [textfield.text length]; i++) 
      {
         unichar c = [textfield.text characterAtIndex:i];
         if(!lowerCaseLetter)
         {
           lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
         }
         if(!upperCaseLetter)
         {
           upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
         }
         if(!digit)
         {
            digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
         }
         if(!specialCharacter)
         {
            specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
         }
      }

      if(specialCharacter && digit && lowerCaseLetter && upperCaseLetter)
      {
         //do what u want
      }
      else
      { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
      }

   }
   else
   {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Please Enter at least 10 password"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
   }
}