kibana/test/functional/services/inspector.ts
Cee Chen cbc68cd40c
Upgrade EUI to v94.1.0 (major EuiTable refactors) (#180514)
`v93.6.0`  `v94.1.0`

> [!important]
> 👋 Hello everyone - this is a special release containing `EuiTable`'s
conversion to Emotion, several long-overdue cleanups and breaking
changes, and one or two fun new features. First, let's address the big
questions:

### Q: I'm listed as a codeowner, how much should I manually QA/review?

Answer: It depends on what exactly in your code changed, but _in
general_ I would strongly suggest at least pulling this branch down and
doing a quick visual smoke test of all tables (_note: **not**
datagrids_) in your apps/plugins. You should not expect to see any major
visual regressions.

If your table contained any kind of custom styling or behavior (e.g.
custom CSS, etc.) I **strongly** recommend taking more time to QA
thoroughly to ensure your table still looks and behaves as expected.
Teams with very manual or specific updates will be flagged below in
comment threads.

### Q: When do I need to review by?

This PR will be merged **after** 8.14FF. Because this upgrade touches so
many files and teams, we're aiming for asking for an admin merge by EOD
4/18 regardless of full approval status.

As always, you're welcome to ping us after merge if you find any issues
later ([see our
FAQ](https://github.com/elastic/eui/blob/main/wiki/eui-team-processes/upgrading-kibana.md#faq-for-kibana-teams)),
as you will have until 8.15FF to catch any bugs.

### Q: What breaking changes were made, and why?

Here's a quick shortlist of all the changes made that affected the
majority of the commits in this PR:

#### <u>Removed several top-level table props</u>
- The `responsive` prop has been removed in favor of the new
`responsiveBreakpoint` prop (same `false` behavior as before)
- The following props were removed from basic and in-memory tables:
  - `hasActions`, `isSelectable`, and `isExpandable`
- These props were not used for functionality and were only used for
styling tables in mobile/responsive views, which is not a best practice
pattern we wanted for our APIs. Mobile tables are now styled correctly
without needing consumers to pass these extra props.
  - `textOnly`
- This prop was unused and had no meaningful impact on tables or table
content.

#### Removed hidden mobile vs. desktop DOM

Previously, EUI would set classes that applied `display: none` CSS for
content that was hidden for mobile vs. desktop. This is no longer the
case, and content that only displays for mobile or only displays for
desktop will no longer render to the DOM at all if the table is not in
that responsive state.

This is both more performant when rendering large quantities of
cells/content, and simpler to write test assertions for when comparing
what the user actually sees vs. what the DOM ‘sees’.
(c3eeb08441e4b6efe6505e7cddaa87b540ddb259,
78cefcd954a7b46eaccd05e431b5e24dc86071a3)

#### Removed direct usages of table `className`s

EuiTable `classNames` no longer have any styles attached to them, so
some instances where Kibana usages were applying the `className` for
styles have been replaced with direct component usage or removed
entirely (86ce80b61f).

#### Custom table cell styles

Any custom CSS for table cells was previously being applied to the inner
`div.euiTableCellContent` wrapper. As of this latest release, the
`className` and `css` props will now be applied directly to the outer
`td.euiTableRowCell` element. If you were targeting custom styles table
cells, please re-QA your styles to ensure everything still looks as
expected.

---

<details open><summary>Full changelog (click to collapse)</summary>

##
[`v94.1.0-backport.0`](https://github.com/elastic/eui/releases/v94.1.0-backport.0)

**This is a backport release only intended for use by Kibana.**

**Bug fixes**

- Fixed a visual text alignment regression in `EuiTableRowCell`s with
the `row` header scope
([#7681](https://github.com/elastic/eui/pull/7681))

**Accessibility**

- Improved `EuiBasicTable` and `EuiInMemoryTable`'s selection checkboxes
to have unique aria-labels per row
([#7672](https://github.com/elastic/eui/pull/7672))

## [`v94.1.0`](https://github.com/elastic/eui/releases/v94.1.0)

- Updated `EuiTableHeaderCell` to show a subdued `sortable` icon for
columns that are not currently sorted but can be
([#7656](https://github.com/elastic/eui/pull/7656))
- Updated `EuiBasicTable` and `EuiInMemoryTable`'s
`columns[].actions[]`'s to pass back click events to `onClick` callbacks
as the second callback
([#7667](https://github.com/elastic/eui/pull/7667))

## [`v94.0.0`](https://github.com/elastic/eui/releases/v94.0.0)

- Updated `EuiTable`, `EuiBasicTable`, and `EuiInMemoryTable` with a new
`responsiveBreakpoint` prop, which allows customizing the point at which
the table collapses into a mobile-friendly view with cards
([#7625](https://github.com/elastic/eui/pull/7625))
- Updated `EuiProvider`'s `componentDefaults` prop to allow configuring
`EuiTable.responsiveBreakpoint`
([#7625](https://github.com/elastic/eui/pull/7625))

**Bug fixes**

- `EuiBasicTable` & `EuiInMemoryTable` `isPrimary` actions are now
correctly shown on mobile views
([#7640](https://github.com/elastic/eui/pull/7640))
- Table `mobileOptions`:
([#7642](https://github.com/elastic/eui/pull/7642))
- `mobileOptions.align` is now respected instead of all cells being
forced to left alignment
- `textTruncate` and `textOnly` are now respected even if a `render`
function is not passed

**Breaking changes**

- Removed unused `EuiTableHeaderButton` component
([#7621](https://github.com/elastic/eui/pull/7621))
- Removed the `responsive` prop from `EuiTable`, `EuiBasicTable`, and
`EuiInMemoryTable`. Use the new `responsiveBreakpoint` prop instead
([#7625](https://github.com/elastic/eui/pull/7625))
- The following props are no longer needed by `EuiBasicTable` or
`EuiInMemoryTable` for responsive table behavior to work correctly, and
can be removed: ([#7632](https://github.com/elastic/eui/pull/7632))
  - `isSelectable`
  - `isExpandable`
  - `hasActions`
- Removed the `showOnHover` prop from `EuiTableRowCell` /
`EuiBasicTable`/`EuiInMemoryTable`'s `columns` API. Use the new actions
`columns[].actions[].showOnHover` API instead.
([#7640](https://github.com/elastic/eui/pull/7640))
- Removed top-level `textOnly` prop from `EuiBasicTable` and
`EuiInMemoryTable`. Use `columns[].textOnly` instead.
([#7642](https://github.com/elastic/eui/pull/7642))

**DOM changes**

- `EuiTable` mobile headers no longer render in the DOM when not visible
(previously rendered with `display: none`). This may affect DOM testing
assertions. ([#7625](https://github.com/elastic/eui/pull/7625))
- `EuiTableRowCell` now applies passed `className`s to the parent `<td>`
element, instead of to the inner cell content `<div>`.
([#7631](https://github.com/elastic/eui/pull/7631))
- `EuiTableRow`s rendered by basic and memory tables now only render a
`.euiTableRow-isSelectable` className if the selection checkbox is not
disabled ([#7632](https://github.com/elastic/eui/pull/7632))
- `EuiTableRowCell`s with `textOnly` set to `false` will no longer
attempt to apply the `.euiTableCellContent__text` className to child
elements. ([#7641](https://github.com/elastic/eui/pull/7641))
- `EuiTableRowCell` no longer renders mobile headers to the DOM unless
the current table is displaying its responsive view.
([#7642](https://github.com/elastic/eui/pull/7642))
- `EuiTableHeaderCell` and `EuiTableRowCell` will no longer render in
the DOM at all on mobile if their columns' `mobileOptions.show` is set
to `false`. ([#7642](https://github.com/elastic/eui/pull/7642))
- `EuiTableHeaderCell` and `EuiTableRowCell` will no longer render in
the DOM at all on desktop if their columns' `mobileOptions.only` is set
to `true`. ([#7642](https://github.com/elastic/eui/pull/7642))

**CSS-in-JS conversions**

- Converted `EuiTable`, `EuiTableRow`, `EuiTableRowCell`, and all other
table subcomponents to Emotion
([#7654](https://github.com/elastic/eui/pull/7654))
- Removed the following `EuiTable` Sass variables:
([#7654](https://github.com/elastic/eui/pull/7654))
  - `$euiTableCellContentPadding`
  - `$euiTableCellContentPaddingCompressed`
  - `$euiTableCellCheckboxWidth`
  - `$euiTableHoverColor`
  - `$euiTableSelectedColor`
  - `$euiTableHoverSelectedColor`
  - `$euiTableActionsBorderColor`
  - `$euiTableHoverClickableColor`
  - `$euiTableFocusClickableColor`
- Removed the following `EuiTable` Sass mixins:
([#7654](https://github.com/elastic/eui/pull/7654))
  - `euiTableActionsBackgroundMobile`
  - `euiTableCellCheckbox`
  - `euiTableCell`
</details>
2024-04-19 00:05:48 +01:00

331 lines
11 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 expect from '@kbn/expect';
import { FtrService } from '../ftr_provider_context';
interface FilterForTableCell {
column: number;
row: number;
filter: 'in' | 'out';
}
export class InspectorService extends FtrService {
private readonly log = this.ctx.getService('log');
private readonly retry = this.ctx.getService('retry');
private readonly renderable = this.ctx.getService('renderable');
private readonly flyout = this.ctx.getService('flyout');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly find = this.ctx.getService('find');
private readonly comboBox = this.ctx.getService('comboBox');
private readonly monacoEditor = this.ctx.getService('monacoEditor');
private async getIsEnabled(): Promise<boolean> {
const ariaDisabled = await this.testSubjects.getAttribute('openInspectorButton', 'disabled');
return ariaDisabled !== 'true';
}
/**
* Asserts that inspector is enabled
*/
public async expectIsEnabled(): Promise<void> {
await this.retry.try(async () => {
const isEnabled = await this.getIsEnabled();
expect(isEnabled).to.be(true);
});
}
/**
* Asserts that inspector is disabled
*/
public async expectIsNotEnabled(): Promise<void> {
await this.retry.try(async () => {
const isEnabled = await this.getIsEnabled();
expect(isEnabled).to.be(false);
});
}
/**
* Opens inspector panel
*/
public async open(openButton: string = 'openInspectorButton'): Promise<void> {
this.log.debug('Inspector.open');
const isOpen = await this.testSubjects.exists('inspectorPanel');
if (!isOpen) {
await this.retry.try(async () => {
await this.testSubjects.click(openButton);
await this.testSubjects.exists('inspectorPanel');
});
}
}
/**
* Closes inspector panel
*/
public async close(): Promise<void> {
this.log.debug('Close Inspector');
let isOpen = await this.testSubjects.exists('inspectorPanel');
if (isOpen) {
await this.retry.try(async () => {
await this.flyout.close('inspectorPanel');
isOpen = await this.testSubjects.exists('inspectorPanel');
if (isOpen) {
throw new Error('Failed to close inspector');
}
});
}
}
/**
* Asserts data on inspector panel
* @param expectedData
*/
public async expectTableData(expectedData: string[][]): Promise<void> {
this.log.debug(`Inspector.expectTableData(${expectedData.join(',')})`);
const data = await this.getTableData();
expect(data).to.eql(expectedData);
}
/**
* Sets table page size
* @param size rows count
*/
public async setTablePageSize(size: number): Promise<void> {
const panel = await this.testSubjects.find('inspectorPanel');
await this.find.clickByButtonText('Rows per page: 20', panel);
// The buttons for setting table page size are in a popover element. This popover
// element appears as if it's part of the inspectorPanel but it's really attached
// to the body element by a portal.
const tableSizesPopover = await this.find.byCssSelector('.euiPanel .euiContextMenuPanel');
await this.find.clickByButtonText(`${size} rows`, tableSizesPopover);
}
/**
* Returns table data in nested array format
*/
public async getTableData(): Promise<string[][]> {
// TODO: we should use datat-test-subj=inspectorTable as soon as EUI supports it
const inspectorPanel = await this.testSubjects.find('inspectorPanel');
const tableBody = await this.retry.try(async () => inspectorPanel.findByTagName('tbody'));
const $ = await tableBody.parseDomContent();
return $('tr')
.toArray()
.map((tr) => {
return $(tr)
.find('td')
.toArray()
.map((cell) => {
// if this is an EUI table, filter down to the specific cell content
// otherwise this will include mobile-specific header information
const euiTableCellContent = $(cell).find('.euiTableCellContent');
if (euiTableCellContent.length > 0) {
return $(cell).find('.euiTableCellContent').text().trim();
} else {
return $(cell).text().trim();
}
});
});
}
/**
* Returns table headers
*/
public async getTableHeaders(): Promise<string[]> {
this.log.debug('Inspector.getTableHeaders');
// TODO: we should use datat-test-subj=inspectorTable as soon as EUI supports it
const dataTableHeader = await this.retry.try(async () => {
const inspectorPanel = await this.testSubjects.find('inspectorPanel');
return await inspectorPanel.findByTagName('thead');
});
const $ = await dataTableHeader.parseDomContent();
return $('th .euiTableCellContent span')
.toArray()
.map((cell) => $(cell).text().trim());
}
/**
* Asserts table headers
* @param expected expected headers
*/
public async expectTableHeaders(expected: string[]): Promise<void> {
await this.retry.try(async () => {
const headers = await this.getTableHeaders();
expect(headers).to.eql(expected);
});
}
/**
* Filter / filter out table for value by clicking specified cell
* @param column column index
* @param row row index
* @param filter 'in' to filter and 'out' to filter out
*/
public async filterForTableCell(options: FilterForTableCell): Promise<void> {
const filterLocator = {
in: 'filterForInspectorCellValue',
out: 'filterOutInspectorCellValue',
};
await this.retry.try(async () => {
const table = await this.testSubjects.find('inspectorTable');
const cell = await table.findByCssSelector(
`tbody tr:nth-child(${options.row}) td:nth-child(${options.column})`
);
await cell.moveMouseTo();
const filterBtn = await this.testSubjects.findDescendant(filterLocator[options.filter], cell);
await filterBtn.click();
});
await this.renderable.waitForRender();
}
/**
* Opens inspector viewId for example 'Requests
* @param viewId
*/
public async openInspectorView(viewId: string): Promise<void> {
this.log.debug(`Open Inspector view ${viewId}`);
const dtsViewId = 'inspectorViewChooser' + viewId;
await this.retry.try(async () => {
await this.testSubjects.click('inspectorViewChooser');
// check whether popover menu opens, if not, fail and retry opening
await this.testSubjects.existOrFail(dtsViewId, { timeout: 2000 });
await this.testSubjects.click(dtsViewId);
const selection = await this.testSubjects.getVisibleText('inspectorViewChooser');
this.log.debug(`inspector view selection = ${selection}`);
expect(selection.includes(viewId)).to.be(true);
});
}
/**
* Opens inspector requests view
*/
public async openInspectorRequestsView(): Promise<void> {
if (!(await this.testSubjects.exists('inspectorViewChooser'))) return;
await this.openInspectorView('Requests');
}
/**
* Check how many tables are being shown in the inspector.
* @returns
*/
public async getNumberOfTables(): Promise<number> {
const chooserDataTestId = 'inspectorTableChooser';
const menuDataTestId = 'inspectorTableChooserMenuPanel';
if (!(await this.testSubjects.exists(chooserDataTestId))) {
return 1;
}
return await this.retry.try(async () => {
await this.testSubjects.click(chooserDataTestId);
const menu = await this.testSubjects.find(menuDataTestId);
return (
await menu.findAllByCssSelector(`[data-test-subj="${menuDataTestId}"] .euiContextMenuItem`)
).length;
});
}
public async getTableDataWithId(tableTestSubj: string): Promise<string[][]> {
const chooserDataTestId = 'inspectorTableChooser';
if (!(await this.testSubjects.exists(chooserDataTestId))) {
return [];
}
return await this.retry.try(async () => {
await this.testSubjects.click(chooserDataTestId);
await this.testSubjects.click(tableTestSubj);
return this.getTableData();
});
}
/**
* Returns the selected option value from combobox
*/
public async getSelectedOption(): Promise<string> {
await this.openInspectorRequestsView();
const selectedOption = await this.comboBox.getComboBoxSelectedOptions(
'inspectorRequestChooser'
);
if (selectedOption.length !== 1) {
return 'Combobox has multiple options';
}
return selectedOption[0];
}
/**
* Opens request by name. Use when inspector has multiple requests and you want to view a specific request
*/
public async openRequestByName(requestName: string): Promise<void> {
await this.openInspectorRequestsView();
this.log.debug(`Open Inspector request ${requestName}`);
await this.testSubjects.click('inspectorRequestChooser');
await this.testSubjects.click(`inspectorRequestChooser${requestName.replace(/\s+/, '_')}`);
}
/**
* Returns request name as the comma-separated string from combobox
*/
public async getRequestNames(): Promise<string> {
await this.openInspectorRequestsView();
const comboBoxOptions = await this.comboBox.getOptionsList('inspectorRequestChooser');
return comboBoxOptions.trim().split('\n').join(',');
}
public getOpenRequestStatisticButton() {
return this.testSubjects.find('inspectorRequestDetailStatistics');
}
public getOpenRequestDetailRequestButton() {
return this.testSubjects.find('inspectorRequestDetailRequest');
}
public getOpenRequestDetailResponseButton() {
return this.testSubjects.find('inspectorRequestDetailResponse');
}
public async getRequest(
codeEditorIndex: number = 0
): Promise<{ command: string; body: Record<string, any> }> {
await (await this.getOpenRequestDetailRequestButton()).click();
await this.monacoEditor.waitCodeEditorReady('inspectorRequestCodeViewerContainer');
const requestString = await this.monacoEditor.getCodeEditorValue(codeEditorIndex);
this.log.debug('Request string from inspector:', requestString);
const openBraceIndex = requestString.indexOf('{');
return {
command: openBraceIndex >= 0 ? requestString.substring(0, openBraceIndex).trim() : '',
body: openBraceIndex >= 0 ? JSON.parse(requestString.substring(openBraceIndex)) : {},
};
}
public async getResponse(): Promise<Record<string, any>> {
await (await this.getOpenRequestDetailResponseButton()).click();
await this.monacoEditor.waitCodeEditorReady('inspectorRequestCodeViewerContainer');
const responseString = await this.monacoEditor.getCodeEditorValue();
this.log.debug('Response string from inspector:', responseString);
return JSON.parse(responseString);
}
/**
* Returns true if the value equals the combobox options list
* @param value default combobox single option text
*/
public async hasSingleRequest(
value: string = "You've selected all available options"
): Promise<boolean> {
await this.openInspectorRequestsView();
const comboBoxOptions = await this.comboBox.getOptionsList('inspectorRequestChooser');
return value === comboBoxOptions;
}
}