diff options
author | Artur <artur@vaadin.com> | 2017-06-12 16:44:05 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-06-12 16:44:05 +0300 |
commit | 2f5e484a12f25d10ac6034d8ee9176b553b96c2d (patch) | |
tree | a12ee185335d2dbc9530a3300ec0915cd056c176 /testbench-api/src/main/java/com/vaadin/testbench | |
parent | c5cdca0c6290b96cf9499d29b4b5380466b33082 (diff) | |
download | vaadin-framework-2f5e484a12f25d10ac6034d8ee9176b553b96c2d.tar.gz vaadin-framework-2f5e484a12f25d10ac6034d8ee9176b553b96c2d.zip |
Add methods for setting the date as an ISO value for date fields
Diffstat (limited to 'testbench-api/src/main/java/com/vaadin/testbench')
3 files changed, 102 insertions, 0 deletions
diff --git a/testbench-api/src/main/java/com/vaadin/testbench/elements/AbstractDateFieldElement.java b/testbench-api/src/main/java/com/vaadin/testbench/elements/AbstractDateFieldElement.java index 5d298d3c3b..a920f90b7a 100644 --- a/testbench-api/src/main/java/com/vaadin/testbench/elements/AbstractDateFieldElement.java +++ b/testbench-api/src/main/java/com/vaadin/testbench/elements/AbstractDateFieldElement.java @@ -22,4 +22,32 @@ import com.vaadin.testbench.elementsbase.ServerClass; */ @ServerClass("com.vaadin.ui.AbstractDateField") public class AbstractDateFieldElement extends AbstractFieldElement { + + /** + * Gets the value of the date field as a ISO8601 compatible string + * (yyyy-MM-dd or yyyy-MM-dd'T'HH:mm:ss depending on whether the element + * supports time). + * + * @return the date in ISO-8601 format + * @since + */ + protected String getISOValue() { + return (String) getCommandExecutor() + .executeScript("return arguments[0].getISOValue();", this); + } + + /** + * Sets the value of the date field as a ISO8601 compatible string + * (yyyy-MM-dd or yyyy-MM-dd'T'HH:mm:ss depending on whether the element + * supports time). + * + * @param isoDateValue + * the date in ISO-8601 format + * @since + */ + protected void setISOValue(String isoDateValue) { + getCommandExecutor().executeScript( + "arguments[0].setISOValue(arguments[1]);", this, isoDateValue); + } + } diff --git a/testbench-api/src/main/java/com/vaadin/testbench/elements/DateFieldElement.java b/testbench-api/src/main/java/com/vaadin/testbench/elements/DateFieldElement.java index 162796cd3c..503eb1ca03 100644 --- a/testbench-api/src/main/java/com/vaadin/testbench/elements/DateFieldElement.java +++ b/testbench-api/src/main/java/com/vaadin/testbench/elements/DateFieldElement.java @@ -15,6 +15,9 @@ */ package com.vaadin.testbench.elements; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; @@ -72,4 +75,38 @@ public class DateFieldElement extends AbstractDateFieldElement { findElement(By.tagName("button")).click(); } + /** + * Sets the value to the given date and time. + * + * @param value + * the date and time to set. + */ + public void setDate(LocalDate value) { + setISOValue(value.format(getISOFormatter())); + } + + /** + * Gets the value as a LocalDate object. + * + * @return the current value as a date object, or null if a date is not set + * or if the text field contains an invalid date + */ + public LocalDate getDate() { + String value = getISOValue(); + if (value == null) { + return null; + } + return LocalDate.parse(value, getISOFormatter()); + } + + /** + * Gets a date and time formatter for ISO-8601 dates. + * + * @return a date formatter for ISO-8601 + * @since + */ + protected DateTimeFormatter getISOFormatter() { + return DateTimeFormatter.ISO_LOCAL_DATE; + } + } diff --git a/testbench-api/src/main/java/com/vaadin/testbench/elements/DateTimeFieldElement.java b/testbench-api/src/main/java/com/vaadin/testbench/elements/DateTimeFieldElement.java index 8e6155e33c..2a714d9a33 100644 --- a/testbench-api/src/main/java/com/vaadin/testbench/elements/DateTimeFieldElement.java +++ b/testbench-api/src/main/java/com/vaadin/testbench/elements/DateTimeFieldElement.java @@ -15,6 +15,9 @@ */ package com.vaadin.testbench.elements; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; @@ -63,4 +66,38 @@ public class DateTimeFieldElement extends AbstractDateFieldElement { findElement(By.tagName("button")).click(); } + /** + * Sets the value to the given date and time. + * + * @param value + * the date and time to set. + */ + public void setDateTime(LocalDateTime value) { + setISOValue(value.format(getISOFormatter())); + } + + /** + * Gets the value as a LocalDateTime object. + * + * @return the current value as a date object, or null if a date is not set + * or if the text field contains an invalid date + */ + public LocalDateTime getDateTime() { + String value = getISOValue(); + if (value == null) { + return null; + } + return LocalDateTime.parse(value, getISOFormatter()); + } + + /** + * Gets a date and time formatter for ISO-8601. + * + * @return a date and time formatter for ISO-8601 + * @since + */ + protected DateTimeFormatter getISOFormatter() { + return DateTimeFormatter.ISO_LOCAL_DATE_TIME; + } + } |