playwright-go/clock.go
Can Stand d2aa790e89
Some checks failed
Go / Lint (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, windows-latest) (push) Has been cancelled
Go / test-examples (push) Has been cancelled
Docs / Deploy docs (push) Has been cancelled
Verify Types / verify (push) Has been cancelled
Go / finish (push) Has been cancelled
chore: maintain StorageState type (#583)
2026-02-23 09:13:11 -08:00

112 lines
2.2 KiB
Go

package playwright
import (
"errors"
"time"
)
type clockImpl struct {
browserCtx *browserContextImpl
}
func newClock(bCtx *browserContextImpl) Clock {
return &clockImpl{
browserCtx: bCtx,
}
}
func (c *clockImpl) FastForward(ticks any) error {
params, err := parseTicks(ticks)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockFastForward", params)
return err
}
func (c *clockImpl) Install(options ...ClockInstallOptions) (err error) {
params := map[string]any{}
if len(options) == 1 {
if options[0].Time != nil {
params, err = parseTime(options[0].Time)
if err != nil {
return err
}
}
}
_, err = c.browserCtx.channel.Send("clockInstall", params)
return err
}
func (c *clockImpl) PauseAt(time any) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockPauseAt", params)
return err
}
func (c *clockImpl) Resume() error {
_, err := c.browserCtx.channel.Send("clockResume")
return err
}
func (c *clockImpl) RunFor(ticks any) error {
params, err := parseTicks(ticks)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockRunFor", params)
return err
}
func (c *clockImpl) SetFixedTime(time any) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockSetFixedTime", params)
return err
}
func (c *clockImpl) SetSystemTime(time any) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockSetSystemTime", params)
return err
}
func parseTime(t any) (map[string]any, error) {
switch v := t.(type) {
case int, int64:
return map[string]any{"timeNumber": v}, nil
case string:
return map[string]any{"timeString": v}, nil
case time.Time:
return map[string]any{"timeNumber": v.UnixMilli()}, nil
default:
return nil, errors.New("time should be one of: int, int64, string, time.Time")
}
}
func parseTicks(ticks any) (map[string]any, error) {
switch v := ticks.(type) {
case int, int64:
return map[string]any{"ticksNumber": v}, nil
case string:
return map[string]any{"ticksString": v}, nil
default:
return nil, errors.New("ticks should be one of: int, int64, string")
}
}