@percy/playwright 1.0.6-beta.0 → 1.0.6-beta.2

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.
Files changed (4) hide show
  1. package/cache.js +51 -0
  2. package/index.js +9 -5
  3. package/package.json +4 -2
  4. 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/index.js CHANGED
@@ -9,7 +9,7 @@ const ENV_INFO = `${playwrightPkg.name}/${playwrightPkg.version}`;
9
9
  const log = utils.logger('playwright');
10
10
 
11
11
  // Take a DOM snapshot and post it to the snapshot endpoint
12
- async function percySnapshot(page, name, options) {
12
+ const percySnapshot = async function(page, name, options) {
13
13
  if (!page) throw new Error('A Playwright `page` object is required.');
14
14
  if (!name) throw new Error('The `name` argument is required.');
15
15
  if (!(await utils.isPercyEnabled())) return;
@@ -39,10 +39,10 @@ async function percySnapshot(page, name, options) {
39
39
  log.error(`Could not take DOM snapshot "${name}"`);
40
40
  log.error(err);
41
41
  }
42
- }
42
+ };
43
43
 
44
44
  // Takes Playwright screenshot with Automate
45
- async function percyScreenshot(page, name, options) {
45
+ const percyScreenshot = async function(page, name, options) {
46
46
  if (!page) throw new Error('A Playwright `page` object is required.');
47
47
  if (!name) throw new Error('The `name` argument is required.');
48
48
  if (!(await utils.isPercyEnabled())) return;
@@ -71,6 +71,10 @@ async function percyScreenshot(page, name, options) {
71
71
  log.error(`Could not take percy screenshot "${name}"`);
72
72
  log.error(err);
73
73
  }
74
- }
74
+ };
75
75
 
76
- module.exports = { percySnapshot, percyScreenshot, CLIENT_INFO, ENV_INFO };
76
+ module.exports = percySnapshot;
77
+ module.exports.percySnapshot = percySnapshot;
78
+ module.exports.percyScreenshot = percyScreenshot;
79
+ module.exports.CLIENT_INFO = CLIENT_INFO;
80
+ module.exports.ENV_INFO = ENV_INFO;
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.0",
4
+ "version": "1.0.6-beta.2",
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.2",
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
+ };