forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
41 lines (40 loc) · 1.2 KB
/
common.js
File metadata and controls
41 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module.exports = {
/**
* Validate whether a selector is visible in viewport
*
* @param {Object} page Page object
* @param {String} selector Selector to validate
* @returns Boolean
*/
isIntersectingViewport: async (page, selector) => {
return page.$eval(selector, async el => {
const bottom = window.innerHeight
const rect = el.getBoundingClientRect()
return rect.top < bottom && rect.top > 0 - rect.height
})
},
/**
* Override page DateTime with a new value
*
* @param {Object} page Page object
* @param {Object} dateTimeOverride New DateTime object
*/
overridePageDateTime: async (page, dateTimeOverride) => {
await page.addInitScript(`{
// Extend Date constructor to default to fixed time
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(${dateTimeOverride.toMillis()});
} else {
super(...args);
}
}
}
// Override Date.now() to start from fixed time
const __DateNowOffset = ${dateTimeOverride.toMillis()} - Date.now();
const __DateNow = Date.now;
Date.now = () => __DateNow() + __DateNowOffset;
}`)
}
}