-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
119 lines (100 loc) · 2.69 KB
/
utils.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const fs = require('fs');
const path = require('path');
const moment = require('moment');
export const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
export const currentTimeInStr = (formattedString=undefined) => {
return moment().format(formattedString || 'YYYYMMDD_hhmmss.SSS');
};
export const currentTimeInMilliseconds = () => {
return moment().format('x');
}
const writeFile = (path, data, opts = 'utf8') =>
new Promise((resolve, reject) => {
fs.writeFile(path, data, opts, (err) => {
if (err) reject(err)
else resolve()
})
})
export const createDirectory = (folderPath) => {
return fs.existsSync(folderPath) || fs.mkdirSync(folderPath);
}
export const takeScreenshot = async (Page, name='scresnshot') => {
const ss = await Page.captureScreenshot({ format: 'png', fromSurface: true });
await writeFile(`${name}.png`, ss.data, 'base64');
};
export const fillElementWithValue = async (Runtime, elementID, value) => {
await Runtime.evaluate({
expression: `$('#${elementID}')[0].value = "${value}";`
});
};
export const waitUntilExpressionExists = async (Runtime, expression) => {
const limit = 120;
let counter = 0;
while (true) {
await sleep(1000);
let result = await Runtime.evaluate({ expression: expression });
if (
result != undefined &&
result.result != undefined &&
result.result.type != 'undefined' &&
result.result.subtype != undefined &&
result.result.subtype != "error"
) {
break;
}
counter += 1;
if (counter > limit) {
break;
}
}
return counter > limit;
};
export const waitUntilElementExists = async (Runtime, elementID) => {
const limit = 90;
let counter = 0;
while (true) {
await sleep(1000);
let result = await Runtime.evaluate({ expression: `$('#${elementID}')[0]` });
if (
result != undefined &&
result.result != undefined &&
result.result.subtype == "node"
) {
break;
}
counter += 1;
if (counter > limit) {
break;
}
}
return counter > limit;
};
export const runJavascript = async (Runtime, js) => {
let result = await Runtime.evaluate({ expression: js });
try {
return result.result.value;
} catch {
return undefined;
}
};
export const checkIfExpressionExists = async (Runtime, expression) => {
let result = await Runtime.evaluate({ expression: expression });
console.log(result)
console.log(result.result.type);
if(result.result.type == 'undefined'){
return false;
} else {
return true;
}
}
export default {
sleep,
currentTimeInStr,
takeScreenshot,
waitUntilElementExists,
runJavascript,
createDirectory,
checkIfExpressionExists,
};