-
Notifications
You must be signed in to change notification settings - Fork 8k
/
data_grid.ts
380 lines (323 loc) · 12.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
/*
* 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 { FtrService } from '../ftr_provider_context';
import { WebElementWrapper } from './lib/web_element_wrapper';
export interface TabbedGridData {
columns: string[];
rows: string[][];
}
interface SelectOptions {
isAnchorRow?: boolean;
rowIndex?: 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));
}
/**
* 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 }
): Promise<void> {
const row = await this.getRow(options);
const testSubj = options.isAnchorRow
? '~docTableExpandToggleColumnAnchor'
: '~docTableExpandToggleColumn';
const toggle = await row[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 textContent = await cell.getAttribute('textContent');
textArr.push(textContent.trim());
}
return Promise.resolve(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 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);
}
}