9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Cypress】時間制御のテスト方法

Last updated at Posted at 2023-07-25
package version
cypress 10.0.7
date-fns 2.29.3
cypress
cy.clock()
cy.tick()

やりたいこと

  • setTimeoutやsetInterval、Dateなど、Cypress上で時間経過が必要な動作のテスト
setTimeout
clearTimeout
setInterval
clearInterval
Date

javascript

実際のコード
// setIntervalで1秒ごとにHTMLを更新(タイマー)
let seconds = 0;

let secondsDom = document.getElementById("seconds-elapsed");
setInterval(() => {
  secondsDom.innerHTML = ++seconds + ' seconds';
}, 1000);

E2Eテスト

cypress
//オブジェクトを生成
cy.clock()

//対象のページにアクセス
cy.visit("/test.html")

//1000ms(1秒)時間を進める
cy.tick(1000)

//タイマーが1秒経過したことを確認
cy.get('#seconds-elapsed').should('have.text', '1 seconds')

//さらに1000ms(1秒)時間を進める
cy.tick(1000)

//タイマーが2秒経過したことを確認
cy.get('#seconds-elapsed').should('have.text', '2 seconds')

参照

clock | Cypress Documentation
https://docs.cypress.io/api/commands/clock

9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?