Search code examples
javawebdriverselenium-webdrivertestngleap-year

Verification of Leap Year for DOB field in WebDriver using Java


I have a text field for inputting Date of Birth(DOB) as "dd/mm/yyyy" format. I like to automate the test with WebDriver for Leap Year checking for that field. I'm using Java and TestNG with WebDriver (Selenium 2). I know the formula/logic for Leap Year mentioned below:

(year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

HTML code for the text field is as below:

<input type="text" class="text-field" id="DOB" value="" name="field(DOB)">

I want to verify the Leap Year checking in a certain range of year (for example 1900 - 2012). How can I design and write Test Case for that?


Solution

  • The following WebDriver Java code should work well:

    for(int year = 1900; year <= 2012; year++){
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
    driver.findElement(By.id("DOB")).clear();
    driver.findElement(By.id("DOB")).sendKeys("29/02/" + year);
    }
    }