我正在使用UITesting,因为我必须使用保存的NSUserDefaults值来测试电子邮件字段和密码。请给我一些代码。
注意:1。检查电子邮件和密码字段是有效还是无效或为空。
答案 0 :(得分:-1)
使用以下代码从文本字段中获取文本并验证它们。
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()
}
如果存在任何警报,请稍后检查条件。如果存在您的信用错误。否则等待下一个XCUIElement。使用此代码进行等待
let existsPredicate = NSPredicate(format: "exists == true")
expectation(for: existsPredicate,
evaluatedWith: element, handler: nil)
waitForExpectations(timeout: 20, handler: nil)
答案 1 :(得分:-1)
@interface NSString (emailValidation)
- (BOOL)isValidEmail;
@end
实施
@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
密码修正
-(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];
}
}
答案 2 :(得分:-1)
在NSUserDefaults中存储电子邮件和密码:
[[NSUserDefaults standardUserDefaults] setObject:@"sam.martin105@gmail.com" forKey:@"Email"];
[[NSUserDefaults standardUserDefaults] setObject:@"123456Sam@" forKey:@"Password"];
验证电子邮件:
-(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];
}
验证强密码:
- (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;
}
检查验证电子邮件或密码如果成功,则登录,否则您可以显示警告:
-(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");
}
}