Webdriver支持selenium java - 如何验证页面上文本中的所有字符组合?

时间:2016-07-26 10:52:06

标签: java eclipse selenium-webdriver

上,下,数和组合特殊字符,

如何在selenium webdriver中验证上述字符

2 个答案:

答案 0 :(得分:2)

如果字符串有Upper,Lower,Number&,请找到以下返回布尔值的片段。特殊字符:

String Password = "Abcd123$";
public static boolean UpperCasePresence(String Password)
{

    int UC = 0;
    for(int i=0;i<Password.length();i++)
    {
        if(Character.isUpperCase(Password.charAt(i)))
        {
            UC++;
        }
    }
    if(UC>=1)
    {
        System.out.println("Upper Case Count :" + UC );
        return true;
    }
    else
    {
        System.out.println("Upper Case Count is " + UC );
        return false;
    }
}


public static boolean LowerCasePresence(String Password)
{
    int LC = 0;
    for(int i=0;i<Password.length();i++)
    {
        if(Character.isLowerCase(Password.charAt(i)))
        {
            LC++;
        }
    }
    if(LC>=1)
    {
        System.out.println("Lower Case Count :" + LC );
        return true;
    }
    else
    {
        System.out.println("Lower Case Count is" + LC );
        return false;
    }
}

public static boolean SpecialCharPresence(String Password)
{

     Pattern p = Pattern.compile("[^A-Za-z0-9]");
     Matcher m = p.matcher(Password);
     boolean b = m.find();
     if (b == true)
     {
        System.out.println("Special character are there in Password");
        return true;
     }
     else
     {
         System.out.println("There is no special char in Password");
         return false;
     }


}


public static boolean NumberPresence(String Password)
{

     Pattern p = Pattern.compile("([0-9])");
     Matcher m = p.matcher(Password);
     boolean b = m.find();
     if (b == true)
     {
        System.out.println("Numbers are there in Password ");
        return true;
     }
     else
     {
         System.out.println("There is no Numbers in Password");
         return false;
     }


}

使用它进行验证。希望我理解你的要求。有任何疑问请评论。

      if(PWD.length()>=6)
      {
        System.out.println("Password is greater than 6 char" );
        if(UpperCasePresence(PWD))
        {
            System.out.println("Password has Upper case letter" );

            if(LowerCasePresence(PWD))
            {
                System.out.println("Password has Lower case letter" );
                if(SpecialCharPresence(PWD))
                {
                    System.out.println("Password has Special Character" );
                    if(NumberPresence(PWD))
                    {
                        System.out.println("Password has Number" );
                        System.out.println("Password Matches all Conditions");
                    }
                    else
                    {
                        System.out.println("Password doesnot have Number" );
                    }


                }
                else
                {
                    System.out.println("Password doesnot have Special Characters");
                }
            }
            else
            {
                System.out.println("Password doesnot have Lower Case Letters");
            }
        }
        else
        {
            System.out.println("Password doesnot have Upper Case Letters");
        }
    }
    else
    {
        System.out.println("Password Length is" + PWD.length() );
    }

如果在@test中阻止,请使用此方法并将方法放在外面并调用它们。也可以根据您的要求用Assert语句替换println语句。

答案 1 :(得分:0)

这只是使用Regex的一个班轮解决方案,如下所示: -

String password = "Abcd123$";
String regex = "((?=.*\\d)(?=.*[a-zA-Z])(?=.*[~'!@#$%?\\\\/&*\\]|\\[=()}\"{+_:;,.><'-])).{8,}";
System.out.println(password.matches(regex));

注意: - selenium没有任何角色。这只是java的东西。

希望它有帮助...:)