/
data_grid.ts
506 lines (433 loc) · 16.3 KB
/
data_grid.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { chunk } from 'lodash';
import { Key } from 'selenium-webdriver';
import { WebElementWrapper } from '@kbn/ftr-common-functional-ui-services';
import { FtrService } from '../ftr_provider_context';
export interface TabbedGridData {
columns: string[];
rows: string[][];
}
interface SelectOptions {
isAnchorRow?: boolean;
rowIndex?: number;
columnIndex?: number;
renderMoreRows?: boolean;
}
export class DataGridService extends FtrService {
private readonly find = this.ctx.getService('find');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly header = this.ctx.getPageObject('header');
private readonly retry = this.ctx.getService('retry');
async getDataGridTableData(): Promise<TabbedGridData> {
const table = await this.find.byCssSelector('.euiDataGrid');
const $ = await table.parseDomContent();
const columns = $('.euiDataGridHeaderCell__content')
.toArray()
.map((cell) => $(cell).text());
const cells = $.findTestSubjects('dataGridRowCell')
.toArray()
.map((cell) => $(cell).text());
const rows = chunk(cells, columns.length);
return {
columns,
rows,
};
}
/**
* Converts the data grid data into nested array
* [ [cell1_in_row1, cell2_in_row1], [cell1_in_row2, cell2_in_row2] ]
* @param element table
*/
public async getDataFromElement(
element: WebElementWrapper,
cellDataTestSubj: string
): Promise<string[][]> {
const $ = await element.parseDomContent();
const columnNumber = $('.euiDataGridHeaderCell__content').length;
const cells = $.findTestSubjects('dataGridRowCell')
.toArray()
.map((cell) =>
$(cell)
.findTestSubject(cellDataTestSubj)
.text()
.replace(/ /g, '')
.trim()
);
return chunk(cells, columnNumber);
}
/**
* Returns an array of data grid headers names
*/
public async getHeaders() {
const header = await this.testSubjects.find('euiDataGridBody > dataGridHeader');
const $ = await header.parseDomContent();
return $('.euiDataGridHeaderCell__content')
.toArray()
.map((cell) => $(cell).text());
}
private getCellElementSelector(rowIndex: number = 0, columnIndex: number = 0) {
return `[data-test-subj="euiDataGridBody"] [data-test-subj="dataGridRowCell"][data-gridcell-column-index="${columnIndex}"][data-gridcell-row-index="${rowIndex}"]`;
}
/**
* Returns a grid cell element by row & column indexes.
* @param rowIndex data row index starting from 0 (0 means 1st row)
* @param columnIndex column index starting from 0 (0 means 1st column)
*/
public async getCellElement(rowIndex: number = 0, columnIndex: number = 0) {
return await this.find.byCssSelector(this.getCellElementSelector(rowIndex, columnIndex));
}
private async getCellActionButton(
rowIndex: number = 0,
columnIndex: number = 0,
selector: string
): Promise<WebElementWrapper> {
let actionButton: WebElementWrapper | undefined;
await this.retry.try(async () => {
const cell = await this.getCellElement(rowIndex, columnIndex);
await cell.click();
actionButton = await cell.findByTestSubject(selector);
if (!actionButton) {
throw new Error(`Unable to find cell action button ${selector}`);
}
});
return actionButton!;
}
/**
* Clicks grid cell 'expand' action button
* @param rowIndex data row index starting from 0 (0 means 1st row)
* @param columnIndex column index starting from 0 (0 means 1st column)
*/
public async clickCellExpandButton(rowIndex: number = 0, columnIndex: number = 0) {
const actionButton = await this.getCellActionButton(
rowIndex,
columnIndex,
'euiDataGridCellExpandButton'
);
await actionButton.click();
}
/**
* Clicks grid cell 'filter for' action button
* @param rowIndex data row index starting from 0 (0 means 1st row)
* @param columnIndex column index starting from 0 (0 means 1st column)
*/
public async clickCellFilterForButton(rowIndex: number = 0, columnIndex: number = 0) {
const actionButton = await this.getCellActionButton(rowIndex, columnIndex, 'filterForButton');
await actionButton.click();
}
/**
* The same as getCellElement, but useful when multiple data grids are on the page.
*/
public async getAllCellElements(rowIndex: number = 0, columnIndex: number = 0) {
return await this.find.allByCssSelector(this.getCellElementSelector(rowIndex, columnIndex));
}
public async getDocCount(): Promise<number> {
const grid = await this.find.byCssSelector('[data-document-number]');
return Number(await grid.getAttribute('data-document-number'));
}
public async getFields(options?: SelectOptions) {
const selector = options?.isAnchorRow
? '.euiDataGridRowCell.dscDocsGrid__cell--highlight'
: '.euiDataGridRowCell';
const cells = await this.find.allByCssSelector(selector);
const rows: string[][] = [];
let rowIdx = -1;
for (const cell of cells) {
if (await cell.elementHasClass('euiDataGridRowCell--firstColumn')) {
// first column contains expand icon
rowIdx++;
rows[rowIdx] = [];
}
if (!(await cell.elementHasClass('euiDataGridRowCell--controlColumn'))) {
rows[rowIdx].push(await cell.getVisibleText());
}
}
return rows;
}
public async getTable(selector: string = 'docTable') {
return await this.testSubjects.find(selector);
}
public async getBodyRows(options?: SelectOptions): Promise<WebElementWrapper[][]> {
return this.getDocTableRows(options);
}
public async getRowsText(renderMoreRows?: boolean) {
// open full screen mode
if (renderMoreRows) {
await this.testSubjects.click('dataGridFullScreenButton');
}
const table = await this.getTable('docTable');
const rows = await table.findAllByCssSelector('.euiDataGridRow');
const rowsText = [];
for (const row of rows) {
rowsText.push((await row.getVisibleText()).replace(/[\r\n]/gm, ''));
}
// close full screen mode
if (renderMoreRows) {
await this.testSubjects.click('dataGridFullScreenButton');
}
return rowsText;
}
/**
* Returns an array of rows (which are array of cells)
*/
public async getDocTableRows(options?: SelectOptions) {
// open full screen mode
if (options?.renderMoreRows) {
await this.testSubjects.click('dataGridFullScreenButton');
}
const table = await this.getTable('docTable');
if (!table) {
return [];
}
const selector = options?.isAnchorRow
? '.euiDataGridRowCell.dscDocsGrid__cell--highlight'
: '.euiDataGridRowCell';
const cells = await table.findAllByCssSelector(selector);
const rows: WebElementWrapper[][] = [];
let rowIdx = -1;
for (const cell of cells) {
if (await cell.elementHasClass('euiDataGridRowCell--firstColumn')) {
rowIdx++;
rows[rowIdx] = [];
}
rows[rowIdx].push(cell);
}
// close full screen mode
if (options?.renderMoreRows) {
await this.testSubjects.click('dataGridFullScreenButton');
}
return rows;
}
/**
* Returns an array of cells for that row
*/
public async getRow(options: SelectOptions): Promise<WebElementWrapper[]> {
return (await this.getBodyRows(options))[options.rowIndex || 0];
}
public async clickRowToggle(
options: SelectOptions = { isAnchorRow: false, rowIndex: 0, columnIndex: 0 }
): Promise<void> {
const rowColumns = await this.getRow(options);
const testSubj = options.isAnchorRow
? '~docTableExpandToggleColumnAnchor'
: '~docTableExpandToggleColumn';
const toggle = await rowColumns[options.columnIndex ?? 0].findByTestSubject(testSubj);
await toggle.scrollIntoViewIfNecessary();
await toggle.click();
}
public async getDetailsRows(): Promise<WebElementWrapper[]> {
return await this.testSubjects.findAll('docTableDetailsFlyout');
}
public async closeFlyout() {
await this.testSubjects.click('euiFlyoutCloseButton');
}
public async getHeaderFields(): Promise<string[]> {
const result = await this.find.allByCssSelector(
'.euiDataGridHeaderCell__button > .euiDataGridHeaderCell__content'
);
const textArr = [];
for (const cell of result) {
const cellText = await cell.getVisibleText();
textArr.push(cellText.trim());
}
return textArr;
}
public async getControlColumnHeaderFields(): Promise<string[]> {
const result = await this.find.allByCssSelector(
'.euiDataGridHeaderCell--controlColumn > .euiDataGridHeaderCell__content'
);
const textArr = [];
for (const cell of result) {
const cellText = await cell.getVisibleText();
textArr.push(cellText.trim());
}
return textArr;
}
public async getRowActions(
options: SelectOptions = { isAnchorRow: false, rowIndex: 0 }
): Promise<WebElementWrapper[]> {
const detailsRow = (await this.getDetailsRows())[options.rowIndex || 0];
return await detailsRow.findAllByTestSubject('~docTableRowAction');
}
public async getAnchorDetailsRow(): Promise<WebElementWrapper> {
const table = await this.getTable();
return await table.findByCssSelector(
'[data-test-subj~="docTableAnchorRow"] + [data-test-subj~="docTableDetailsRow"]'
);
}
public async openColMenuByField(field: string) {
await this.retry.waitFor('header cell action being displayed', async () => {
// to prevent flakiness
await this.testSubjects.click(`dataGridHeaderCell-${field}`);
return await this.testSubjects.exists(`dataGridHeaderCellActionGroup-${field}`);
});
}
private async clickColumnMenuField(field?: string) {
if (field) {
await this.openColMenuByField(field);
} else {
await this.find.clickByCssSelector('.euiDataGridHeaderCell__button');
}
}
public async clickDocSortAsc(field?: string, sortText = 'Sort Old-New') {
await this.clickColumnMenuField(field);
await this.find.clickByButtonText(sortText);
}
public async clickDocSortDesc(field?: string, sortText = 'Sort New-Old') {
await this.clickColumnMenuField(field);
await this.find.clickByButtonText(sortText);
}
public async clickMoveColumnRight(field?: string) {
await this.clickColumnMenuField(field);
await this.find.clickByButtonText('Move right');
}
public async clickMoveColumnLeft(field?: string) {
await this.clickColumnMenuField(field);
await this.find.clickByButtonText('Move left');
}
public async clickRemoveColumn(field?: string) {
await this.clickColumnMenuField(field);
await this.find.clickByButtonText('Remove column');
}
public async clickCopyColumnValues(field: string) {
await this.openColMenuByField(field);
await this.find.clickByButtonText('Copy column');
}
public async clickCopyColumnName(field: string) {
await this.openColMenuByField(field);
await this.find.clickByButtonText('Copy name');
}
public async clickEditField(field: string) {
await this.openColMenuByField(field);
await this.testSubjects.click('gridEditFieldButton');
}
public async clickGridSettings() {
await this.testSubjects.click('dataGridDisplaySelectorButton');
}
public async getCurrentRowHeightValue() {
const buttonGroup = await this.testSubjects.find(
'unifiedDataTableRowHeightSettings_rowHeightButtonGroup'
);
let value = '';
await this.retry.waitFor('row height value not to be empty', async () => {
// to prevent flakiness
const selectedButton = await buttonGroup.findByCssSelector(
'.euiButtonGroupButton-isSelected'
);
value = await selectedButton.getVisibleText();
return value !== '';
});
return value;
}
public async changeRowHeightValue(newValue: string) {
const buttonGroup = await this.testSubjects.find(
'unifiedDataTableRowHeightSettings_rowHeightButtonGroup'
);
const option = await buttonGroup.findByCssSelector(`[data-text="${newValue}"]`);
await option.click();
}
public async getCurrentHeaderRowHeightValue() {
const buttonGroup = await this.testSubjects.find(
'unifiedDataTableHeaderRowHeightSettings_rowHeightButtonGroup'
);
return (
await buttonGroup.findByCssSelector('.euiButtonGroupButton-isSelected')
).getVisibleText();
}
public async changeHeaderRowHeightValue(newValue: string) {
const buttonGroup = await this.testSubjects.find(
'unifiedDataTableHeaderRowHeightSettings_rowHeightButtonGroup'
);
const option = await buttonGroup.findByCssSelector(`[data-text="${newValue}"]`);
await option.click();
}
private async findSampleSizeInput() {
return await this.find.byCssSelector(
'input[type="number"][data-test-subj="unifiedDataTableSampleSizeInput"]'
);
}
public async getCurrentSampleSizeValue() {
const sampleSizeInput = await this.findSampleSizeInput();
return Number(await sampleSizeInput.getAttribute('value'));
}
public async changeSampleSizeValue(newValue: number) {
const sampleSizeInput = await this.findSampleSizeInput();
await sampleSizeInput.focus();
// replacing the input values with a new one
await sampleSizeInput.pressKeys([
Key[process.platform === 'darwin' ? 'COMMAND' : 'CONTROL'],
'a',
]);
await sampleSizeInput.type(String(newValue));
}
public async getDetailsRow(): Promise<WebElementWrapper> {
const detailRows = await this.getDetailsRows();
return detailRows[0];
}
public async addInclusiveFilter(detailsRow: WebElementWrapper, fieldName: string): Promise<void> {
const tableDocViewRow = await this.getTableDocViewRow(detailsRow, fieldName);
const addInclusiveFilterButton = await this.getAddInclusiveFilterButton(tableDocViewRow);
await addInclusiveFilterButton.click();
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async getAddInclusiveFilterButton(
tableDocViewRow: WebElementWrapper
): Promise<WebElementWrapper> {
return await tableDocViewRow.findByTestSubject(`~addInclusiveFilterButton`);
}
public async getTableDocViewRow(
detailsRow: WebElementWrapper,
fieldName: string
): Promise<WebElementWrapper> {
return await detailsRow.findByTestSubject(`~tableDocViewRow-${fieldName}`);
}
public async getRemoveInclusiveFilterButton(
tableDocViewRow: WebElementWrapper
): Promise<WebElementWrapper> {
return await tableDocViewRow.findByTestSubject(`~removeInclusiveFilterButton`);
}
public async clickFieldActionInFlyout(fieldName: string, actionName: string): Promise<void> {
const openPopoverButtonSelector = `openFieldActionsButton-${fieldName}`;
const inlineButtonsGroupSelector = `fieldActionsGroup-${fieldName}`;
if (await this.testSubjects.exists(openPopoverButtonSelector)) {
await this.testSubjects.click(openPopoverButtonSelector);
} else {
await this.testSubjects.existOrFail(inlineButtonsGroupSelector);
}
await this.testSubjects.click(`${actionName}-${fieldName}`);
}
public async removeInclusiveFilter(
detailsRow: WebElementWrapper,
fieldName: string
): Promise<void> {
const tableDocViewRow = await this.getTableDocViewRow(detailsRow, fieldName);
const addInclusiveFilterButton = await this.getRemoveInclusiveFilterButton(tableDocViewRow);
await addInclusiveFilterButton.click();
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async hasNoResults() {
return await this.find.existsByCssSelector('.euiDataGrid__noResults');
}
public async checkCurrentRowsPerPageToBe(value: number) {
await this.retry.try(async () => {
return (
(await this.testSubjects.getVisibleText('tablePaginationPopoverButton')) ===
`Rows per page: ${value}`
);
});
}
public async changeRowsPerPageTo(newValue: number) {
await this.testSubjects.click('tablePaginationPopoverButton');
const option = `tablePagination-${newValue}-rows`;
await this.retry.try(async () => {
return this.testSubjects.exists(option);
});
await this.testSubjects.click(option);
await this.checkCurrentRowsPerPageToBe(newValue);
}
}