Skip to content

Commit

Permalink
Use JavaScript to set the value of datetime-local inputs (for Chrome)
Browse files Browse the repository at this point in the history
Relates apache#188
  • Loading branch information
Adam Pounder authored and Adam Pounder committed Oct 22, 2024
1 parent e48bd91 commit 7e3332c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ class DateTimeLocalInput extends AbstractInput {
final String inputType = 'datetime-local'

void setDateTime(LocalDateTime dateTime) {
value(dateTime.toString())
doSetDateTime(dateTime.toString())
}

void setDateTime(String iso8601FormattedDateTime) {
value(iso8601FormattedDateTime)
doSetDateTime(iso8601FormattedDateTime)
}

private void doSetDateTime(String iso8601FormattedDateTime) {
String truncated = iso8601FormattedDateTime.substring(0, 23)
allElements().each { elem ->
browser.js.exec(elem, truncated, '''\
if (document.activeElement !== arguments[0]){
arguments[0].focus();
}
if (arguments[0].value != arguments[1]) {
arguments[0].value = arguments[1]
arguments[0].dispatchEvent(new InputEvent('input'));
arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
}
''')
}
}

LocalDateTime getDateTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import java.time.LocalDateTime

@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() {
Expand All @@ -47,7 +46,7 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer {
input.dateTime = dateTime

then:
input.dateTime == dateTime
input.dateTime == truncated(dateTime)

where:
dateTime = LocalDateTime.now()
Expand All @@ -58,7 +57,7 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer {
input.dateTime = dateTime.toString()

then:
input.dateTime == dateTime
input.dateTime == truncated(dateTime)

where:
dateTime = LocalDateTime.now()
Expand All @@ -69,13 +68,16 @@ class DateTimeLocalInputSpec extends GebSpecWithCallbackServer {
input.dateTime = dateTime

and:
input.dateTime = dateTime.plusDays(1)
input.dateTime = truncated(dateTime.plusDays(1))

then:
input.dateTime == dateTime.plusDays(1)
input.dateTime == truncated(dateTime.plusDays(1))

where:
dateTime = LocalDateTime.now()
}

private static LocalDateTime truncated(LocalDateTime dateTime) {
return LocalDateTime.parse(dateTime.toString().substring(0, 23))
}
}

0 comments on commit 7e3332c

Please sign in to comment.