Generate Random Data(String,Date,Number,Domain,Email,Mobile Number)using java for Selenium WebDriver_Selenium online Training

Generating random data(String,Date,Number,Domain,Email,Mobile Number)using java which can be used for while designing selenium Scripts.

Please find below class code for the same.

Sample code:

package DriverMethods;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

public class RandomDataMethods {

    public static final String ALPHA_CAPS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String ALPHA   = "abcdefghijklmnopqrstuvwxyz";
    public static final String NUM     = "0123456789";
    public static final String SPL_CHARS   = "@$";

    public static ArrayList<String> domain;
public static Random randomGenerator;

    //Reusable Method for generate Domain names randomly.

   public static String pickDomain()

    {
    domain= new ArrayList<String>();
        randomGenerator= new Random();
       
        domain.add(".com");
        domain.add(".net");
        domain.add(".org");
        domain.add(".in");
        domain.add(".co");
        domain.add(".edu");
        int index = randomGenerator.nextInt(domain.size());
        return domain.get(index);
    }
  
     //Reusable Method for Generating Random Email 

   public static String generateEmail()
    {
     String email=null;
     char[] name = generateRandomData(3, 20, 1, 1, 0);
     char[] domain = generateRandomData(3, 20, 1, 1, 0);
   
     email=new String(name)+"@"+new String(domain)+pickDomain();
   
    return email;
    }
 
      //Reusable Method for Generating Random Mobile Number

 public static String generateMobileNumber()
      {
       String mobileNumber=null;
       char[] number = generateRandomData(9, 9, 0, 9, 0);
     
      mobileNumber= "9"+new String(number);
     
      return mobileNumber;
      }

      //Reusable Method for Generate Random Number between maximum and minimum values

 public static int getRandomNumberBetween(int min, int max) {
       
      Random foo = new Random();
          int randomNumber = foo.nextInt(max - min) + min;
          if(randomNumber == min) {
              // Since the random number is between the min and max values, simply add 1
              return min + 1;
          }
          else {
              return randomNumber;
          }

      }

   //Reusable Method for Generate Random Date from Start date to End Date

 public static String generateRandomDate(String Format,String startDate,String endDate) throws java.text.ParseException
   {
   DateFormat formatter = new SimpleDateFormat(Format);
   Calendar cal=Calendar.getInstance();
   cal.setTime(formatter.parse(startDate));
   Long value1 = cal.getTimeInMillis();

   cal.setTime(formatter.parse(endDate));
   Long value2 = cal.getTimeInMillis();

   long value3 = (long)(value1 + Math.random()*(value2 - value1));
   cal.setTimeInMillis(value3);
   return formatter.format(cal.getTime());
       }

    //Reusable Method for Generating Random String

    public static char[] generateRandomData(int minLen, int maxLen, int noOfCAPSAlpha, int noOfDigits,int noOfSplChars)
    {
    
        if(minLen > maxLen)
            throw new IllegalArgumentException("Min. Length > Max. Length!");
        if( (noOfCAPSAlpha + noOfDigits +noOfSplChars) > minLen )
            throw new IllegalArgumentException
            ("Min. Length should be atleast sum of (CAPS, DIGITS, SPL CHARS) Length!");
        Random rnd = new Random();
        int len = rnd.nextInt(maxLen - minLen + 1) + minLen;
        char[] pswd = new char[len];
        int index = 0;
        for (int i = 0; i < noOfCAPSAlpha; i++)
        {
            index = getNextIndex(rnd, len, pswd);
            pswd[index] = ALPHA_CAPS.charAt(rnd.nextInt(ALPHA_CAPS.length()));
        }
        for (int i = 0; i < noOfDigits; i++)
        {
            index = getNextIndex(rnd, len, pswd);
            pswd[index] = NUM.charAt(rnd.nextInt(NUM.length()));
        }
        for (int i = 0; i < noOfSplChars; i++)
        {
            index = getNextIndex(rnd, len, pswd);
            pswd[index] = SPL_CHARS.charAt(rnd.nextInt(SPL_CHARS.length()));
        }
        for(int i = 0; i < len; i++)
        {
            if(pswd[i] == 0)
            {
                pswd[i] = ALPHA.charAt(rnd.nextInt(ALPHA.length()));
            }
        }
        return pswd;
    }

    public static int getNextIndex(Random rnd, int len, char[] pswd) {
        int index = rnd.nextInt(len);
        while(pswd[index = rnd.nextInt(len)] != 0);
        return index;
    }

                 //Usage

    public static void main(String args[]) throws ParseException  {
    
     System.out.println(RandomDataMethods.pickDomain());
     System.out.println(RandomDataMethods.generateRandomDate("dd MMM yyyy", "10 Aug 2016", "01 Sep 2017"));
     System.out.println(RandomDataMethods.generateEmail());
     System.out.println(RandomDataMethods.getRandomNumberBetween(5, 41));
     System.out.println(RandomDataMethods.generateMobileNumber());
     System.out.println(RandomDataMethods.generateRandomData(10, 11, 1, 1, 1));
    
    }
   

}

How to work with Firefox browser using Selenium 3.0 beta 1_Selenium online Training

Recently selenium has launched Selenium 3.0 beta 1 jar.In this post I will show you How to work with Firefox browser using Selenium 3.0 beta 1 jar.

Please follow the below steps:
Step:1
Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser

In order to work with Firefox browser using Selenium 3 beta 1 jar, you need to use separate a driver which will interact with Firefox browser called as "Geckodriver".

Please find below link for downloading latest version of geckodriver.

https://github.com/mozilla/geckodriver/releases/download/v0.9.0/geckodriver-v0.9.0-win64.zip

Step:2
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");

Step:3
driver = new FirefoxDriver();

Note: Still if you are using old versions of selenium jars(selenium 2) then you can skip first two steps.

Sample Code:
public class SampleTest {

public WebDriver driver;

@Test
public void setup(){

System.setProperty("webdriver.gecko.driver", "E:/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://google.com");

}

}

Generate Random Date in between Start Date and End Date(Java)

Generating random Date using java for Selenium WebDriver by taking inputs as format of date,Start date and End Date.
Please find the below Reusable method for the same

Sample Code for generating Random Date in between Start Date and End Date(Java)::

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GenerateRandomDate {


  
public static String generateRandomDate(String Format,String startDate,String endDate) throws ParseException
 {
 DateFormat formatter = new SimpleDateFormat(Format);
 Calendar cal=Calendar.getInstance();
 cal.setTime(formatter.parse(startDate));
 Long value1 = cal.getTimeInMillis();

 cal.setTime(formatter.parse(endDate));
 Long value2 = cal.getTimeInMillis();

 long value3 = (long)(value1 + Math.random()*(value2 - value1));
 cal.setTimeInMillis(value3);
 return formatter.format(cal.getTime());
     }

  
  
    public static void main(String args[]) throws ParseException{
    
     System.out.println(GenerateRandomDate.generateRandomDate("dd MMM yyyy", "01 Aug 2016", "01 Sep 2017"));
    
    
    }
  
 }

Handle Kendo UI jQuery Date picker using Selenium WebDriver_Selenium online Training

Many applications are using Kendo UI jQuery Date picker for selecting date. So selecting date picker using selenium is a not a difficult task.

In this post, I will explain how we can select date from Kendo UI jQuery DatePicker using Selenium WebDriver.
Please find the below sample code for the same.

Sample code:


                                                                                                 
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DatePickerKendo {

    public WebDriver driver;
    
    @BeforeClass
    public void setup(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
             driver.get("http://demos.telerik.com/kendo-ui/datetimepicker/index");
             driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    @Test
    public void test() throws Exception{
    
     selectDate("23/06/1990");
    }
    
    //Reusable method for Selecting date
    public void selectDate(String expDate) throws Exception{
    
     List<String> months = Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
       //click on calendar button
        driver.findElement(By.cssSelector("span.k-icon.k-i-calendar")).click();
       Thread.sleep(2000);
       //click on center button
        driver.findElement(By.xpath("//div[@class='k-header']//a[contains(@class,'k-nav-fast')]")).click();
        //splitting date
        String Date[]=expDate.split("/");
        String expDay = null;
        if(Date[0].contains("0")){
         expDay=Date[0].replaceAll("0", "");
        }
        else{
         expDay=Date[0];
        }
        String expMonth=Date[1];
        String expYear = Date[2];
        
        System.out.println(Calendar.getInstance().get(Calendar.YEAR));
        
       
        if(Integer.parseInt(expYear)<Calendar.getInstance().get(Calendar.YEAR)){
            int count=Calendar.getInstance().get(Calendar.YEAR)-(Integer.parseInt(expYear));
         for(int i=0;i<count;i++){
             driver.findElement(By.cssSelector("span.k-icon.k-i-arrow-w")).click();
         }
        }
        
         else if(Integer.parseInt(expYear)>Calendar.getInstance().get(Calendar.YEAR)){
             int count1=Integer.parseInt(expYear)-Calendar.getInstance().get(Calendar.YEAR);
         for(int i=0;i<count1;i++){
         driver.findElement(By.cssSelector("span.k-icon.k-i-arrow-e")).click();
         }
        
        }
           Thread.sleep(1000);
           driver.findElement(By.linkText(months.get(Integer.parseInt(expMonth)-1))).click();
           Thread.sleep(1000);  
           driver.findElement(By.linkText(expDay)).click();

        }

}

selenium project support-Defining Agile Methodology

Defining Agile Methodology!

Agile Methodology are models used in the system development arena. The agile methodology has evolved in the mid-1990s as a part of reaction against traditional waterfall methods. That the Agile Methodology were originating resulted from the use of the waterfall model were seen as bureaucratic, inflexible, slow, and inconsistent with the ways that software developers actually perform effective work. Agile development methods mark a return to development practice from early in the history of software development.
Agile methods are a response to the drastic degree of change in the modern business and IT environments. There highly dynamic environments demand software development teams that can respond to change and continuously deliver business value.
The below figure shows the steps in Agile Methodology which focus on iteration and adaptable to change.
Agile Methodology!
Agile Methodology!
Agile Methodology appeal many people because they attempted a useful compromise between no process and too much process to gain a reasonable payoff. They are less document-oriented, usually emphasizing a smaller amount of document for a given task. Agile Methodologies are people-oriented more than process-oriented. Agile Methods assert that no process will ever make up the skill of the development team, so the role of a process is to support the development team in their work. Moreover, thanks to its flexibility and team-oriented, agile methodologies is well suited to the current business environment which continues to change dramatically.

selenium project support-Why is Agile Methodology Important?

Why is Agile Methodology Important?

Agile methodology is an alternative to traditional project management, typically used in software development. It helps teams respond to unpredictability through incremental, iterative work cadences, known as sprints. Agile methodologies are an alternative to waterfall, or traditional sequential development.
why agile methodology is important is as follows.
  1. Revenue
The iterative nature of agile development means features are delivered incrementally, enabling some benefits to be realized early as the product continues to develop.
  1. Speed-to-market
Research suggests about 80% of all market leaders were first to market. As well as the higher revenue from incremental delivery, agile development philosophy also supports the notion of early and regular releases, and ‘perpetual beta’.
  1. Quality
The product owner to make adjustments if necessary and gives the product team early sight of any quality issues.
  1. Visibility
Agile development principles encourage active ‘user’ involvement throughout the product’s development and a very cooperative collaborative approach.
  1. Risk Management
Small incremental releases made visible to the product owner and product team through its development help to identify any issues early and make it easier to respond to change. The clear visibility in agile development helps to ensure that any necessary decisions can be taken at the earliest possible opportunity, while there’s still time to make a material difference to the outcome.
  1. Flexibility / Agility
Agile development principles are different. In agile development, change is accepted. In fact, it’s expected. Because the one thing that’s certain in life is change.
  1. Business Engagement/Customer Satisfaction
The active involvement of a user representative and/or product owner, the high visibility of the product and progress, and the flexibility to change when change is needed, creates much better business engagement and customer satisfaction.

How to Load data dynamically on page scroll using selenium webdriver?

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class jQueryAutoPageLoad {

 public static void main(String[] args) throws InterruptedException {
  int j,i=0;
  WebDriver myTestDriver = new FirefoxDriver();

  EventFiringWebDriver myTestDriverMouse = new EventFiringWebDriver(myTestDriver);
  myTestDriver.manage().window().maximize();

  myTestDriver.get("http://www.webresourcesdepot.com/dnspinger/");

   try {
    while(true){
     Thread.sleep(5000L);

     myTestDriverMouse.executeScript("scroll(0,20000)");
    }
  } catch (Exception e) {
   System.out.println("End of the pagination ");
  }

 }

}

How to find total rows in a paginated web table ?

  1. import java.util.Iterator;

  2. import java.util.List;

  3. import org.junit.After;

  4. import org.junit.Before;

  5. import org.junit.Test;

  6. import org.openqa.selenium.By;

  7. import org.openqa.selenium.WebDriver;

  8. import org.openqa.selenium.WebElement;

  9. import org.openqa.selenium.firefox.FirefoxDriver;

  10.  

  11. public class ReadingTableExample {

  12.         private WebDriver driver;

  13.         private String baseUrl="http://www.espncricinfo.com/";

  14.  

  15.         @Before

  16.         public void setUp() throws Exception {

  17.                 driver = new FirefoxDriver();

  18.                 driver.get(baseUrl);

  19.         }

  20.  

  21.         //Test to display how to read html table using webdriver on cricinfo.com.  

  22.         @Test

  23.         public void printFacebookFriendList() throws Exception {

  24.  

  25.                 //Get all the links for Scorecard.

  26.                 WebElement box = driver.findElement(By.cssSelector("div.ciHomeTopHeadlines"));

  27.                 List <WebElement> scorecard = box.findElements(By.linkText("Scorecard"));

  28.  

  29.                 //Click on the first scorecard link from News Section

  30.                 (scorecard.get(0)).click();

  31.  

  32.                 //Get all the data of the table

  33.                 WebElement table =

  34.                         driver.findElement(By.id("inningsBat1"));

  35.                 List<WebElement> rows = table.findElements(By.tagName("tr"));

  36.                 Iterator<WebElement> i = rows.iterator();

  37.  

  38.                 //Print the table.

  39.                 while(i.hasNext()) {

  40.                         WebElement row = i.next();

  41.                         List<WebElement> columns = row.findElements(By.tagName("td"));

  42.                         Iterator<WebElement> j = columns.iterator();

  43.  

  44.                         while(j.hasNext()) {

  45.                                 WebElement column = j.next();

  46.                                 //Removing blank columns data and add a separator while displaying data.

  47.                                 if (!column.getText().trim().equals("")){

  48.                                         System.out.print(column.getText());

  49.                                         System.out.print(" | ");

  50.                                 }

  51.                         }

  52.                         System.out.println("");

  53.      System.out.println("-----------------------------------------------");

  54.                 }

  55.         }

  56.  

  57.         @After

  58.         public void tearDown() throws Exception {

  59.                 driver.quit();

  60.         }

  61. }

How can we automate pagination using Selenium Webdriver? Need to click on Next and Previous buttons to iterate through the table.

List<webElement> pagination =driver.findElemnts(By.xpath("//div[@class='nav-pages']//a")); 
// checkif pagination link exists 

if(pagination .size()>0){ 
sop("pagination exists"); 

// click on pagination link 

for(int i=0; i<pagination .size(); i++){ 
pagination.get(i).click(); 

} else { 
sop("pagination not exists");