0
public class AddEmployee2 {

    public static void main(String[] args) throws Exception {
        String dateTime ="26/02/1989";

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://testing-1/Premium_Next_03June2016/default.aspx");
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);    

        driver.findElement(By.id("txtUsername")).sendKeys("Super");
        driver.findElement(By.id("txtPassword")).sendKeys("sa");
        driver.findElement(By.id("btnLogin")).click();

        Actions act = new Actions(driver);
        /* Find the xpath of File menu*/
        WebElement element = (driver.findElement(By.xpath("id('maintext-1')")));
        act.moveToElement(element).perform();
        driver.findElement(By.xpath(".//*[@id='subtext-1-10']/span")).click();
        driver.findElement(By.id("_ctl0_MainBody_cmdNew")).click();
        driver.findElement(By.id("_ctl0_MainBody_txtCode")).sendKeys("501");
        driver.findElement(By.id("_ctl0_MainBody_txtName")).sendKeys("Rahul");
           //button to open calendar

         WebElement selectDate = driver.findElement(By.xpath(".//*[@id='_ctl0_MainBody_txtDOB_TextDatepicker']"));
         selectedDate.click();
         WebElement nextLink =   driver.findElement(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_nextArrow']"));
         WebElement midLink = driver.findElement(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_title']"));
         WebElement previousLink = driver.findElement(By.xpath(" //div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_prevArrow']"));
         String date_dd_MM_yyyy[] = (dateTime.split(" ")[0]).split("/");
         int yearDiff = Integer.parseInt(date_dd_MM_yyyy[2])- Calendar.getInstance().get(Calendar.YEAR);
         System.out.println("Test"+Integer.parseInt(date_dd_MM_yyyy[2]));
         System.out.println("what is this"+Calendar.getInstance().get(Calendar.YEAR));
         System.out.println("Year difference Ram"+yearDiff);             

         midLink.click();              
         if(yearDiff!=0){

             //if you have to move next year

             if(yearDiff>0){

                 for(int i=0;i< yearDiff;i++){

                     System.out.println("Year Diff next->"+i);

                     nextLink.click();

                 }

             }

             //if you have to move previous year

             else if(yearDiff<0){

                 for(int i=0;i< (yearDiff*(-1));i++){

                     System.out.println("Year Diff previous->"+i);
                     previousLink.click();

                 }
                 Thread.sleep(1000);

         //Get all months from calendar to select correct one


         List<WebElement> list_AllMonthToBook = driver.findElements(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_months']//table[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_monthsTable']//tbody[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_monthsBody']"));
         list_AllMonthToBook.get(Integer.parseInt(date_dd_MM_yyyy[1])-1).click();

        // System.out.println("Get all Months"+(Integer.parseInt(date_dd_MM_yyyy[1])-1));

         Thread.sleep(1000);

         //get all dates from calendar to select correct one

         List<WebElement> list_AllDateToBook = driver.findElements(By.xpath("//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month'))]"));

         list_AllDateToBook.get(Integer.parseInt(date_dd_MM_yyyy[0])-1).click();

             }
         }

Exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at eMpower.AddEmployee2.main(AddEmployee2.java:109)

ArK
  • 20,698
  • 67
  • 109
  • 136

1 Answers1

0

1st index of arrays is "0", not "1", So with array with size 1, index 1 is IndexOutOfBoundsException. Nothing related to Selenium, but true java

dimkin
  • 108
  • 1
  • 11
  • True, but, probably not a suitable solution in this case, unless you hadn't count 109 lines and saw something like .get(1) – A. Chubarov Nov 08 '16 at 11:33
  • 1
    too lazy to count. I imagine it's date_dd_MM_yyyy[1] :) .get(1) is List related , I suppose – dimkin Nov 08 '16 at 12:24
  • previousLink.click(); event in above code is working only five times but my loop is iterate 27 times click event is also work 27 times. so why its happening? Help me...! – Ramprasad Patil Nov 09 '16 at 11:05
  • debug it, check why array of something is too small – dimkin Nov 09 '16 at 13:38