Skip to content

Commit

Permalink
cherry-pick(#31420): docs(java): correctly parse time (#31422)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Jun 24, 2024
1 parent 1368bca commit d557b7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions docs/src/api/class-clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ page.clock.pause_at("2020-02-02")
```

```java
page.clock().pauseAt(Instant.parse("2020-02-02"));
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
page.clock().pauseAt(format.parse("2020-02-02"));
page.clock().pauseAt("2020-02-02");
```

Expand Down Expand Up @@ -182,8 +183,8 @@ page.clock.set_fixed_time("2020-02-02")
```

```java
page.clock().setFixedTime(Instant.now());
page.clock().setFixedTime(Instant.parse("2020-02-02"));
page.clock().setFixedTime(new Date());
page.clock().setFixedTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
page.clock().setFixedTime("2020-02-02");
```

Expand Down Expand Up @@ -225,8 +226,8 @@ page.clock.set_system_time("2020-02-02")
```

```java
page.clock().setSystemTime(Instant.now());
page.clock().setSystemTime(Instant.parse("2020-02-02"));
page.clock().setSystemTime(new Date());
page.clock().setSystemTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
page.clock().setSystemTime("2020-02-02");
```

Expand Down
10 changes: 6 additions & 4 deletions docs/src/clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:30:00 AM"
```java
// Initialize clock with some time before the test time and let the page load
// naturally. `Date.now` will progress as the timers fire.
page.clock().install(new Clock.InstallOptions().setTime(Instant.parse("2024-02-02T08:00:00")));
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss");
page.clock().install(new Clock.InstallOptions().setTime(format.parse("2024-02-02T08:00:00")));
page.navigate("http://localhost:3333");
Locator locator = page.getByTestId("current-time");

// Pretend that the user closed the laptop lid and opened it again at 10am.
// Pause the time once reached that point.
page.clock().pauseAt(Instant.parse("2024-02-02T10:00:00"));
page.clock().pauseAt(format.parse("2024-02-02T10:00:00"));

// Assert the page state.
assertThat(locator).hasText("2/2/2024, 10:00:00 AM");
Expand Down Expand Up @@ -315,15 +316,16 @@ expect(locator).to_have_text("2/2/2024, 10:00:02 AM")
```

```java
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss");
// Initialize clock with a specific time, let the page load naturally.
page.clock().install(new Clock.InstallOptions()
.setTime(Instant.parse("2024-02-02T08:00:00")));
.setTime(format.parse("2024-02-02T08:00:00")));
page.navigate("http://localhost:3333");
Locator locator = page.getByTestId("current-time");

// Pause the time flow, stop the timers, you now have manual control
// over the page time.
page.clock().pauseAt(Instant.parse("2024-02-02T10:00:00"));
page.clock().pauseAt(format.parse("2024-02-02T10:00:00"));
assertThat(locator).hasText("2/2/2024, 10:00:00 AM");

// Tick through time manually, firing all timers in the process.
Expand Down

0 comments on commit d557b7b

Please sign in to comment.