运行测试时如何设置赛普拉斯的其他时区?

问题描述 投票:0回答:2
cy.clock()
cy.visit('http://localhost:3333')
cy.get('#search').type('Acme Company')
cy.tick(1000)
// more test code here

// restore the clock
cy.clock().then((clock) => {
  clock.restore()
})

运行测试时如何在 Cypress 中设置时区? 我在越南,我的时区是 GMT+7 当我运行测试时,我希望我的测试在 GMT+8 时区运行。

timezone cypress
2个回答
0
投票

cy.clock()
命令与时区无关,它控制 javascript 日期设置,该设置内部只是自 1970 年 1 月 1 日以来的毫秒数。

参见日期

JavaScript Date 对象以独立于平台的格式表示时间上的单个时刻。日期对象封装了一个整数,表示自 UTC(纪元)1970 年 1 月 1 日午夜开始的毫秒数。


这里有一篇文章如何更改 Chrome 中的时区进行测试,展示了如何使用 Chrome 开发工具中的“传感器”选项卡手动设置时区:

enter image description here


要在自动化 Cypress 测试中执行此操作,请使用 Chrome 远程调试器协议,该协议最容易通过 cypress-cdp 插件使用

该网页仅显示当前时区的日期。我想用不同的时区来测试它。

<!DOCTYPE html>
<html>
<body>
  <div id="date"></div>
  <button onclick="setDate()">Set date</button>
  <script>
    function setDate() {
      const d = new Date();
      const dateField = document.getElementById("date")
      dateField.innerHTML = d.toString();
    }
  </script>
</body>
</html>

测试采用三个不同的时区,将浏览器设置为每个时区并验证页面上显示的日期字符串。

const timezones = {
  'Asia/Shanghai': 'GMT+0800 (China Standard Time)',
  'America/Sao_Paulo': 'GMT-0300 (Brasilia Standard Time)',
  'Europe/London': 'GMT+0100 (British Summer Time)'
}

Cypress._.each(timezones, function(offset, tz) { 

  cy.CDP('Emulation.setTimezoneOverride', {
    timezoneId: tz
  })

  cy.contains('button', 'Set date').click()

  cy.get('#date').should('contain', offset)
})

enter image description here


-3
投票

要设置现在的时间戳,您可以使用:

const now = new Date(Date.UTC(2021, 1, 26)).getTime();
cy.clock(now);

文档中的更多信息:cy.clock() - 现在

© www.soinside.com 2019 - 2024. All rights reserved.