Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jul 1, 2024
1 parent 4dbc172 commit 2bbad09
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions playwright/_impl/_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def install(self, time: Union[float, str, datetime.datetime] = None) -> No

async def fast_forward(
self,
ticks: Union[float, str],
ticks: Union[int, str],
) -> None:
await self._browser_context._channel.send(
"clockFastForward", parse_ticks(ticks)
Expand All @@ -51,7 +51,7 @@ async def resume(

async def run_for(
self,
ticks: Union[float, str],
ticks: Union[int, str],
) -> None:
await self._browser_context._channel.send("clockRunFor", parse_ticks(ticks))

Expand All @@ -71,16 +71,16 @@ async def set_system_time(


def parse_time(
time: Union[float, str, datetime.datetime]
time: Union[float, int, str, datetime.datetime]
) -> Dict[str, Union[int, str]]:
if isinstance(time, (int, float)):
if isinstance(time, (float, int)):
return {"timeNumber": int(time)}
if isinstance(time, str):
return {"timeString": time}
return {"timeNumber": int(time.timestamp())}
return {"timeNumber": int(time.timestamp() * 1_000)}


def parse_ticks(ticks: Union[float, str]) -> Dict[str, Union[int, str]]:
if isinstance(ticks, (float, int)):
return {"ticksNumber": int(ticks)}
def parse_ticks(ticks: Union[int, str]) -> Dict[str, Union[int, str]]:
if isinstance(ticks, int):
return {"ticksNumber": ticks}
return {"ticksString": ticks}
12 changes: 4 additions & 8 deletions playwright/async_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6694,7 +6694,7 @@ async def install(

return mapping.from_maybe_impl(await self._impl_obj.install(time=time))

async def fast_forward(self, ticks: typing.Union[float, str]) -> None:
async def fast_forward(self, ticks: typing.Union[int, str]) -> None:
"""Clock.fast_forward

Advance the clock by jumping forward in time. Only fires due timers at most once. This is equivalent to user
Expand All @@ -6709,9 +6709,7 @@ async def fast_forward(self, ticks: typing.Union[float, str]) -> None:

Parameters
----------
ticks : Union[float, str]
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are
"08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
ticks : Union[int, str]
"""

return mapping.from_maybe_impl(await self._impl_obj.fast_forward(ticks=ticks))
Expand Down Expand Up @@ -6748,7 +6746,7 @@ async def resume(self) -> None:

return mapping.from_maybe_impl(await self._impl_obj.resume())

async def run_for(self, ticks: typing.Union[float, str]) -> None:
async def run_for(self, ticks: typing.Union[int, str]) -> None:
"""Clock.run_for

Advance the clock, firing all the time-related callbacks.
Expand All @@ -6762,9 +6760,7 @@ async def run_for(self, ticks: typing.Union[float, str]) -> None:

Parameters
----------
ticks : Union[float, str]
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are
"08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
ticks : Union[int, str]
"""

return mapping.from_maybe_impl(await self._impl_obj.run_for(ticks=ticks))
Expand Down
12 changes: 4 additions & 8 deletions playwright/sync_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6804,7 +6804,7 @@ def install(

return mapping.from_maybe_impl(self._sync(self._impl_obj.install(time=time)))

def fast_forward(self, ticks: typing.Union[float, str]) -> None:
def fast_forward(self, ticks: typing.Union[int, str]) -> None:
"""Clock.fast_forward

Advance the clock by jumping forward in time. Only fires due timers at most once. This is equivalent to user
Expand All @@ -6819,9 +6819,7 @@ def fast_forward(self, ticks: typing.Union[float, str]) -> None:

Parameters
----------
ticks : Union[float, str]
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are
"08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
ticks : Union[int, str]
"""

return mapping.from_maybe_impl(
Expand Down Expand Up @@ -6860,7 +6858,7 @@ def resume(self) -> None:

return mapping.from_maybe_impl(self._sync(self._impl_obj.resume()))

def run_for(self, ticks: typing.Union[float, str]) -> None:
def run_for(self, ticks: typing.Union[int, str]) -> None:
"""Clock.run_for

Advance the clock, firing all the time-related callbacks.
Expand All @@ -6874,9 +6872,7 @@ def run_for(self, ticks: typing.Union[float, str]) -> None:

Parameters
----------
ticks : Union[float, str]
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are
"08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
ticks : Union[int, str]
"""

return mapping.from_maybe_impl(self._sync(self._impl_obj.run_for(ticks=ticks)))
Expand Down
7 changes: 4 additions & 3 deletions tests/async/test_page_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ async def test_should_tick_after_popup(self, page: Page) -> None:
page.wait_for_event("popup"), page.evaluate("window.open('about:blank')")
)
popup_time = await popup.evaluate("Date.now()")
assert popup_time == now.timestamp()
assert popup_time == now.timestamp() * 1000
await page.clock.run_for(1000)
popup_time_after = await popup.evaluate("Date.now()")
assert popup_time_after == now.timestamp() + 1000
assert popup_time_after == now.timestamp() * 1000 + 1000

async def test_should_tick_before_popup(self, page: Page) -> None:
await page.clock.install(time=0)
Expand All @@ -296,7 +296,8 @@ async def test_should_tick_before_popup(self, page: Page) -> None:
page.wait_for_event("popup"), page.evaluate("window.open('about:blank')")
)
popup_time = await popup.evaluate("Date.now()")
assert popup_time == int(now.timestamp() + 1000)
assert popup_time == int(now.timestamp() * 1000 + 1000)
assert datetime.datetime.fromtimestamp(popup_time / 1_000).year == 2015

async def test_should_run_time_before_popup(
self, page: Page, server: Server
Expand Down
13 changes: 10 additions & 3 deletions tests/sync/test_page_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ def test_should_tick_after_popup(self, page: Page) -> None:
page.evaluate("window.open('about:blank')")
popup = popup_info.value
popup_time = popup.evaluate("Date.now()")
assert popup_time == now.timestamp()
assert popup_time == now.timestamp() * 1000
page.clock.run_for(1000)
popup_time_after = popup.evaluate("Date.now()")
assert popup_time_after == now.timestamp() + 1000
assert popup_time_after == now.timestamp() * 1000 + 1000

def test_should_tick_before_popup(self, page: Page) -> None:
page.clock.install(time=0)
Expand All @@ -279,7 +279,8 @@ def test_should_tick_before_popup(self, page: Page) -> None:
page.evaluate("window.open('about:blank')")
popup = popup_info.value
popup_time = popup.evaluate("Date.now()")
assert popup_time == int(now.timestamp() + 1000)
assert popup_time == int(now.timestamp() * 1_000 + 1000)
assert datetime.datetime.fromtimestamp(popup_time / 1_000).year == 2015

def test_should_run_time_before_popup(self, page: Page, server: Server) -> None:
server.set_route(
Expand Down Expand Up @@ -323,6 +324,12 @@ def test_should_not_run_time_before_popup_on_pause(


class TestSetFixedTime:
def test_allows_passing_as_int(self, page: Page) -> None:
page.clock.set_fixed_time(100)
assert page.evaluate("Date.now()") == 100
page.clock.set_fixed_time(int(200))
assert page.evaluate("Date.now()") == 200

def test_does_not_fake_methods(self, page: Page) -> None:
page.clock.set_fixed_time(0)
# Should not stall.
Expand Down

0 comments on commit 2bbad09

Please sign in to comment.