AtDateTime.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { I } = inject();
  2. module.exports = {
  3. _dateInputId: "__test_date_format__",
  4. /**
  5. * Browsers use system date format for input[type=date], not locale,
  6. * and inputs don't provide any API to get this format or user input,
  7. * only the final value in ISO format.
  8. * So we fill in some specific input and check the result.
  9. * Depending on system settings, entered digits can go to different places,
  10. * generating one of 4 possible date formats —year can only be at the beginning
  11. * or at the end.
  12. * We focus only on formats with full numeric day, month and year (DD and YYYY).
  13. * @returns {Promise<string>} date format of y, m, d (e.g. 'ymd')
  14. */
  15. async detectDateFormat() {
  16. // create invisible date input
  17. await I.executeScript(
  18. ({ id }) => {
  19. const date = document.createElement("input");
  20. date.type = "date";
  21. date.id = id;
  22. Object.assign(date.style, {
  23. position: "absolute",
  24. top: 0,
  25. opacity: 0,
  26. });
  27. document.body.appendChild(date);
  28. },
  29. { id: this._dateInputId },
  30. );
  31. I.fillField(`#${this._dateInputId}`, "01020304");
  32. const format = await I.executeScript(
  33. ({ id }) => {
  34. const date = document.getElementById(id) as HTMLInputElement;
  35. const value = date.value; // always ISO format
  36. switch (value) {
  37. case "0102-04-03":
  38. return "ydm";
  39. case "0102-03-04":
  40. return "ymd";
  41. case "0304-02-01":
  42. return "dmy";
  43. case "0304-01-02":
  44. return "mdy";
  45. default:
  46. return "ymd";
  47. }
  48. },
  49. { id: this._dateInputId },
  50. );
  51. // remove this input
  52. await I.executeScript(
  53. ({ id }) => {
  54. (document.getElementById(id) as HTMLInputElement).remove();
  55. },
  56. { id: this._dateInputId },
  57. );
  58. return format;
  59. },
  60. };