-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigateToCustomChart.js
121 lines (100 loc) · 3.13 KB
/
navigateToCustomChart.js
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
119
120
121
const { getButtons } = require('./screenshots').OPTIONS.CHART;
module.exports = async ({
page,
element,
screenshot,
customChartName,
chosenScreenshot,
hoverIndex,
dateFrom,
dateTo,
}) => {
const stepsToReproduce = screenshot.customChart[customChartName];
if (!stepsToReproduce) {
return new Error(
`No custom ${customChartName} chart for chart: ${chosenScreenshot}`
);
}
const evaluteFunc = el => {
if (el.textContent) {
return el.textContent;
}
return el.nodeName;
};
let returnedElement;
for (item of stepsToReproduce) {
const buttons = await getButtons(element);
const [what, which, func, options = {}] = item;
const performOnElement = async () => {
const index = which instanceof Function ? which(hoverIndex) : which;
const series = buttons[what];
if (!series) {
throw new Error(`No series for: ${what}`);
}
if (series.length - 1 < index) {
throw new Error(
`Index ${index} is out of range! Max index: ${series.length - 1}`
);
}
console.log(`Series length: ${series.length}. Index: ${index}`);
const button = series[index];
let result;
// if arguments values are known
const funcArgs = options?.funcArgs || [];
if (button && !options?.value) {
result = await func(button, ...funcArgs);
}
// if arguments values are dynamic, at the moment only for date range
if (button && options?.value) {
type = { dateFrom, dateTo };
const value = type[options.value];
result = await func(button, value, page);
}
let text;
if (!options?.skipContent) {
text = await page.evaluate(evaluteFunc, result ?? button);
}
text && console.log(`Button "${text}" clicked`);
await page.waitForTimeout(500);
return result;
};
const preformOnArrayOfElements = async () => {
const series = buttons[what];
const index = which instanceof Function ? which(hoverIndex) : which;
console.log(`Loop: Series [${what}] length: ${series.length}.`);
options.dateFrom = dateFrom;
options.dateTo = dateTo;
let button;
if (series) {
const [button, buttonsSeries] = await func(
series,
index,
options,
page
);
console.log(
`Series [${options.selector}] length: ${buttonsSeries.length}. Index: ${index}`
);
if (!options?.skipContent) {
text = await page.evaluate(evaluteFunc, button);
}
text && console.log(`Button "${text}" clicked`);
}
return button;
};
const preformOnLineChart = async () => {
const index = which instanceof Function ? which(hoverIndex) : which;
const button = await func(index, options, page);
console.log(`Button [${button}] clicked!`);
};
const FuncDict = {
default: performOnElement,
loop: preformOnArrayOfElements,
line: preformOnLineChart,
};
const performFunc = options.type
? FuncDict[options.type]
: FuncDict.default;
returnedElement = await performFunc();
}
};