Skip to content

feat(webdriverio): support fake timers#13252

Merged
christian-bromann merged 10 commits intomainfrom
cb/sinonjs
Aug 2, 2024
Merged

feat(webdriverio): support fake timers#13252
christian-bromann merged 10 commits intomainfrom
cb/sinonjs

Conversation

@christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Aug 1, 2024

Proposed changes

With this feature you can modify the browser system clock using the emulate command. It overrides native global functions related to time allowing them to be controlled synchronously via clock.tick() or the yielded clock object. This includes controlling:

  • setTimeout
  • clearTimeout
  • setInterval
  • clearInterval
  • Date Objects

The clock starts at the unix epoch (timestamp of 0). This means that when you instantiate new Date in your application, it will have a time of January 1st, 1970 if you don't pass any other options to the emulate command.

Example

When calling browser.emulate('clock', { ... }) it will immediately overwrite the global functions for the current page as well as all following pages, e.g.:

const clock = await browser.emulate('clock', { now: new Date(1989, 7, 4) })

console.log(await browser.execute(() => (new Date()).toString()))
// returns "Fri Aug 04 1989 00:00:00 GMT-0700 (Pacific Daylight Time)"

await browser.url('https://webdriverio')
console.log(await browser.execute(() => (new Date()).toString()))
// returns "Fri Aug 04 1989 00:00:00 GMT-0700 (Pacific Daylight Time)"

await clock.restore()

console.log(await browser.execute(() => (new Date()).toString()))
// returns "Thu Aug 01 2024 17:59:59 GMT-0700 (Pacific Daylight Time)"

await browser.url('http://guinea-pig.webdriver.io/pointer.html')
console.log(await browser.execute(() => (new Date()).toString()))
// returns "Thu Aug 01 2024 17:59:59 GMT-0700 (Pacific Daylight Time)"

Types of changes

  • Polish (an improvement for an existing feature)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (improvements to the project's docs)
  • Specification changes (updates to WebDriver command specifications)
  • Internal updates (everything related to internal scripts, governance documentation and CI files)

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Backport Request

//: # (The current main branch is the development branch for WebdriverIO v9. If your change should be released to the current major version of WebdriverIO (v8), please raise another PR with the same changes against the v8 branch.)

  • This change is solely for v9 and doesn't need to be back-ported
  • Back-ported PR at #XXXXX

Further comments

n/a

Reviewers: @webdriverio/project-committers

@christian-bromann christian-bromann marked this pull request as draft August 1, 2024 05:01
@christian-bromann christian-bromann marked this pull request as ready for review August 2, 2024 06:28
@christian-bromann christian-bromann merged commit a792109 into main Aug 2, 2024
@christian-bromann christian-bromann deleted the cb/sinonjs branch August 2, 2024 06:57
@christian-bromann christian-bromann added the PR: New Feature 🚀 PRs that contain new features label Aug 15, 2024
@christian-bromann christian-bromann added this to the v9 milestone Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 PRs that contain new features

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant