-
Notifications
You must be signed in to change notification settings - Fork 856
/
Copy pathhelper.ts
148 lines (132 loc) · 5.15 KB
/
helper.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import type { Locator, Page } from "@playwright/test";
import { expect } from "@playwright/test";
const environment = process.env.env;
export const frameworks = environment
? [environment]
: ["angular", "react", "vue3"/*, "js-ui"*/];
// eslint-disable-next-line no-console
//console.log("Frameworks: " + frameworks.join(", "));
export const url = "http://127.0.0.1:8080/examples_test/default/";
export const urlV2 = "http://127.0.0.1:8080/examples_test/default/";
export const url_test = "http://127.0.0.1:8080/examples_test/";
export const FLOAT_PRECISION = 0.01;
export const applyTheme = async (page: Page, theme: string) => {
await page.evaluate((theme) => {
// window["Survey"].StylesManager.applyTheme(theme);
}, theme);
};
export const initSurvey = async (page: Page, framework: string, json: any, isDesignMode?: boolean, props?: any) => {
await page.evaluate(([framework, json, isDesignMode, props]) => {
// eslint-disable-next-line no-console
console.error = (msg) => {
throw new Error(msg);
};
// eslint-disable-next-line no-console
console.warn = (msg) => {
throw new Error(msg);
};
// eslint-disable-next-line no-console
console.log("surveyjs console.error and console.warn override");
//!!!TODO!!!
//window["Survey"].settings.animationEnabled = false;
const self: any = window;
const model = new window["Survey"].Model(json);
model.setDesignMode(isDesignMode);
const surveyComplete = function (model) {
window["SurveyResult"] = model.data;
document.getElementById("surveyResultElement").innerHTML = JSON.stringify(
model.data
);
};
if (!!props) {
for (var key in props) {
model[key] = props[key];
}
}
model.onComplete.add(surveyComplete);
const surveyElement: HTMLElement = document.getElementById("surveyElement") as HTMLElement;
if (framework === "js-ui") {
surveyElement.innerHTML = "";
self.SurveyUI.renderSurvey(model, surveyElement);
} else if (framework === "react") {
if (!!self.root) {
self.root.unmount();
}
const root = window["ReactDOMClient"].createRoot(document.getElementById("surveyElement"));
window["root"] = root;
root.render(
self.React.createElement(self.React.StrictMode, { children: self.React.createElement(self.SurveyReact.Survey, { model: model, onComplete: surveyComplete }) }),
);
} else if (framework === "angular" || framework == "vue3") {
self.window.setSurvey(model);
}
// if (framework === "react") {
// if (!!window.root) {
// window.root.unmount();
// }
// const root = window["ReactDOM"].createRoot(document.getElementById("surveyElement"));
// window["root"] = root;
// root.render(
// React.createElement(React.StrictMode, { children: React.createElement(SurveyReact.Survey, { model: model, onComplete: surveyComplete }) }),
// );
// }
window["survey"] = model;
}, [framework, json, isDesignMode, props]);
};
export async function checkSurveyData(page: Page, json: any): Promise<void> {
const data = await page.evaluate(() => { return window["survey"].data; });
await expect(data).toStrictEqual(json);
}
export async function getSurveyResult(page) {
return await page.evaluate(() => {
return window["SurveyResult"];
});
}
export async function getQuestionValue(page) {
return await page.evaluate(() => {
return window["survey"].getAllQuestions()[0].value;
});
}
export async function getQuestionJson(page) {
return await page.evaluate(() => {
return JSON.stringify(window["survey"].getAllQuestions()[0].toJSON());
});
}
export async function checkSurveyWithEmptyQuestion(page) {
const requiredMessage = page.locator(".sv-string-viewer").getByText("Response required.");
await expect(requiredMessage).toHaveCount(0);
await page.locator("input[value=Complete]").click();
await expect(requiredMessage).toHaveCount(1);
const surveyResult = await getSurveyResult(page);
expect(surveyResult).toEqual(undefined);
}
export async function getData(page) {
return await page.evaluate(() => {
return window["survey"].data;
});
}
export async function setRowItemFlowDirection(page) {
await page.evaluate(() => {
window["Survey"].settings.itemFlowDirection = "row";
});
}
export async function visibleInViewport (page, locator: Locator) {
const rect = await locator.boundingBox();
return await page.evaluate((rect) => {
return (
rect?.y >= 0 &&
rect?.x >= 0 &&
rect?.y + rect?.height <= (window.innerHeight || document.documentElement.clientHeight) &&
rect?.x + rect?.width <= (window.innerWidth || document.documentElement.clientWidth)
);
}, rect);
}
export async function expectHaveClasses(locator: Locator, className: string) {
// get current classes of element
const attrClass = await locator.getAttribute("class");
const elementClasses: string[] = attrClass ? attrClass.split(" ") : [];
const targetClasses: string[] = className.split(" ");
// Every class should be present in the current class list
const isValid = targetClasses.every(classItem => elementClasses.indexOf(classItem) > -1);
return isValid;
}