2016-11-08 22 views
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(); 

      } 
     } 

例外:螺纹我得到下面的代码错误硒

异常 “主要” java.lang.IndexOutOfBoundsException:指标:1,大小:1在java.util中。 ArrayList.rangeCheck(未知来源)在java.util.ArrayList.get(未知来源)在eMpower.AddEmployee2.main(AddEmployee2.java:109)

+0

异常线程 “main” java.lang.IndexOutOfBoundsException:指标:1,尺寸:1 \t在java.util.ArrayList.rangeCheck(来源不明) \t在java.util中.ArrayList.get(来源不明) \t在eMpower.AddEmployee2.main(AddEmployee2.java:109) –

+1

哪条线是109? – MordechayS

+0

并请添加例外在你的问题...... – MordechayS

回答

0

阵列的第一索引是 “0”,而不是“1 “,所以对于大小为1的数组,索引1是IndexOutOfBoundsException。没有相关的硒,但真正的Java

+0

没错,但可能不会在这种情况下,一个合适的解决方案,除非你没有指望109线,看到喜欢的东西获得(1) –

+1

懒得算。我想这是date_dd_MM_yyyy [1] :) 获得(1)名单相关的,我想 – dimkin

+0

previousLink.click();上面代码中的事件只工作五次,但我的循环迭代27次点击事件也工作27次。为什么它发生?帮我...! –

相关问题