检查日期是否在selenium Web驱动程序(Java)中的日期范围内

时间:2016-08-15 09:44:10

标签: java date selenium

在我的网站中,我可以选择日期范围并列出日期范围内的所有交易。我的测试用例是验证列出的交易日期是否在选定的日期范围内。

这是我的代码。我将所有交易日期都记录到LinkedList中。 Comp_Dates方法将比较实际日期在“From”和“To”日期内。 问题是此代码将始终返回True。我已经更改了FromDate和ToDate来测试错误的场景,但仍然代码将返回True。

你能帮帮忙吗?这段代码有什么问题?

//Set From Date
        driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_dtDateFrom_txtDate")).sendKeys(Keys.chord(Keys.CONTROL, "a"),"01/03/2016");
        //Set To date
        driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_dtDateTo_txtDate")).sendKeys(Keys.chord(Keys.CONTROL, "a"),"30/04/2016");
          driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_btnList")).click();


 List<WebElement> Date = 
 driver.findElements(By.xpath(".//*  [@id='ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_stxOutstandingTransactions_gvOSTransactions']/tbody/tr[*]/td[1]"));

List<String> Dates = new LinkedList<String>();

for(int i=0;i<Date.size();i++)    
        {    
            Dates.add(Date.get(i).getText());    
            System.out.println(Dates);
        }    

        boolean result = comp_Dates(Dates);

        if (result=true)
        {
        System.out.println(result + ", Address are within the range");
        }
        else 
        {
        System.out.println(result + ", Addresses are not within the range. Test Case Failed");
        }
    }

         private static boolean comp_Dates(List<String> Dates)  {
             try
             {
             SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
                //Date date = fmt.parse("2013-05-06");
                String FromDate= "01/05/2016";
                String ToDate= "30/06/2016";
                java.util.Date Fdate =fmt.parse(FromDate);
                java.util.Date Tdate =fmt.parse(ToDate);

             for(String e : Dates) 
                { 
                    java.util.Date ActualDate = fmt.parse(e);       
                 if (ActualDate.compareTo(Fdate)>=0 & ActualDate.compareTo(Tdate)<=0 );
                    {
                        return true;

                    }

                 }
             }
             catch (Exception ex ){
                  System.out.println(ex);
               }
            return false;

             }

            }

链接列表中的交易日期为[18/04 / 2016,14 / 04 / 2016,13 / 04/2016] 我在代码中指定了以下日期。

String FromDate =“01/05/2016”;

String ToDate =“30/06/2016”;

比较这些日期时,代码应返回false,因为日期不属于From和To日期。但它返回True。我在这里做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

当您返回true时,只要在范围内找到日期,它就会退出该函数。因此,它不会检查列表中的所有日期。

如果您想检查所有日期,正确的comp_Dates方法可能是:

    //Set From Date
    driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_dtDateFrom_txtDate")).sendKeys(Keys.chord(Keys.CONTROL, "a"), "01/03/2016");
    //Set To date
    driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_dtDateTo_txtDate")).sendKeys(Keys.chord(Keys.CONTROL, "a"), "30/04/2016");
    driver.findElement(By.id("ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_btnList")).click();


    List<WebElement> Date =
            driver.findElements(By.xpath(".//*  [@id='ctl00_ContentPlaceHolderMain_container_container_Block_172_tabPanelMyAccounts_stxOutstandingTransactions_gvOSTransactions']/tbody/tr[*]/td[1]"));


    for (int i = 0; i < Date.size(); i++) {
        String date = Date.get(i).getText();
        boolean result = comp_Dates(date);

        if (result) {
            System.out.println(result + ", Address are within the range");
        } else {
            System.out.println(result + ", Addresses are not within the range. Test Case Failed");
        }
    }

private static boolean comp_Dates(String date) {
    try {
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");

        String FromDate = "01/05/2016";
        String ToDate = "30/06/2016";

        java.util.Date Fdate = fmt.parse(FromDate);
        java.util.Date Tdate = fmt.parse(ToDate);
        java.util.Date ActualDate = fmt.parse(date);

        if (ActualDate.compareTo(Fdate) >= 0 && ActualDate.compareTo(Tdate) <= 0) {
            return true;
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return false;
}

N.B:你的代码中有很多拼写错误。你应该解决这些问题。