Skip to content

Commit

Permalink
Type safe browser.executeAsync (#69018) (#69154)
Browse files Browse the repository at this point in the history
* make browserAsync type safe

* adopt tests

* prefer unknown over any

* simplify signature
  • Loading branch information
mshustov authored Jun 15, 2020
1 parent d07b0de commit b10e6b1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
24 changes: 20 additions & 4 deletions test/functional/services/common/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,27 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
);
}

public async executeAsync<R>(
fn: string | ((...args: any[]) => Promise<R>),
public async executeAsync<T = unknown>(fn: (cb: (value?: T) => void) => void): Promise<T>;
public async executeAsync<T = unknown, A1 = unknown>(
fn: (a1: A1, cb: (value?: T) => void) => void,
a1: A1
): Promise<T>;
public async executeAsync<T = unknown, A1 = unknown, A2 = unknown>(
fn: (a1: A1, a2: A2, cb: (value?: T) => void) => void,
a1: A1,
a2: A2
): Promise<T>;
public async executeAsync<T = unknown, A1 = unknown, A2 = unknown, A3 = unknown>(
fn: (a1: A1, a2: A2, a3: A3, cb: (value?: T) => void) => void,
a1: A1,
a2: A2,
a3: A3
): Promise<T>;
public async executeAsync<T = unknown>(
fn: (...args: any[]) => void,
...args: any[]
): Promise<R> {
return await driver.executeAsyncScript(
): Promise<T> {
return await driver.executeAsyncScript<T>(
fn,
...cloneDeep<any>(args, (arg) => {
if (arg instanceof WebElementWrapper) {
Expand Down
14 changes: 7 additions & 7 deletions test/interpreter_functional/test_suites/run_pipeline/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ export function expectExpressionProvider({
stepContext: ExpressionValue = context
): Promise<ExpressionResult> => {
log.debug(`running expression ${step || expression}`);
return browser.executeAsync<ExpressionResult>(
(
_expression: string,
_currentContext: ExpressionValue & { type: string },
_initialContext: ExpressionValue,
done: (expressionResult: ExpressionResult) => void
) => {
return browser.executeAsync<
ExpressionResult,
string,
ExpressionValue & { type: string },
ExpressionValue
>(
(_expression, _currentContext, _initialContext, done) => {
if (!_currentContext) _currentContext = { type: 'null' };
if (!_currentContext.type) _currentContext.type = 'null';
return window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
const testSubjects = getService('testSubjects');

const setNonReloadedFlag = () => {
return browser.executeAsync(async (cb: Function) => {
return browser.executeAsync(async (cb) => {
window.__nonReloadedFlag = true;
cb();
});
};
const wasReloaded = (): Promise<boolean> => {
const wasReloaded = () => {
return browser.executeAsync<boolean>(async (cb) => {
const reloaded = window.__nonReloadedFlag !== true;
cb(reloaded);
return reloaded;
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
const testSubjects = getService('testSubjects');

const setAppStatus = async (s: Partial<AppUpdatableFields>) => {
return browser.executeAsync(async (status: Partial<AppUpdatableFields>, cb: Function) => {
return browser.executeAsync(async (status, cb) => {
window.__coreAppStatus.setAppStatus(status);
cb();
}, s);
};

const navigateToApp = async (i: string) => {
return (await browser.executeAsync(async (appId, cb: Function) => {
return await browser.executeAsync(async (appId, cb) => {
await window.__coreAppStatus.navigateToApp(appId);
cb();
}, i)) as any;
}, i);
};

// FLAKY: https://github.com/elastic/kibana/issues/65423
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide

it('to start services via coreSetup.getStartServices', async () => {
expect(
await browser.executeAsync(async (cb) => {
await browser.executeAsync<boolean>(async (cb) => {
const [coreStart] = await window.__coreProvider.setup.core.getStartServices();
cb(Boolean(coreStart.overlays));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide

expect(settingsValue).to.be('2');

const settingsValueViaObservables = await browser.executeAsync(async (callback: Function) => {
const settingsValueViaObservables = await browser.executeAsync(async (callback) => {
window.__coreProvider.setup.core.uiSettings
.get$('ui_settings_plugin')
.subscribe((v) => callback(v));
Expand Down
8 changes: 4 additions & 4 deletions x-pack/test/licensing_plugin/public/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function (ftrContext: FtrProviderContext) {
await scenario.waitForPluginToDetectLicenseUpdate();

expect(
await browser.executeAsync(async (cb: Function) => {
await browser.executeAsync(async (cb) => {
const { setup, testUtils } = window.__coreProvider;
// this call enforces signature check to detect license update
// and causes license re-fetch
Expand All @@ -43,7 +43,7 @@ export default function (ftrContext: FtrProviderContext) {
await scenario.waitForPluginToDetectLicenseUpdate();

expect(
await browser.executeAsync(async (cb: Function) => {
await browser.executeAsync(async (cb) => {
const { setup, testUtils } = window.__coreProvider;
// this call enforces signature check to detect license update
// and causes license re-fetch
Expand All @@ -59,7 +59,7 @@ export default function (ftrContext: FtrProviderContext) {
await scenario.waitForPluginToDetectLicenseUpdate();

expect(
await browser.executeAsync(async (cb: Function) => {
await browser.executeAsync(async (cb) => {
const { setup, testUtils } = window.__coreProvider;
// this call enforces signature check to detect license update
// and causes license re-fetch
Expand All @@ -75,7 +75,7 @@ export default function (ftrContext: FtrProviderContext) {
await scenario.waitForPluginToDetectLicenseUpdate();

expect(
await browser.executeAsync(async (cb: Function) => {
await browser.executeAsync(async (cb) => {
const { setup, testUtils } = window.__coreProvider;
// this call enforces signature check to detect license update
// and causes license re-fetch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
const browser = getService('browser');

const findResultsWithAPI = async (t: string): Promise<GlobalSearchResult[]> => {
return browser.executeAsync(async (term: string, cb: Function) => {
return browser.executeAsync(async (term, cb) => {
const { start } = window.__coreProvider;
const globalSearchTestApi: GlobalSearchTestApi = start.plugins.globalSearchTest;
globalSearchTestApi.findAll(term).then((results) => {
cb(results);
});
}, t) as any; // executeAsync signature is broken. return type should be inferred from the cb param.
globalSearchTestApi.findAll(term).then(cb);
}, t);
};

describe('GlobalSearch API', function () {
Expand Down

0 comments on commit b10e6b1

Please sign in to comment.