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.
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.
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)
@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];
}
}
Store Email and Password in NSUserDefaults :
Validation For Email:
Validation For Strong Password :
check validation Email or password if success then login else you can show alert :