Tuesday, 4 February 2014

Working with Dates using Selenium Webdriver

Working with the Dates : 


It is very common to work with dates in part of our automation testing,Generally we come across the following requirements:


  1. Getting  the current System date in various formats
  2. Getting the previous dates in various formats
  3. Getting the future dates in various formats
This post will make easier the things.


Steps to follow:

  1. Download the jar file from https://drive.google.com/file/d/0B9ZTXXKTZAYVUGZ5d0loWUNhMlk/edit?usp=sharing
  2. Add the ‘testUtils.jar’ (I will provide the .Jar file ) file to your project class path,    Import the following class in your Test.java file
  3.              Import com.webdriver.framework.*;     (If it’s not already imported)
  4.     Use the code as shown below :



3 comments:

  1. Hello,

    While executing the below code, i am getting wrong month. Day and Year is correct. Could you please check ?


    import org.testng.annotations.Test;

    import com.webdriver.framework.TestUtils;

    public class s022_workingWithDates {

    @Test
    public void tests022_workingWithDates() {

    TestUtils util = new TestUtils();

    System.out.println(util.computedDateFromTodayDate("dd/mm/yyyy",0));
    System.out.println(util.computedDateFromTodayDate("mm/dd/yyyy",0));
    System.out.println(util.computedDateFromTodayDate("yyyy/mm/dd",0));
    System.out.println("\n");
    System.out.println(util.computedDateFromTodayDate("dd/mm/yyyy",1));
    System.out.println(util.computedDateFromTodayDate("mm/dd/yyyy",1));
    System.out.println(util.computedDateFromTodayDate("yyyy/mm/dd",1));
    System.out.println("\n");
    System.out.println(util.computedDateFromTodayDate("dd/mm/yyyy",-1));
    System.out.println(util.computedDateFromTodayDate("mm/dd/yyyy",-1));
    System.out.println(util.computedDateFromTodayDate("yyyy/mm/dd",-1));
    }

    }

    ReplyDelete
  2. Please try with capital MM, Please find the corrected code:

    System.out.println(util.computedDateFromTodayDate("dd/MM/yyyy",0));
    System.out.println(util.computedDateFromTodayDate("MM/dd/yyyy",0));
    System.out.println(util.computedDateFromTodayDate("yyyy/MM/dd",0));
    System.out.println("\n");
    System.out.println(util.computedDateFromTodayDate("dd/MM/yyyy",1));
    System.out.println(util.computedDateFromTodayDate("MM/dd/yyyy",1));
    System.out.println(util.computedDateFromTodayDate("yyyy/MM/dd",1));
    System.out.println("\n");
    System.out.println(util.computedDateFromTodayDate("dd/MM/yyyy",-1));
    System.out.println(util.computedDateFromTodayDate("MM/dd/yyyy",-1));
    System.out.println(util.computedDateFromTodayDate("yyyy/MM/dd",-1));

    ReplyDelete
  3. OUTPUT for the above code is :

    20/02/2014
    02/20/2014
    2014/02/20


    21/02/2014
    02/21/2014
    2014/02/21


    19/02/2014
    02/19/2014
    2014/02/19

    ReplyDelete