Automation Using Selenium Webdriver
Showing posts with label Fieldlevel validation in Selenium Webdriver (Java). Show all posts
Showing posts with label Fieldlevel validation in Selenium Webdriver (Java). Show all posts

Sunday, 16 October 2016

Fieldlevel validation in Selenium Webdriver (Java)

 Check Filedlevel Validation in Selenium Webdriver(Java)

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


Second example


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