Skip to content

Commit

Permalink
follow-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jul 2, 2024
1 parent 2bbad09 commit 8b8140d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
4 changes: 2 additions & 2 deletions playwright/_impl/_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ async def set_system_time(


def parse_time(
time: Union[float, int, str, datetime.datetime]
time: Union[float, str, datetime.datetime]
) -> Dict[str, Union[int, str]]:
if isinstance(time, (float, int)):
return {"timeNumber": int(time)}
return {"timeNumber": int(time * 1_000)}
if isinstance(time, str):
return {"timeString": time}
return {"timeNumber": int(time.timestamp() * 1_000)}
Expand Down
6 changes: 6 additions & 0 deletions playwright/async_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6710,6 +6710,8 @@ async def fast_forward(self, ticks: typing.Union[int, str]) -> None:
Parameters
----------
ticks : Union[int, 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.
"""

return mapping.from_maybe_impl(await self._impl_obj.fast_forward(ticks=ticks))
Expand All @@ -6734,6 +6736,7 @@ async def pause_at(self, time: typing.Union[float, str, datetime.datetime]) -> N
Parameters
----------
time : Union[datetime.datetime, float, str]
Time to pause at.
"""

return mapping.from_maybe_impl(await self._impl_obj.pause_at(time=time))
Expand Down Expand Up @@ -6761,6 +6764,8 @@ async def run_for(self, ticks: typing.Union[int, str]) -> None:
Parameters
----------
ticks : Union[int, 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.
"""

return mapping.from_maybe_impl(await self._impl_obj.run_for(ticks=ticks))
Expand Down Expand Up @@ -6806,6 +6811,7 @@ async def set_system_time(
Parameters
----------
time : Union[datetime.datetime, float, str]
Time to be set.
"""

return mapping.from_maybe_impl(await self._impl_obj.set_system_time(time=time))
Expand Down
6 changes: 6 additions & 0 deletions playwright/sync_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6820,6 +6820,8 @@ def fast_forward(self, ticks: typing.Union[int, str]) -> None:
Parameters
----------
ticks : Union[int, 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.
"""

return mapping.from_maybe_impl(
Expand All @@ -6846,6 +6848,7 @@ def pause_at(self, time: typing.Union[float, str, datetime.datetime]) -> None:
Parameters
----------
time : Union[datetime.datetime, float, str]
Time to pause at.
"""

return mapping.from_maybe_impl(self._sync(self._impl_obj.pause_at(time=time)))
Expand Down Expand Up @@ -6873,6 +6876,8 @@ def run_for(self, ticks: typing.Union[int, str]) -> None:
Parameters
----------
ticks : Union[int, 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.
"""

return mapping.from_maybe_impl(self._sync(self._impl_obj.run_for(ticks=ticks)))
Expand Down Expand Up @@ -6918,6 +6923,7 @@ def set_system_time(
Parameters
----------
time : Union[datetime.datetime, float, str]
Time to be set.
"""

return mapping.from_maybe_impl(
Expand Down
2 changes: 0 additions & 2 deletions scripts/documentation_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ def inner_serialize_doc_type(self, type: Any, direction: str) -> str:
return "bool"
if type_name.lower() == "string":
return "str"
if type_name.lower() == "long":
return "float"
if type_name == "any" or type_name == "Serializable":
return "Any"
if type_name == "Object":
Expand Down
28 changes: 14 additions & 14 deletions tests/async/test_page_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class TestFastForward:
@pytest.fixture(autouse=True)
async def before_each(self, page: Page) -> AsyncGenerator[None, None]:
await page.clock.install(time=0)
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
yield

async def test_ignores_timers_which_wouldnt_be_run(
Expand Down Expand Up @@ -184,11 +184,11 @@ class TestStubTimers:
@pytest.fixture(autouse=True)
async def before_each(self, page: Page) -> AsyncGenerator[None, None]:
await page.clock.install(time=0)
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
yield

async def test_sets_initial_timestamp(self, page: Page) -> None:
await page.clock.set_system_time(1400)
await page.clock.set_system_time(1.4)
assert await page.evaluate("Date.now()") == 1400

async def test_replaces_global_setTimeout(
Expand Down Expand Up @@ -255,8 +255,8 @@ async def test_fakes_Date_constructor(self, page: Page) -> None:

class TestStubTimersPerformance:
async def test_replaces_global_performance_time_origin(self, page: Page) -> None:
await page.clock.install(time=1000)
await page.clock.pause_at(2000)
await page.clock.install(time=1)
await page.clock.pause_at(2)
promise = asyncio.create_task(
page.evaluate(
"""async () => {
Expand Down Expand Up @@ -332,7 +332,7 @@ async def test_should_not_run_time_before_popup_on_pause(
),
)
await page.clock.install(time=0)
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
await page.goto(server.EMPTY_PAGE)
# Wait for 2 second in real life to check that it is past in popup.
await page.wait_for_timeout(2000)
Expand All @@ -351,25 +351,25 @@ async def test_does_not_fake_methods(self, page: Page) -> None:
await page.evaluate("new Promise(f => setTimeout(f, 1))")

async def test_allows_setting_time_multiple_times(self, page: Page) -> None:
await page.clock.set_fixed_time(100)
await page.clock.set_fixed_time(0.1)
assert await page.evaluate("Date.now()") == 100
await page.clock.set_fixed_time(200)
await page.clock.set_fixed_time(0.2)
assert await page.evaluate("Date.now()") == 200

async def test_fixed_time_is_not_affected_by_clock_manipulation(
self, page: Page
) -> None:
await page.clock.set_fixed_time(100)
await page.clock.set_fixed_time(0.1)
assert await page.evaluate("Date.now()") == 100
await page.clock.fast_forward(20)
assert await page.evaluate("Date.now()") == 100

async def test_allows_installing_fake_timers_after_setting_time(
self, page: Page, calls: List[Any]
) -> None:
await page.clock.set_fixed_time(100)
await page.clock.set_fixed_time(0.1)
assert await page.evaluate("Date.now()") == 100
await page.clock.set_fixed_time(200)
await page.clock.set_fixed_time(0.2)
await page.evaluate("setTimeout(() => window.stub(Date.now()))")
await page.clock.run_for(0)
assert calls == [[200]]
Expand Down Expand Up @@ -407,7 +407,7 @@ async def test_should_fast_forward_to(self, page: Page) -> None:
async def test_should_pause(self, page: Page) -> None:
await page.clock.install(time=0)
await page.goto("data:text/html,")
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
await page.wait_for_timeout(1000)
await page.clock.resume()
now = await page.evaluate("Date.now()")
Expand All @@ -416,15 +416,15 @@ async def test_should_pause(self, page: Page) -> None:
async def test_should_pause_and_fast_forward(self, page: Page) -> None:
await page.clock.install(time=0)
await page.goto("data:text/html,")
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
await page.clock.fast_forward(1000)
now = await page.evaluate("Date.now()")
assert now == 2000

async def test_should_set_system_time_on_pause(self, page: Page) -> None:
await page.clock.install(time=0)
await page.goto("data:text/html,")
await page.clock.pause_at(1000)
await page.clock.pause_at(1)
now = await page.evaluate("Date.now()")
assert now == 1000

Expand Down
36 changes: 18 additions & 18 deletions tests/sync/test_page_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class TestFastForward:
@pytest.fixture(autouse=True)
def before_each(self, page: Page) -> Generator[None, None, None]:
page.clock.install(time=0)
page.clock.pause_at(1000)
page.clock.pause_at(1)
yield

def test_ignores_timers_which_wouldnt_be_run(
Expand Down Expand Up @@ -177,11 +177,11 @@ class TestStubTimers:
@pytest.fixture(autouse=True)
def before_each(self, page: Page) -> Generator[None, None, None]:
page.clock.install(time=0)
page.clock.pause_at(1000)
page.clock.pause_at(1)
yield

def test_sets_initial_timestamp(self, page: Page) -> None:
page.clock.set_system_time(1400)
page.clock.set_system_time(1.4)
assert page.evaluate("Date.now()") == 1400

def test_replaces_global_setTimeout(self, page: Page, calls: List[Any]) -> None:
Expand Down Expand Up @@ -239,8 +239,8 @@ def test_fakes_Date_constructor(self, page: Page) -> None:

class TestStubTimersPerformance:
def test_replaces_global_performance_time_origin(self, page: Page) -> None:
page.clock.install(time=1000)
page.clock.pause_at(2000)
page.clock.install(time=1)
page.clock.pause_at(2)
page.evaluate(
"""() => {
window.waitForPromise = new Promise(async resolve => {
Expand Down Expand Up @@ -312,7 +312,7 @@ def test_should_not_run_time_before_popup_on_pause(
),
)
page.clock.install(time=0)
page.clock.pause_at(1000)
page.clock.pause_at(1)
page.goto(server.EMPTY_PAGE)
# Wait for 2 second in real life to check that it is past in popup.
page.wait_for_timeout(2000)
Expand All @@ -325,34 +325,34 @@ 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
page.clock.set_fixed_time(1)
assert page.evaluate("Date.now()") == 1000
page.clock.set_fixed_time(int(2))
assert page.evaluate("Date.now()") == 2000

def test_does_not_fake_methods(self, page: Page) -> None:
page.clock.set_fixed_time(0)
# Should not stall.
page.evaluate("new Promise(f => setTimeout(f, 1))")

def test_allows_setting_time_multiple_times(self, page: Page) -> None:
page.clock.set_fixed_time(100)
page.clock.set_fixed_time(0.1)
assert page.evaluate("Date.now()") == 100
page.clock.set_fixed_time(200)
page.clock.set_fixed_time(0.2)
assert page.evaluate("Date.now()") == 200

def test_fixed_time_is_not_affected_by_clock_manipulation(self, page: Page) -> None:
page.clock.set_fixed_time(100)
page.clock.set_fixed_time(0.1)
assert page.evaluate("Date.now()") == 100
page.clock.fast_forward(20)
assert page.evaluate("Date.now()") == 100

def test_allows_installing_fake_timers_after_setting_time(
self, page: Page, calls: List[Any]
) -> None:
page.clock.set_fixed_time(100)
page.clock.set_fixed_time(0.1)
assert page.evaluate("Date.now()") == 100
page.clock.set_fixed_time(200)
page.clock.set_fixed_time(0.2)
page.evaluate("setTimeout(() => window.stub(Date.now()))")
page.clock.run_for(0)
assert calls == [[200]]
Expand Down Expand Up @@ -390,7 +390,7 @@ def test_should_fast_forward_to(self, page: Page) -> None:
def test_should_pause(self, page: Page) -> None:
page.clock.install(time=0)
page.goto("data:text/html,")
page.clock.pause_at(1000)
page.clock.pause_at(1)
page.wait_for_timeout(1000)
page.clock.resume()
now = page.evaluate("Date.now()")
Expand All @@ -399,15 +399,15 @@ def test_should_pause(self, page: Page) -> None:
def test_should_pause_and_fast_forward(self, page: Page) -> None:
page.clock.install(time=0)
page.goto("data:text/html,")
page.clock.pause_at(1000)
page.clock.pause_at(1)
page.clock.fast_forward(1000)
now = page.evaluate("Date.now()")
assert now == 2000

def test_should_set_system_time_on_pause(self, page: Page) -> None:
page.clock.install(time=0)
page.goto("data:text/html,")
page.clock.pause_at(1000)
page.clock.pause_at(1)
now = page.evaluate("Date.now()")
assert now == 1000

Expand Down

0 comments on commit 8b8140d

Please sign in to comment.