Can I set the date for playwright browser

When I write tests to run in playwright, I would like to be able to set the date that the browser believes it to be at the start of the test. Is there a way to achieve this using playwright?

21 thoughts on “Can I set the date for playwright browser”

  1. You can use mocking tools like TimeShift.
    This piece of code would inject the timeshift library and then then the date to Feb 02 2012:

    const playwright = require("playwright");
    (async () => {
        let removed = false;
        const browser = await playwright["chromium"].launch({headless: false});
        const page = await browser.newPage();
        await page.setContent('<a href="#" onclick="document.querySelector(\'span\').innerText = new Date().toString()">Test me</a><span></span>');
        console.log(await page.innerText('span'));
        await page.addScriptTag({ url: ''});
        await page.evaluate(() => {
            Date = TimeShift.Date; 
        console.log(await page.innerText('span'));


    Fri Sep 18 2020 15:06:58 GMT-0300 (Argentina Standard Time)
    Thu Feb 02 2012 22:02:03 GMT-0300

Leave a Comment