7

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();
    }   
}
3
  • What about spaces? Is a space accepted as a valid name? I've tripped up many apps that allow this Mar 4, 2014 at 16:57
  • Yes, space is accepted as a valid name in my example application..
    – QA4it
    Mar 5, 2014 at 5:58
  • 1
    if nothing else, I like your code :)
    – Thufir
    Oct 14, 2014 at 6:12

1 Answer 1

8

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.

6
  • Hello Paul, thanks for your valuable time...!!!. And special thanks for your suggestions. I executed your code successfully..
    – QA4it
    Mar 4, 2014 at 13:29
  • 1
    You are very welcome. I am learning Selenium and I always found reviewing over questions that other people has and solving them is the best way to master a field.
    – Paul Muir
    Mar 4, 2014 at 13:35
  • 1
    Hello Paul, Below are my observations from above code, kindly look into these. 1). 'String[] invalidChars = {"#", "!", "$", "@", "%", "^", "&"};' is taking only first spl char ("#) while executing the application. 2). Not clearing previously entered data in the firstname textfield.
    – QA4it
    Mar 5, 2014 at 7:11
  • 1
    Whoops, forgot to clear updating adding in the line to clear It is d.findElement(By.id("FIRSTNAME")).clear();
    – Paul Muir
    Mar 5, 2014 at 12:45
  • Hello Paul, it is worked perfectly now. THANKS :)
    – QA4it
    Mar 5, 2014 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.