Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Hello Paul, thanks for your valuable time...!!!. And special thanks for your suggestions. I executed your code successfully.. –  QA4it Mar 4 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. –  PaulDonny Mar 4 at 13:35
    
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 at 7:11
1  
Whoops, forgot to clear updating adding in the line to clear It is d.findElement(By.id("FIRSTNAME")).clear(); –  PaulDonny Mar 5 at 12:45
    
Hello Paul, it is worked perfectly now. THANKS :) –  QA4it Mar 5 at 13:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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