This is an automated email from the ASF dual-hosted git repository. jonnybot pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/groovy-geb.git
commit df7d70914b2fce8b6ab72fab822d31b75c205c95 Author: Adam Pounder <[email protected]> AuthorDate: Thu Oct 24 13:13:13 2024 +0100 #194 - Reformat any date time values passed to the DateTimeLocalInput module into a format that will be accepted by Chrome --- .../groovy/geb/module/DateTimeLocalInput.groovy | 31 ++++++++++++++++++++-- .../geb/module/DateTimeLocalInputSpec.groovy | 13 +++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/module/geb-core/src/main/groovy/geb/module/DateTimeLocalInput.groovy b/module/geb-core/src/main/groovy/geb/module/DateTimeLocalInput.groovy index 92c7f46f..cef76fff 100644 --- a/module/geb-core/src/main/groovy/geb/module/DateTimeLocalInput.groovy +++ b/module/geb-core/src/main/groovy/geb/module/DateTimeLocalInput.groovy @@ -15,18 +15,36 @@ */ package geb.module +import groovy.util.logging.Slf4j + import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import java.time.format.DateTimeFormatterBuilder + +import static java.time.temporal.ChronoField.* +@Slf4j class DateTimeLocalInput extends AbstractInput { + private static final DateTimeFormatter DATE_TIME_FORMAT = new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(MILLI_OF_SECOND, 0, 3, true) + .toFormatter() + final String inputType = 'datetime-local' void setDateTime(LocalDateTime dateTime) { - value(dateTime.toString()) + value(reformat(dateTime)) } void setDateTime(String iso8601FormattedDateTime) { - value(iso8601FormattedDateTime) + setDateTime(LocalDateTime.parse(iso8601FormattedDateTime)) } LocalDateTime getDateTime() { @@ -39,4 +57,13 @@ class DateTimeLocalInput extends AbstractInput { super.isTypeValid(type) || type == "text" } + private static String reformat(LocalDateTime localDateTime) { + String inputValue = localDateTime.format(DATE_TIME_FORMAT) + if(localDateTime.toString() != inputValue) { + log.warn("The datetime value {} was truncated to {} as it was being used to set the value of a <input type=\"datetime-local\" />", + localDateTime, + inputValue) + } + return inputValue + } } diff --git a/module/geb-core/src/test/groovy/geb/module/DateTimeLocalInputSpec.groovy b/module/geb-core/src/test/groovy/geb/module/DateTimeLocalInputSpec.groovy index 2c4c6447..7a0d17fa 100644 --- a/module/geb-core/src/test/groovy/geb/module/DateTimeLocalInputSpec.groovy +++ b/module/geb-core/src/test/groovy/geb/module/DateTimeLocalInputSpec.groovy @@ -18,13 +18,12 @@ package geb.module import geb.test.GebSpecWithCallbackServer import geb.test.browsers.Chrome import geb.test.browsers.RequiresRealBrowser -import spock.lang.Ignore import java.time.LocalDateTime +import java.time.temporal.ChronoUnit @Chrome @RequiresRealBrowser // maybe due to https://sourceforge.net/p/htmlunit/bugs/1923/ -@Ignore("https://github.com/geb/geb/issues/188") class DateTimeLocalInputSpec extends GebSpecWithCallbackServer { def setup() { @@ -47,7 +46,7 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer { input.dateTime = dateTime then: - input.dateTime == dateTime + input.dateTime == truncated(dateTime) where: dateTime = LocalDateTime.now() @@ -58,7 +57,7 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer { input.dateTime = dateTime.toString() then: - input.dateTime == dateTime + input.dateTime == truncated(dateTime) where: dateTime = LocalDateTime.now() @@ -72,10 +71,14 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer { input.dateTime = dateTime.plusDays(1) then: - input.dateTime == dateTime.plusDays(1) + input.dateTime == truncated(dateTime.plusDays(1)) where: dateTime = LocalDateTime.now() } + + private static LocalDateTime truncated(LocalDateTime ldt) { + return ldt.truncatedTo(ChronoUnit.MILLIS) + } }
