/**
* @author etao
* @description check last time selected
* @param timePeriod
* @param dateFormat
* @CreateDate 21 Dec 2016 13:50:39
* @modifyUser
* @modifyDate
* @comments
*/
public void checkLastTimeSelected(String timePeriod, String dateFormat){
List<WebElement> lastTimeSelectedList = page.getLastTimeSelectedList();
List<String> dateStr = new ArrayList<String>();
for (WebElement e : lastTimeSelectedList) {
String tempStr = e.getText().trim();
tempStr = tempStr.split(" ")[0];
dateStr.add(tempStr.trim());
}
isAllTimeCorrect(dateFormat, timePeriod, dateStr);
}
public void isAllTimeCorrect(String dateFormat, String timePeriod, List<String> date) {
for (String selectedDay : date) {
if(timePeriod.equals("Last Day")){
checkReportDateInTimeRange(Calendar.DAY_OF_MONTH, -1, dateFormat, selectedDay);
}else if (timePeriod.equals("Last Week")) {
checkReportDateInTimeRange(Calendar.DATE, -7, dateFormat, selectedDay);
}else if (timePeriod.equals("Last Month")) {
checkReportDateInTimeRange(Calendar.MONTH, -1, dateFormat, selectedDay);
}else if (timePeriod.equals("Last 2 Months")) {
checkReportDateInTimeRange(Calendar.MONTH, -2, dateFormat, selectedDay);
}else if (timePeriod.equals("Last 3 Months")) {
checkReportDateInTimeRange(Calendar.MONTH, -3, dateFormat, selectedDay);
}else if (timePeriod.equals("Last 6 Months")) {
checkReportDateInTimeRange(Calendar.MONTH, -6, dateFormat, selectedDay);
}else if (timePeriod.equals("Last 9 Months")) {
checkReportDateInTimeRange(Calendar.MONTH, -9, dateFormat, selectedDay);
}else if (timePeriod.equals("Last Year")) {
checkReportDateInTimeRange(Calendar.YEAR, -1, dateFormat, selectedDay);
}
}
}
public void checkReportDateInTimeRange(int calendar, int diff, String dateFormat ,String selectedDay) {
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(dateFormat, Locale.US);
List<String> dateStr = new ArrayList<String>();
Calendar c = Calendar.getInstance();
dateStr.add(format.format(c.getTime()));//add date of today into string
dateStr.add(selectedDay);//add date of selected date into string
c.add(calendar, diff);
c.add(Calendar.DATE, -1);
dateStr.add(format.format(c.getTime()));//add date of last time range
Assert.assertTrue(checkDatePeriod(dateStr, dateFormat),"The last time selected date is not in the time range");
}
public boolean checkDatePeriod(List<String> date, String dateFormat) {
boolean flg = true;
DateFormat df = new SimpleDateFormat(dateFormat, Locale.US);
try {
for (int i = 1; i < date.size(); i++) {
Date d1 = df.parse(date.get(i - 1));
Date d2 = df.parse(date.get(i));
flg = flg && (d1.getTime() >= d2.getTime());
if (flg == false) {
System.out.println("******* In the third return");
System.out.println("=== date -1: " + date.get(i - 1).toString());
System.out.println("=== date: " + date.get(i).toString());
if (i + 1 < date.size()) {
System.out.println("=== date +1: " + date.get(i + 1).toString());
}
break;
}
}
return flg;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}