@percy/playwright 1.0.6-beta.0 → 1.0.6-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache.js +51 -0
- package/package.json +4 -2
- package/utils.js +28 -0
package/cache.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
class Cache {
|
|
2
|
+
static CACHE = {};
|
|
3
|
+
static CACHE_TIMEOUT = 5 * 60; // 300 seconds
|
|
4
|
+
static TIMEOUT_KEY = 'last_access_time';
|
|
5
|
+
|
|
6
|
+
// Caching Keys
|
|
7
|
+
static sessionDetails = 'sessionDetails';
|
|
8
|
+
|
|
9
|
+
static checkTypes(sessionId, property) {
|
|
10
|
+
if (typeof sessionId !== 'string') {
|
|
11
|
+
throw new TypeError('Argument sessionId should be a string');
|
|
12
|
+
}
|
|
13
|
+
if (typeof property !== 'string') {
|
|
14
|
+
throw new TypeError('Argument property should be a string');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static setCache(sessionId, property, value) {
|
|
19
|
+
this.checkTypes(sessionId, property);
|
|
20
|
+
let session = this.CACHE[sessionId] || {};
|
|
21
|
+
session[this.TIMEOUT_KEY] = Math.floor(Date.now() / 1000);
|
|
22
|
+
session[property] = value;
|
|
23
|
+
this.CACHE[sessionId] = session;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static getCache(sessionId, property) {
|
|
27
|
+
this.cleanupCache();
|
|
28
|
+
this.checkTypes(sessionId, property);
|
|
29
|
+
/* Below line is covered even then nyc is not able to consider it as coverage */
|
|
30
|
+
/* istanbul ignore next */
|
|
31
|
+
let session = this.CACHE[sessionId] || {};
|
|
32
|
+
return session[property] || null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static cleanupCache() {
|
|
36
|
+
let now = Math.floor(Date.now() / 1000);
|
|
37
|
+
for (let sessionId in this.CACHE) {
|
|
38
|
+
let session = this.CACHE[sessionId];
|
|
39
|
+
let timestamp = session[this.TIMEOUT_KEY];
|
|
40
|
+
if (now - timestamp >= this.CACHE_TIMEOUT) {
|
|
41
|
+
this.CACHE[sessionId] = {
|
|
42
|
+
[this.sessionDetails]: session[this.sessionDetails]
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = {
|
|
50
|
+
Cache
|
|
51
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@percy/playwright",
|
|
3
3
|
"description": "Playwright client library for visual testing with Percy",
|
|
4
|
-
"version": "1.0.6-beta.
|
|
4
|
+
"version": "1.0.6-beta.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Perceptual Inc.",
|
|
7
7
|
"repository": "https://github.com/percy/percy-playwright",
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
"types": "types/index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"index.js",
|
|
17
|
+
"utils.js",
|
|
18
|
+
"cache.js",
|
|
17
19
|
"types/index.d.ts"
|
|
18
20
|
],
|
|
19
21
|
"engines": {
|
|
@@ -33,7 +35,7 @@
|
|
|
33
35
|
"playwright-core": ">=1"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@percy/cli": "^1.28.
|
|
38
|
+
"@percy/cli": "^1.28.8-beta.3",
|
|
37
39
|
"@playwright/test": "^1.24.2",
|
|
38
40
|
"babel-eslint": "^10.1.0",
|
|
39
41
|
"cross-env": "^7.0.2",
|
package/utils.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const utils = require('@percy/sdk-utils');
|
|
2
|
+
const { Cache } = require('./cache');
|
|
3
|
+
|
|
4
|
+
class Utils {
|
|
5
|
+
static projectType() {
|
|
6
|
+
return utils.percy?.type;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static async captureAutomateScreenshot(data) {
|
|
10
|
+
return await utils.captureAutomateScreenshot(data);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static async sessionDetails(page) {
|
|
14
|
+
/* It is browser's guid maintained by playwright, considering it is unique for one automate session
|
|
15
|
+
will use it to cache the session details */
|
|
16
|
+
const browserGuid = page._parent._parent._guid;
|
|
17
|
+
let sessionDetails = Cache.getCache(browserGuid, Cache.sessionDetails);
|
|
18
|
+
if (!sessionDetails) {
|
|
19
|
+
sessionDetails = JSON.parse(await page.evaluate(/* istanbul ignore next */ _ => { }, `browserstack_executor: ${JSON.stringify({ action: 'getSessionDetails' })}`));
|
|
20
|
+
Cache.setCache(browserGuid, Cache.sessionDetails, sessionDetails);
|
|
21
|
+
}
|
|
22
|
+
return sessionDetails;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
Utils
|
|
28
|
+
};
|