Interface Clock


  • public interface Clock
    Accurately simulating time-dependent behavior is essential for verifying the correctness of applications. Learn more about clock emulation.

    Note that clock is installed for the entire BrowserContext, so the time in all the pages and iframes is controlled by the same clock.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Clock.InstallOptions  
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void fastForward​(long ticks)
      Advance the clock by jumping forward in time.
      void fastForward​(String ticks)
      Advance the clock by jumping forward in time.
      default void install()
      Install fake implementations for the following time-related functions: Date setTimeout clearTimeout setInterval clearInterval requestAnimationFrame cancelAnimationFrame requestIdleCallback cancelIdleCallback performance
      void install​(Clock.InstallOptions options)
      Install fake implementations for the following time-related functions: Date setTimeout clearTimeout setInterval clearInterval requestAnimationFrame cancelAnimationFrame requestIdleCallback cancelIdleCallback performance
      void pauseAt​(long time)
      Advance the clock by jumping forward in time and pause the time.
      void pauseAt​(String time)
      Advance the clock by jumping forward in time and pause the time.
      void pauseAt​(Date time)
      Advance the clock by jumping forward in time and pause the time.
      void resume()
      Resumes timers.
      void runFor​(long ticks)
      Advance the clock, firing all the time-related callbacks.
      void runFor​(String ticks)
      Advance the clock, firing all the time-related callbacks.
      void setFixedTime​(long time)
      Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.
      void setFixedTime​(String time)
      Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.
      void setFixedTime​(Date time)
      Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.
      void setSystemTime​(long time)
      Sets current system time but does not trigger any timers.
      void setSystemTime​(String time)
      Sets current system time but does not trigger any timers.
      void setSystemTime​(Date time)
      Sets current system time but does not trigger any timers.
    • Method Detail

      • fastForward

        void fastForward​(long ticks)
        Advance the clock by jumping forward in time. Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it later, after given time.

        Usage

        
         page.clock().fastForward(1000);
         page.clock().fastForward("30:00");
         
        Parameters:
        ticks - Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
        Since:
        v1.45
      • fastForward

        void fastForward​(String ticks)
        Advance the clock by jumping forward in time. Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it later, after given time.

        Usage

        
         page.clock().fastForward(1000);
         page.clock().fastForward("30:00");
         
        Parameters:
        ticks - Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
        Since:
        v1.45
      • install

        default void install()
        Install fake implementations for the following time-related functions:
        • Date
        • setTimeout
        • clearTimeout
        • setInterval
        • clearInterval
        • requestAnimationFrame
        • cancelAnimationFrame
        • requestIdleCallback
        • cancelIdleCallback
        • performance

        Fake timers are used to manually control the flow of time in tests. They allow you to advance time, fire timers, and control the behavior of time-dependent functions. See Clock.runFor() and Clock.fastForward() for more information.

        Since:
        v1.45
      • install

        void install​(Clock.InstallOptions options)
        Install fake implementations for the following time-related functions:
        • Date
        • setTimeout
        • clearTimeout
        • setInterval
        • clearInterval
        • requestAnimationFrame
        • cancelAnimationFrame
        • requestIdleCallback
        • cancelIdleCallback
        • performance

        Fake timers are used to manually control the flow of time in tests. They allow you to advance time, fire timers, and control the behavior of time-dependent functions. See Clock.runFor() and Clock.fastForward() for more information.

        Since:
        v1.45
      • runFor

        void runFor​(long ticks)
        Advance the clock, firing all the time-related callbacks.

        Usage

        
         page.clock().runFor(1000);
         page.clock().runFor("30:00");
         
        Parameters:
        ticks - Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
        Since:
        v1.45
      • runFor

        void runFor​(String ticks)
        Advance the clock, firing all the time-related callbacks.

        Usage

        
         page.clock().runFor(1000);
         page.clock().runFor("30:00");
         
        Parameters:
        ticks - Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
        Since:
        v1.45
      • pauseAt

        void pauseAt​(long time)
        Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless Clock.runFor(), Clock.fastForward(), Clock.pauseAt() or Clock.resume() is called.

        Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it at the specified time and pausing.

        Usage

        
         SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
         page.clock().pauseAt(format.parse("2020-02-02"));
         page.clock().pauseAt("2020-02-02");
         
        Parameters:
        time - Time to pause at.
        Since:
        v1.45
      • pauseAt

        void pauseAt​(String time)
        Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless Clock.runFor(), Clock.fastForward(), Clock.pauseAt() or Clock.resume() is called.

        Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it at the specified time and pausing.

        Usage

        
         SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
         page.clock().pauseAt(format.parse("2020-02-02"));
         page.clock().pauseAt("2020-02-02");
         
        Parameters:
        time - Time to pause at.
        Since:
        v1.45
      • pauseAt

        void pauseAt​(Date time)
        Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless Clock.runFor(), Clock.fastForward(), Clock.pauseAt() or Clock.resume() is called.

        Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it at the specified time and pausing.

        Usage

        
         SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
         page.clock().pauseAt(format.parse("2020-02-02"));
         page.clock().pauseAt("2020-02-02");
         
        Parameters:
        time - Time to pause at.
        Since:
        v1.45
      • resume

        void resume()
        Resumes timers. Once this method is called, time resumes flowing, timers are fired as usual.
        Since:
        v1.45
      • setFixedTime

        void setFixedTime​(long time)
        Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.

        Usage

        
         page.clock().setFixedTime(new Date());
         page.clock().setFixedTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setFixedTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45
      • setFixedTime

        void setFixedTime​(String time)
        Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.

        Usage

        
         page.clock().setFixedTime(new Date());
         page.clock().setFixedTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setFixedTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45
      • setFixedTime

        void setFixedTime​(Date time)
        Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.

        Usage

        
         page.clock().setFixedTime(new Date());
         page.clock().setFixedTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setFixedTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45
      • setSystemTime

        void setSystemTime​(long time)
        Sets current system time but does not trigger any timers.

        Usage

        
         page.clock().setSystemTime(new Date());
         page.clock().setSystemTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setSystemTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45
      • setSystemTime

        void setSystemTime​(String time)
        Sets current system time but does not trigger any timers.

        Usage

        
         page.clock().setSystemTime(new Date());
         page.clock().setSystemTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setSystemTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45
      • setSystemTime

        void setSystemTime​(Date time)
        Sets current system time but does not trigger any timers.

        Usage

        
         page.clock().setSystemTime(new Date());
         page.clock().setSystemTime(new SimpleDateFormat("yyy-MM-dd").parse("2020-02-02"));
         page.clock().setSystemTime("2020-02-02");
         
        Parameters:
        time - Time to be set in milliseconds.
        Since:
        v1.45