kibana/test/functional/services/apps_menu.ts
Brandon Kobel 4584a8b570
Elastic License 2.0 (#90099)
* Updating everything except the license headers themselves

* Applying ESLint rules

* Manually replacing the stragglers
2021-02-03 18:12:39 -08:00

139 lines
4.4 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 { FtrProviderContext } from '../ftr_provider_context';
export function AppsMenuProvider({ getService, getPageObjects }: FtrProviderContext) {
const testSubjects = getService('testSubjects');
const log = getService('log');
const config = getService('config');
const defaultFindTimeout = config.get('timeouts.find');
const find = getService('find');
return new (class AppsMenu {
private async waitUntilLoadingHasFinished() {
try {
await this.isGlobalLoadingIndicatorVisible();
} catch (exception) {
if (exception.name === 'ElementNotVisible') {
// selenium might just have been too slow to catch it
} else {
throw exception;
}
}
await this.awaitGlobalLoadingIndicatorHidden();
}
private async isGlobalLoadingIndicatorVisible() {
log.debug('isGlobalLoadingIndicatorVisible');
return await testSubjects.exists('globalLoadingIndicator', { timeout: 1500 });
}
private async awaitGlobalLoadingIndicatorHidden() {
await testSubjects.existOrFail('globalLoadingIndicator-hidden', {
allowHidden: true,
timeout: defaultFindTimeout * 10,
});
}
/**
* Close the collapsible nav
* TODO #64541 can replace with a data-test-subj
*/
public async closeCollapsibleNav() {
const CLOSE_BUTTON = '[data-test-subj=collapsibleNav] > button';
if (await find.existsByCssSelector(CLOSE_BUTTON)) {
// Close button is only visible when focused
const button = await find.byCssSelector(CLOSE_BUTTON);
await button.focus();
await find.clickByCssSelector(CLOSE_BUTTON);
}
}
public async openCollapsibleNav() {
if (!(await testSubjects.exists('collapsibleNav'))) {
await testSubjects.click('toggleNavButton');
}
}
/**
* Get the attributes from each of the links in the apps menu
*/
public async readLinks() {
// wait for the chrome to finish initializing
await this.waitUntilLoadingHasFinished();
await this.openCollapsibleNav();
const appMenu = await testSubjects.find('collapsibleNav');
const $ = await appMenu.parseDomContent();
const links = $.findTestSubjects('collapsibleNavAppLink')
.toArray()
.map((link) => {
return {
text: $(link).text(),
href: $(link).attr('href'),
disabled: $(link).attr('disabled') != null,
};
});
await this.closeCollapsibleNav();
return links;
}
/**
* Get the attributes from the link with the given name.
* @param name
*/
public async getLink(name: string) {
return (await this.readLinks()).find((nl) => nl.text === name);
}
/**
* Determine if an app link with the given name exists
* @param name
*/
public async linkExists(name: string) {
return (await this.readLinks()).some((nl) => nl.text === name);
}
/**
* Click the app link within the app menu that has the given name
* @param name
* @param options.closeCollapsibleNav
* @param options.category - optional field to ensure that a link is clicked in a particular category
* helpful when there may be a recent link with the same name as an app
*/
public async clickLink(
name: string,
{
closeCollapsibleNav = true,
category,
}: { closeCollapsibleNav?: boolean; category?: string } = {}
) {
try {
log.debug(`click "${name}" app link`);
await this.openCollapsibleNav();
let nav;
if (typeof category === 'string') {
nav = await testSubjects.find(`collapsibleNavGroup-${category}`);
} else {
nav = await testSubjects.find('collapsibleNav');
}
const link = await nav.findByPartialLinkText(name);
await link.click();
if (closeCollapsibleNav) {
await this.closeCollapsibleNav();
}
} finally {
// Intentionally empty
}
}
})();
}