-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.ts
80 lines (76 loc) · 2.48 KB
/
task.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* eslint-disable camelcase */
import { createCypressEnvVariable, getSDKConfigFromFile } from './src/utils';
import { createSDK } from '@devtools-ai/js-sdk';
import 'dotenv/config';
import open from 'open';
import { sync } from 'fast-glob';
import { readFileSync, unlinkSync } from 'fs';
export const task = {
register(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) {
on('task', {
openClassifyTab: task.openClassifyTab,
readScreenshot: task.readScreenshot,
cleanupImages: task.cleanupImages,
});
// add some environment variables we care about
// config is passed by reference
if (config) {
config.env = config.env || {};
config.env.interactiveMode =
config.env.interactiveMode ||
process.env.interactiveMode?.toLocaleLowerCase() === 'true' ||
process.env.DEVTOOLSAI_INTERACTIVE?.toLocaleLowerCase() === 'true' ||
process.env.DEVTOOLSAI_INTERACTIVE?.toLocaleLowerCase() === '1';
}
const configFile = getSDKConfigFromFile();
Object.keys(configFile).forEach((configKey) => {
// save the config variable to cypress config
const smartDriverEnvName = createCypressEnvVariable(configKey);
config.env[smartDriverEnvName] = configFile[configKey];
});
},
createPingEvent(testCaseName: string) {
const config = this.getSDKConfig();
const client = createSDK(config);
client.createCheckIn(testCaseName);
return null;
},
async openClassifyTab(url: string) {
await open(url);
return null;
},
cleanupImages() {
return new Promise((resolve, reject) => {
const entries = sync('cypress/**/temp-devtools-*.png', {
onlyFiles: true,
});
for (const entry of entries) {
try {
unlinkSync(entry);
} catch (e) {
reject('Error deleting temp image' + entry);
}
}
resolve(entries);
});
},
readScreenshot(fileName: string) {
// use glob to try to find the file
// cypress/**/${fileName}.{png}
return new Promise((resolve, reject) => {
const entries = sync(`cypress/**/${fileName}*.png`, {
onlyFiles: true,
});
if (!entries.length) {
reject(`Couldn't find a screenshot with the file name: ${fileName}`);
}
const filePath = entries[0];
try {
const data = readFileSync(filePath, { encoding: 'base64' });
resolve(data);
} catch (e) {
reject(`Couldn't read the screenshot: ${filePath}`);
}
});
},
};