Automation Using Selenium Webdriver

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

General Function To Comparing Two Strings In Selenium WebDriver TestCase

General Function To Comparing Two Strings In Selenium WebDriver TestCase

When you will use selenium webdriver to automate real worldapplications, Many times you have to compare two strings like page title, product name or any other string. And based on comparison result you have to take specific action. Simple example of such scenario Is, You are navigating on any view product page to add product In to basket. But before adding product In basket, You wants to verify product's name to check It Is correct product or not


Many time you have to compare strings In automating any single application. So What you will do? You will write samecode multiple time In your different scripts? General 
way(Which I am using) Is to create common method In baseclass (Which Is accessible from all test classes) to compare two strings. Then we can call that method by passing actualand expected string to compare strings whenever required In out tests.

Let me explain you how to do It.

Step 1 : Create CommonFunctions Class In your package which can be accessible by any other class.
Step 2 : Create method compareStrings which can accept twostring values. Also Create object of TestNG SoftAssert class to assert assertion softly as shown In bellow given 
example.
package Testng_Pack;
import org.testng.Assert;
import org.testng.asserts.SoftAssert;
public class CommonFunctions {

 SoftAssert Soft_Assert = new SoftAssert();

 public boolean compareStrings(String actualStr, String expectedStr){
  try{
   //If this assertion will fail, It will throw exception and catch block will be executed.
   Assert.assertEquals(actualStr, expectedStr);
   }catch(Throwable t){
    //This will throw soft assertion to keep continue your execution even assertion failure.
    //Use here hard assertion "Assert.fail("Failure Message String")"
 If you wants to stop your test on assertion failure.
    Soft_Assert.fail("Actual String '"+actualStr+"' And Expected String '"+expectedStr +"' Do Not Match.");
    //If Strings will not match, return false.
    return false;
   }
  //If Strings match, return true.
  return true;
 } 
}