Anji Anji - 4 months ago 18
Java Question

Webdriver backed selenium java - how can I verify all combination of characters within text on the page?

Combination of Upper, Lower, Number & Special Characters,

How to validate above characters in selenium webdriver

Answer

Please find the following snippets which return boolean value if the string has Upper, Lower, Number & Special Characters:

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;
     }


}

Use this for your validation. Hope I understood what you asked. Any doubts please comment.


      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() );
    }

Use this if Block inside your @test and place the methods outside and call them. Also replace the println statements with Assert statements as per your requirement.