時計
はじめに
アプリケーションの正確性を検証するためには、時間に依存する動作を正確にシミュレートすることが不可欠です。Clock機能を利用することで、開発者はテスト内で時間を操作および制御でき、レンダリング時間、タイムアウト、スケジュールされたタスクなどの機能を、実際の実行による遅延や変動なしに正確に検証できます。
Clock API は、時間を制御するための以下のメソッドを提供します。
setFixedTime:Date.now()およびnew Date()の固定時間を設定します。install: クロックを初期化し、次のことを可能にします。pauseAt: 特定の時間で時間を一時停止します。fastForward: 時間を早送りします。runFor: 特定の期間だけ時間を実行します。resume: 時間を再開します。
setSystemTime: 現在のシステム時間を設定します。
推奨されるアプローチは、setFixedTime を使用して時間を特定の値に設定することです。この方法がユースケースに合わない場合は、install を使用できます。これにより、後で時間を一時停止したり、早送りしたり、刻んだりすることができます。setSystemTime は、高度なユースケースにのみ推奨されます。
page.clock は、時間に関連するネイティブのグローバルクラスと関数をオーバーライドし、手動で制御できるようにします。
DatesetTimeoutclearTimeoutsetIntervalclearIntervalrequestAnimationFramecancelAnimationFramerequestIdleCallbackcancelIdleCallbackperformanceEvent.timeStamp
テスト内の任意の時点で install を呼び出す場合、その呼び出しは、他のクロック関連の呼び出し (上記リストの注記を参照) の前に発生「する必要」があります。これらのメソッドを順不同で呼び出すと、未定義の動作が発生します。たとえば、setInterval を呼び出し、次に install を呼び出し、その後に clearInterval を呼び出すことはできません。これは、install がクロック関数のネイティブ定義をオーバーライドするためです。
事前定義された時間でのテスト
多くの場合、タイマーを動かしながら Date.now を偽装するだけで済みます。そうすれば時間は自然に流れますが、Date.now は常に固定値を返します。
<div id="current-time" data-testid="current-time"></div>
<script>
const renderTime = () => {
document.getElementById('current-time').textContent =
new Date().toLocaleString();
};
setInterval(renderTime, 1000);
</script>
- 同期
- 非同期
page.clock.set_fixed_time(datetime.datetime(2024, 2, 2, 10, 0, 0))
page.goto("https://:3333")
expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:00:00 AM")
page.clock.set_fixed_time(datetime.datetime(2024, 2, 2, 10, 30, 0))
# We know that the page has a timer that updates the time every second.
expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:30:00 AM")
await page.clock.set_fixed_time(datetime.datetime(2024, 2, 2, 10, 0, 0))
await page.goto("https://:3333")
await expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:00:00 AM")
await page.clock.set_fixed_time(datetime.datetime(2024, 2, 2, 10, 30, 0))
# We know that the page has a timer that updates the time every second.
await expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:30:00 AM")
一貫した時間とタイマー
タイマーが Date.now に依存しており、Date.now の値が時間とともに変化しない場合に混乱することがあります。この場合、クロックをインストールし、テスト時に目的の時間まで早送りすることができます。
<div id="current-time" data-testid="current-time"></div>
<script>
const renderTime = () => {
document.getElementById('current-time').textContent =
new Date().toLocaleString();
};
setInterval(renderTime, 1000);
</script>
- 同期
- 非同期
# 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(time=datetime.datetime(2024, 2, 2, 8, 0, 0))
page.goto("https://:3333")
# Pretend that the user closed the laptop lid and opened it again at 10am.
# Pause the time once reached that point.
page.clock.pause_at(datetime.datetime(2024, 2, 2, 10, 0, 0))
# Assert the page state.
expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:00:00 AM")
# Close the laptop lid again and open it at 10:30am.
page.clock.fast_forward("30:00")
expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:30:00 AM")
# Initialize clock with some time before the test time and let the page load
# naturally. `Date.now` will progress as the timers fire.
await page.clock.install(time=datetime.datetime(2024, 2, 2, 8, 0, 0))
await page.goto("https://:3333")
# Pretend that the user closed the laptop lid and opened it again at 10am.
# Pause the time once reached that point.
await page.clock.pause_at(datetime.datetime(2024, 2, 2, 10, 0, 0))
# Assert the page state.
await expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:00:00 AM")
# Close the laptop lid again and open it at 10:30am.
await page.clock.fast_forward("30:00")
await expect(page.get_by_test_id("current-time")).to_have_text("2/2/2024, 10:30:00 AM")
非アクティビティ監視のテスト
非アクティビティ監視は、一定期間の非アクティビティ後にユーザーをログアウトするウェブアプリケーションの一般的な機能です。この機能のテストは、効果を確認するために長い時間待つ必要があるため、難しい場合があります。クロックの助けを借りて、時間を加速し、この機能を迅速にテストできます。
<div id="remaining-time" data-testid="remaining-time"></div>
<script>
const endTime = Date.now() + 5 * 60_000;
const renderTime = () => {
const diffInSeconds = Math.round((endTime - Date.now()) / 1000);
if (diffInSeconds <= 0) {
document.getElementById('remaining-time').textContent =
'You have been logged out due to inactivity.';
} else {
document.getElementById('remaining-time').textContent =
`You will be logged out in ${diffInSeconds} seconds.`;
}
setTimeout(renderTime, 1000);
};
renderTime();
</script>
<button type="button">Interaction</button>
- 同期
- 非同期
# Initial time does not matter for the test, so we can pick current time.
page.clock.install()
page.goto("https://:3333")
# Interact with the page
page.get_by_role("button").click()
# Fast forward time 5 minutes as if the user did not do anything.
# Fast forward is like closing the laptop lid and opening it after 5 minutes.
# All the timers due will fire once immediately, as in the real browser.
page.clock.fast_forward("05:00")
# Check that the user was logged out automatically.
expect(page.get_by_text("You have been logged out due to inactivity.")).to_be_visible()
# Initial time does not matter for the test, so we can pick current time.
await page.clock.install()
await page.goto("https://:3333")
# Interact with the page
await page.get_by_role("button").click()
# Fast forward time 5 minutes as if the user did not do anything.
# Fast forward is like closing the laptop lid and opening it after 5 minutes.
# All the timers due will fire once immediately, as in the real browser.
await page.clock.fast_forward("05:00")
# Check that the user was logged out automatically.
await expect(page.getByText("You have been logged out due to inactivity.")).toBeVisible()
手動で時間を刻み、すべてのタイマーを一貫して起動する
まれに、時間の経過を細かく制御するために、すべてのタイマーとアニメーションフレームを起動しながら、手動で時間を刻みたい場合があります。
<div id="current-time" data-testid="current-time"></div>
<script>
const renderTime = () => {
document.getElementById('current-time').textContent =
new Date().toLocaleString();
};
setInterval(renderTime, 1000);
</script>
- 同期
- 非同期
# Initialize clock with a specific time, let the page load naturally.
page.clock.install(
time=datetime.datetime(2024, 2, 2, 8, 0, 0, tzinfo=datetime.timezone.pst),
)
page.goto("https://:3333")
locator = page.get_by_test_id("current-time")
# Pause the time flow, stop the timers, you now have manual control
# over the page time.
page.clock.pause_at(datetime.datetime(2024, 2, 2, 10, 0, 0))
expect(locator).to_have_text("2/2/2024, 10:00:00 AM")
# Tick through time manually, firing all timers in the process.
# In this case, time will be updated in the screen 2 times.
page.clock.run_for(2000)
expect(locator).to_have_text("2/2/2024, 10:00:02 AM")
# Initialize clock with a specific time, let the page load naturally.
await page.clock.install(time=
datetime.datetime(2024, 2, 2, 8, 0, 0, tzinfo=datetime.timezone.pst),
)
await page.goto("https://:3333")
locator = page.get_by_test_id("current-time")
# Pause the time flow, stop the timers, you now have manual control
# over the page time.
await page.clock.pause_at(datetime.datetime(2024, 2, 2, 10, 0, 0))
await expect(locator).to_have_text("2/2/2024, 10:00:00 AM")
# Tick through time manually, firing all timers in the process.
# In this case, time will be updated in the screen 2 times.
await page.clock.run_for(2000)
await expect(locator).to_have_text("2/2/2024, 10:00:02 AM")