kibana/test/functional/page_objects/legacy/data_table_vis.ts
Spencer 4f4cf054de
[7.x] [ts] migrate root test dir to project refs (#99148) (#101416)
* [ts] migrate root test dir to project refs (#99148)

Co-authored-by: spalger <spalger@users.noreply.github.com>

* include schema files in telemetry ts project

Co-authored-by: spalger <spalger@users.noreply.github.com>
2021-06-04 16:22:00 -04:00

84 lines
2.7 KiB
TypeScript

/*
* 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 { FtrService } from '../../ftr_provider_context';
import { WebElementWrapper } from '../../services/lib/web_element_wrapper';
export class LegacyDataTableVisPageObject extends FtrService {
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly retry = this.ctx.getService('retry');
/**
* Converts the table data into nested array
* [ [cell1_in_row1, cell2_in_row1], [cell1_in_row2, cell2_in_row2] ]
* @param element table
*/
private async getDataFromElement(element: WebElementWrapper): Promise<string[][]> {
const $ = await element.parseDomContent();
return $('tr')
.toArray()
.map((row) =>
$(row)
.find('td')
.toArray()
.map((cell) =>
$(cell)
.text()
.replace(/&nbsp;/g, '')
.trim()
)
);
}
public async getTableVisContent({ stripEmptyRows = true } = {}) {
return await this.retry.try(async () => {
const container = await this.testSubjects.find('tableVis');
const allTables = await this.testSubjects.findAllDescendant(
'paginated-table-body',
container
);
if (allTables.length === 0) {
return [];
}
const allData = await Promise.all(
allTables.map(async (t) => {
let data = await this.getDataFromElement(t);
if (stripEmptyRows) {
data = data.filter(
(row) => row.length > 0 && row.some((cell) => cell.trim().length > 0)
);
}
return data;
})
);
if (allTables.length === 1) {
// If there was only one table we return only the data for that table
// This prevents an unnecessary array around that single table, which
// is the case we have in most tests.
return allData[0];
}
return allData;
});
}
public async filterOnTableCell(columnIndex: number, rowIndex: number) {
await this.retry.try(async () => {
const tableVis = await this.testSubjects.find('tableVis');
const cell = await tableVis.findByCssSelector(
`tbody tr:nth-child(${rowIndex}) td:nth-child(${columnIndex})`
);
await cell.moveMouseTo();
const filterBtn = await this.testSubjects.findDescendant('filterForCellValue', cell);
await filterBtn.click();
});
}
}