Fieldlevel validation in Selenium Webdriver (Java)



  • how to test validations for "FIRSTNAME" textfield. Below are the details and code.

    URL: http://www.vrlbus.in/vrl2013/register_customer.aspx

    Special chars: #!#!#$@#!$@!$@#$%#%^#$^^&%&$%* (Not valid).

    Alphabetic chars: acbcdefghijklmnopqrstuvwxyzabcdef (Valid).

    Code:

    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Validations {
    public static WebDriver d;
        public static void main(String []args)throws Exception{
            d = new FirefoxDriver();
            d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            d.get("http://www.vrlbus.in/vrl2013/register_customer.aspx");
            d.findElement(By.id("FIRSTNAME")).sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");
            d.findElement(By.id("Button1")).click();
            String alertMessage = d.switchTo().alert().getText();
            System.out.println(alertMessage);
          if (alertMessage.equals("First name Should not contain Special Characters")){
                System.out.println("Error displayed: First name Should not contain Special Characters");
                d.switchTo().alert().dismiss();
            } else{
                System.out.println("Accepted");
            }
            d.findElement(By.id("FIRSTNAME")).sendKeys("acbcdefghijklmnopqrstuvwxyzabcdef");
            d.findElement(By.id("Button1")).click();
             if (alertMessage.equals("First name Should not contain Special Characters")){
                    System.out.println("Error displayed: First name Should not contain Special Characters");
                    d.switchTo().alert().dismiss();
                } else{
                    System.out.println("Accepted");
                }
            d.quit();
        }   
    }
    


  • For testing it, your doing an extremely basic test. A major advantage of automation is that you can do a lot more testing. I would set up a datasource for names, declare them valid or invalid, and have it attempt to use it. I would also have it only use one of the special characters at a time and the rest be valid (As of now your essentially testing if any of those special characters will cause a fail, not all).

    It sounds like you are doing boundary testing, so try and push the boundaries to their limits. Make sure that you are trying to 'trick' the system. Don't just throw all of the characters at once.

    If you want the easiest way to perform this....

    public static void main(String[] args) throws Exception {
        String[] invalidChars = {"#", "!", "$", "@", "%", "^", "&"};    String name = "acbcdefghijklmnopqrstuvwxyzab";
        d = new FirefoxDriver();
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        d.get("http://www.vrlbus.in/vrl2013/register_customer.aspx");
        for (String invalid : invalidChars) {
            d.findElement(By.id("FIRSTNAME")).clear();
            d.findElement(By.id("FIRSTNAME")).sendKeys(name + invalid);
            d.findElement(By.id("Button1")).click();
            String alertMessage = d.switchTo().alert().getText();
            System.out.println(invalid);
            if (alertMessage.equals("First name Should not contain Special Characters")) {
                System.out.println("Error displayed: First name Should not contain Special Characters");
                d.switchTo().alert().dismiss();
            } else {
                System.out.println("Accepted");
            }
        }
        d.findElement(By.id("FIRSTNAME")).sendKeys("acbcdefghijklmnopqrstuvwxyzabcdef");
        d.findElement(By.id("Button1")).click();
        String alertMessage = d.switchTo().alert().getText();
        if (alertMessage.equals("First name Should not contain Special Characters")) {
            System.out.println("Error displayed: First name Should not contain Special Characters");
            d.switchTo().alert().dismiss();
        } else {
            System.out.println("Accepted");
        }
        d.quit();
    }
    

    This will loop through the array of invalidChars and add them onto the end of the valid characters. I had to drop the valid characters down to 29 since it seems your form maxes out at 30.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2