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