Skip to content

Commit 232b1d5

Browse files
committed
Adding some dates utils
1 parent b5857fd commit 232b1d5

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/main/java/uk/co/evoco/testdata/Dates.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.andreinc.mockneat.abstraction.MockUnitBase;
44
import org.joda.time.DateTime;
5+
import org.joda.time.Duration;
56
import org.joda.time.LocalDate;
67
import org.joda.time.format.DateTimeFormat;
78
import org.joda.time.format.DateTimeFormatter;
@@ -52,6 +53,21 @@ public static String now(String dateFormat) {
5253
return dateTime.toString(dateTimeFormatter);
5354
}
5455

56+
/**
57+
* Returns boolean result on if the two given dates are within the given tolerance
58+
* @param dateFormat
59+
* @param baseDate
60+
* @param comparisonDate
61+
* @param toleranceInMinutes
62+
* @return
63+
*/
64+
public static boolean isDateWithinTolerance(String dateFormat, String baseDate, String comparisonDate, long toleranceInMinutes) {
65+
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateFormat);
66+
long differenceInMillis = DateTime.parse(comparisonDate, dateTimeFormatter).getMillis() - DateTime.parse(baseDate, dateTimeFormatter).getMillis();
67+
long differenceInMinutes = (differenceInMillis/1000)/60;
68+
return differenceInMinutes <= toleranceInMinutes;
69+
}
70+
5571
/**
5672
*
5773
* @param startDate the date to start with

src/main/java/uk/co/evoco/webdriver/utils/FindByUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ private FindByUtils() {
3232
* Finds the first element that is displayed with given locator.
3333
* Useful for instances where there are multiple elements on the DOM with the same locator and one (or more)
3434
* of them are hidden (e.g. used in mobile layouts or hold a different z-index given some other element is present)
35+
*
3536
* @param webDriver active WebDriver instance
36-
* @param locator selector for target WebElement
37+
* @param locator selector for target WebElement
3738
* @return active WebElement, already located
3839
*/
3940
public static WebElement multipleLocatorMatchGetDisplayed(WebDriver webDriver, By locator)
4041
throws WebDriverException {
41-
try(final Timer.Context ignored = multipleLocatorMatchGetDisplayedAction.time()) {
42+
try (final Timer.Context ignored = multipleLocatorMatchGetDisplayedAction.time()) {
4243
List<WebElement> elements = webDriver.findElements(locator);
4344
logger.info("Found {} elements with locator: {}", elements.size(), locator.toString());
4445
for (WebElement element : elements) {
@@ -52,14 +53,14 @@ public static WebElement multipleLocatorMatchGetDisplayed(WebDriver webDriver, B
5253

5354
/**
5455
* Finds elements by either the HTML name or id attribute using the given selector text
56+
*
5557
* @param webDriver active WebDriver instance
56-
* @param idOrName the variable locator
58+
* @param idOrName the variable locator
5759
* @return active WebElement, already located
5860
*/
5961
public static WebElement byIdOrName(WebDriver webDriver, String idOrName) {
60-
try(final Timer.Context ignored = byIdOrNameAction.time()) {
62+
try (final Timer.Context ignored = byIdOrNameAction.time()) {
6163
return webDriver.findElement(new ByIdOrName(idOrName));
6264
}
6365
}
64-
}
65-
66+
}

src/test/java/uk/co/evoco/testdata/DatesTests.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
import static org.hamcrest.CoreMatchers.is;
88
import static org.hamcrest.MatcherAssert.assertThat;
9-
import static uk.co.evoco.testdata.Dates.futureDateAvoidingWeekends;
10-
import static uk.co.evoco.testdata.Dates.futureDate;
11-
import static uk.co.evoco.testdata.Dates.pastDate;
12-
import static uk.co.evoco.testdata.Dates.futureDataAvoidingWeekendsAndBankHolidays;
9+
import static uk.co.evoco.testdata.Dates.*;
1310

1411
public class DatesTests {
1512

@@ -64,4 +61,28 @@ public void testCanDataTimeInFutureAvoidingWeekend() {
6461
is("13/06/2019 13:54"));
6562
}
6663

64+
@Test
65+
public void testCanCheckDateWithinGivenToleranceOnUpperBoundary() {
66+
assertThat(isDateWithinTolerance("dd/MM/yyyy HH:mm","27/06/2021 19:55", "27/06/2021 20:00", 5), is(true));
67+
}
68+
69+
@Test
70+
public void testCanCheckDateWithinGivenToleranceOverUpperBoundary() {
71+
assertThat(isDateWithinTolerance("dd/MM/yyyy HH:mm","27/06/2021 19:55", "27/06/2021 20:01", 5), is(false));
72+
}
73+
74+
@Test
75+
public void testCanCheckDateWithinGivenToleranceWithinUpperBoundary() {
76+
assertThat(isDateWithinTolerance("dd/MM/yyyy HH:mm","27/06/2021 19:55", "27/06/2021 19:59", 5), is(true));
77+
}
78+
79+
@Test
80+
public void testCanCheckDateWithinGivenToleranceOneDayOverOnUpper() {
81+
assertThat(isDateWithinTolerance("dd/MM/yyyy HH:mm","27/06/2021 19:55", "28/06/2021 19:59", 5), is(false));
82+
}
83+
84+
@Test
85+
public void testCanCheckDateWithinGivenToleranceOneDayUnderOnLower() {
86+
assertThat(isDateWithinTolerance("dd/MM/yyyy HH:mm","26/06/2021 19:55", "27/06/2021 19:59", 5), is(false));
87+
}
6788
}

0 commit comments

Comments
 (0)