-
Notifications
You must be signed in to change notification settings - Fork 2
/
testTools.js
343 lines (306 loc) · 9.79 KB
/
testTools.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const {userName, password, backEndUrl} = require("./additionalConfig");
const fs = require('fs');
const puppeteer = require("puppeteer");
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function login(page) {
const userNameInput = await page.waitForXPath(
`//input[@id='userNameInput']`,
{ visible: true }
);
await page.$eval(`#userNameInput`, (element, value) => element.value = value, userName);
await sleep(200);
const passwordInput = await page.waitForXPath(`//input[@id='passInput']`, {
visible: true,
});
await page.$eval(`#passInput`, (element, value) => element.value = value, password);
await sleep(200);
const loginButton = await page.waitForXPath(`//a[@id='loginButton']`, {
visible: true,
});
await sleep(500); // give the translations some time to load
await loginButton.click();
}
async function afterEachTest(browser){
const pages = await browser.pages();
try{
await Promise.all(pages.map(async page => await page.close()));
}catch(e){
console.warn(e);
}
await sleep(200);
if(browser) {
await browser.close();
}
}
async function beforeEachTest(){
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
//devtools: true,
// slowMo: 50,
headless: false,
defaultViewport: {
width: 1800,
height: 2000, // to make all 30 lines visible and avoid the need for scrolling
},
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-sandbox",
]
});
const page = await browser.newPage();
// await installMouseHelper(page); // uncomment to see the mouse movement
await page.goto(backEndUrl);
await sleep(500);
await page.evaluate(() => {
localStorage.setItem("debugCloseAllForms", "1");
});
return [browser, page]
}
async function openMenuItem(page, menuItemIdList) {
for (const menuItemId of menuItemIdList) {
const menuItem = await page.waitForXPath(
`//*[@id='${menuItemId}']`,
{visible: true}
);
await sleep(100);
await menuItem.click();
}
}
async function getImage(page, element){
const elementBounds = await element.boundingBox();
return page.screenshot({
clip: elementBounds,
});
}
function xPathContainsClass(className){
return `contains(concat(' ',normalize-space(@class),' '),' ${className} ')`
}
async function waitForRowCount(page, dataViewId, expectedRowCount){
const modelInstanceId = dataViewId.substring(9);
const timeoutMs = 10_000;
const evalDelayMs = 50;
let tableData
for (let i = 0; i < timeoutMs / evalDelayMs; i++) {
tableData = await page.evaluate(
modelInstanceId => window.tableDebugMonitor && window.tableDebugMonitor[modelInstanceId],
modelInstanceId);
if(tableData && tableData.rendered && tableData.data.length === expectedRowCount){
await page.evaluate(() => window.tableDebugMonitor = undefined);
return tableData;
}
await sleep(evalDelayMs);
}
await page.evaluate(() => window.tableDebugMonitor = undefined);
const rowCount = tableData ? tableData.data.length : 0;
expect(rowCount).toBe(expectedRowCount);
}
async function takeScreenShot(page, name){
const screenDir = process.platform === "win32"
? "screenshots"
: "/home/origam/HTML5/screenshots";
if(!fs.existsSync(screenDir)){
fs.mkdirSync(screenDir)
}
const dateTime =new Date().toISOString()
.replace(/T/, ' ')
.replace(/Z/, '')
.replace(/\./, '_')
.replace(/:/, '_')
.replace(/:/, '_')
console.log("dateTime: "+ dateTime)
await page.screenshot({path: `${screenDir}/${name}_${dateTime}.png`});
}
async function waitForRowCountData(page, dataViewId, expectedRowCount) {
let countData;
for (let i = 0; i < 200; i++) {
countData = await getRowCountData(page, dataViewId)
if(countData.rowCount === expectedRowCount.toString()){
return countData;
}
await sleep(50);
}
await takeScreenShot(page, `Error expectedRowCount ${expectedRowCount} `);
throw new Error(`Row count did not change before timeout, expectedRowCount: ${expectedRowCount}, countData.rowCount: ${countData.rowCount}`);
}
async function getTableData(page, dataViewId){
const modelInstanceId = dataViewId.substring(9);
const timeoutMs = 10_000;
const evalDelayMs = 50;
let tableData
for (let i = 0; i < timeoutMs / evalDelayMs; i++) {
tableData = await page.evaluate(
modelInstanceId => window.tableDebugMonitor && window.tableDebugMonitor[modelInstanceId],
modelInstanceId);
if(tableData && tableData.rendered){
await page.evaluate(() => window.tableDebugMonitor = undefined);
return tableData;
}
await sleep(evalDelayMs);
}
}
async function getRowCountData(page, dataViewId) {
const rowCountElement = await page.waitForSelector(`#${dataViewId} .rowCount`,{visible: true});
let rowCountText = await page.evaluate(x => x.textContent, rowCountElement);
const rowCountData = rowCountText
.split("/")
.map(x => x.trim())
.filter(x=> x !== "");
return {
rowCount: rowCountData[1],
selectedRow: rowCountData[0]};
}
async function waitForRowSelected(page, dataViewId, rowNumber){
const timeoutMs = 10_000;
const testDelayMs = 50;
let rowCountData;
for (let i = 0; i < timeoutMs / testDelayMs ; i++) {
rowCountData = await getRowCountData(page, dataViewId);
if(rowCountData.selectedRow === rowNumber.toString()){
return rowCountData;
}
await sleep(testDelayMs);
}
expect(rowCountData && rowCountData.selectedRow).toBe(rowNumber.toString());
}
function catchRequests(page, reqs = 0) {
const started = () => (reqs = reqs + 1);
const ended = () => (reqs = reqs - 1);
page.on('request', started);
page.on('requestfailed', ended);
page.on('requestfinished', ended);
return async (timeout = 5000, success = false) => {
while (true) {
if (reqs < 1) break;
await new Promise((yay) => setTimeout(yay, 100));
if ((timeout = timeout - 100) < 0) {
throw new Error('Timeout');
}
}
page.off('request', started);
page.off('requestfailed', ended);
page.off('requestfinished', ended);
};
}
async function typeAndWaitForSelector(args){
await args.page.focus(`#${args.inputId}`)
await args.page.keyboard.type(args.value)
for (let i = 0; i < 3; i++) {
try{
return await args.page.waitForSelector(
args.selector,
{visible: true, timeout: 3000}
);
}catch(TimeoutError){
console.log(TimeoutError)
await sleep(30000);
await args.page.focus(`#${args.inputId}`)
await args.page.keyboard.type(args.value)
}
}
await sleep(30000);
throw Error(`${args.selector} did not appear before timeout`);
}
async function clickAndWaitForSelector(args){
try{
await args.clickable.click();
}catch(error){
console.log(error);
await sleep(200);
}
for (let i = 0; i < 3; i++) {
try{
return await args.page.waitForSelector(
args.selector,
{visible: true, timeout: 5000}
)
}catch(error){
if(error.name !== "TimeoutError"){
console.log(error);
}
try {
await args.clickable.click();
}catch(error){
console.log(error)
await args.page.evaluate(x => x.click(), args.clickable);
}
}
}
throw Error(`${args.selector} did not appear before timeout`);
}
async function clickAndWaitForXPath(args){
await args.clickable.click();
for (let i = 0; i < 3; i++) {
try{
return await args.page.waitForXPath(
args.xPath,
{ visible: true, timeout: 3000 }
);
}catch(error){
if(error.name !== "TimeoutError"){
console.log(error);
}
await args.clickable.click();
}
}
throw Error(`${args.xPath} did not appear before timeout`);
}
async function switchToFormPerspective(args){
const switchButton = await args.page.waitForSelector(
`.formPerspectiveButton`,
{visible: true});
await sleep(300);
await clickAndWaitForSelector({
page: args.page,
clickable: switchButton,
selector:`#editor_${args.aPropertyId}`
});
}
async function switchToTablePerspective(args){
const switchButton = await args.page.waitForSelector(
`.tablePerspectiveButton`,
{visible: true});
await sleep(300);
await clickAndWaitForSelector({
page: args.page,
clickable: switchButton,
selector:`.tablePerspectiveDirector`
});
}
async function inputByPressingKeys(args){
for (const key of args.value) {
await args.page.keyboard.press(key);
await sleep(100);
}
}
async function switchLanguageTo(args){
const languageLinkContainer = await args.page.waitForSelector(
"#languageLinkContainer",
{visible: true});
await sleep(300);
await args.page.evaluate((container, locale) =>
Array.from(container.children)
.filter(element=> element.tagName === "A" && element.attributes["value"].value === locale)
.forEach(element => element.click())
,languageLinkContainer, args.locale);
}
async function waitForFocus(args){
const element = await args.page.waitForSelector(
"#" + args.elementId,
{visible: true});
await sleep(300);
for (let i = 0; i < 10 ; i++) {
await args.page.evaluate(x => x.focus(), element);
await sleep(100);
const focusedElementId = await args.page.evaluate(x => document.activeElement.attributes["id"].nodeValue);
if(focusedElementId === args.elementId){
return;
}
}
throw new Error("Could not set focus to:" + args.elementId);
}
module.exports = {sleep, xPathContainsClass, getImage, openMenuItem, login, getRowCountData, waitForRowCountData,
getTableData, waitForRowCount, catchRequests, waitForRowSelected, clickAndWaitForXPath, clickAndWaitForSelector,
typeAndWaitForSelector, switchToFormPerspective, inputByPressingKeys, switchLanguageTo, waitForFocus,
switchToTablePerspective, afterEachTest, beforeEachTest};