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?
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?
Way easier solution with
page.evaluate(() => {
Date.now = () => {
return 1609477200000; //Jan 1, 2021
};
});
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>');
await page.click('a');
console.log(await page.innerText('span'));
await page.addScriptTag({ url: 'https://cdn.jsdelivr.net/npm/timeshift-js@1.1.1/timeshift.js'});
await page.evaluate(() => {
Date = TimeShift.Date;
TimeShift.setTime(1328230923000);
});
await page.click('a');
console.log(await page.innerText('span'));
browser.close();
})();
Output:
Fri Sep 18 2020 15:06:58 GMT-0300 (Argentina Standard Time)
Thu Feb 02 2012 22:02:03 GMT-0300
Version without tick functionality:
page.on('load', () => {
page.evaluate(() => {
const OriginalDate = window.Date;
class MockDate extends OriginalDate {
static currentDate = '18 May 2018 12:00 UTC';
static currentTimeStamp = (new OriginalDate(MockDate.currentDate)).getTime();
constructor(...args) {
const params = (args && args.length) ? args : [MockDate.currentTimeStamp];
super(...params);
}
static [Symbol.hasInstance](instance: Date) {
return typeof instance.getDate === 'function';
}
static now() {
return MockDate.currentTimeStamp;
}
}
window.Date = MockDate;
});
});
await page.goto(yourUrl);
Version with tick:
page.on('load', () => {
page.evaluate(() => {
const OriginalDate = window.Date;
class MockDate extends OriginalDate {
static currentDate = '18 May 2018 12:00 UTC';
static currentTimeStamp = (new OriginalDate(MockDate.currentDate)).getTime();
static originalNow = OriginalDate.now();
constructor(...args) {
const params = (args && args.length) ? args : [(MockDate.currentTimeStamp + MockDate.getTick())];
super(...params);
}
static [Symbol.hasInstance](instance: Date) {
return typeof instance.getDate === 'function';
}
static getTick() {
return OriginalDate.now() - MockDate.originalNow;
}
static now() {
return MockDate.currentTimeStamp + MockDate.getTick();
}
}
window.Date = MockDate;
});
});
await page.goto(yourUrl);