Skip to content

Commit 663a015

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[test] Fix types of selector root
This makes sure that we call the helpers with ElementHandle and not a JsHandle which will throw as it does not have the methods we are trying to call. Bug: none Change-Id: I3458af0f0774f9ab1c5b2f5f52c59704442f8f0a Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6253044 Auto-Submit: Nikolay Vitkov <[email protected]> Commit-Queue: Alex Rudenko <[email protected]> Reviewed-by: Alina Varkki <[email protected]> Reviewed-by: Alex Rudenko <[email protected]>
1 parent a9561e0 commit 663a015

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

test/e2e/elements/accessibility-tree_test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import type * as puppeteer from 'puppeteer-core';
6+
57
import {
68
assertNotNullOrUndefined,
79
click,
@@ -33,8 +35,9 @@ describe('Accessibility Tree in the Elements Tab', function() {
3335
`RootWebArea\xa0"Simple page with aria labeled element" focusable:\xa0true url:\xa0${
3436
getResourcesPath()}/elements/accessibility-simple-page.html`);
3537
const arrowIconContainer =
36-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37-
await iframeDoc.evaluateHandle(node => (node as any).parentElementOrShadowHost().parentElement.parentElement);
38+
(await iframeDoc.evaluateHandle(
39+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40+
node => (node as any).parentElementOrShadowHost().parentElement.parentElement)) as puppeteer.ElementHandle;
3841
assertNotNullOrUndefined(arrowIconContainer);
3942
await click('.arrow-icon', {root: arrowIconContainer});
4043
await waitForElementWithTextContent(`link\xa0"cats" focusable:\xa0true url:\xa0${getResourcesPath()}/elements/x`);

test/e2e/helpers/issues-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const HIDE_THIS_ISSUE = 'Hide issues like this';
3838
export const UNHIDE_THIS_ISSUE = 'Unhide issues like this';
3939
export const UNHIDE_ALL_ISSUES = '.unhide-all-issues-button';
4040

41-
export async function getHideIssuesMenu(root?: puppeteer.JSHandle) {
41+
export async function getHideIssuesMenu(root?: puppeteer.ElementHandle) {
4242
return await waitFor(HIDE_ISSUES_MENU, root);
4343
}
4444

test/e2e/helpers/sources-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export async function getBreakpointHitLocation() {
196196
const locationHandle = await waitFor('.location', breakpointHitHandle);
197197
const locationText = await locationHandle.evaluate(location => location.textContent);
198198

199-
const groupHandle = await breakpointHitHandle.evaluateHandle(x => x.parentElement);
199+
const groupHandle = await breakpointHitHandle.evaluateHandle(x => x.parentElement!);
200200
const groupHeaderTitleHandle = await waitFor('.group-header-title', groupHandle);
201201
const groupHeaderTitle = await groupHeaderTitleHandle?.evaluate(header => header.textContent);
202202

test/e2e/host/user-metrics_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ describe('User Metrics for clicking stylesheet request initiators', () => {
647647
actionCode: 80, // StyleSheetInitiatorLinkClicked
648648
};
649649
it('dispatches an event when clicked in the console', async () => {
650-
async function clickOnLinkWithText(text: string, root?: puppeteer.JSHandle) {
650+
async function clickOnLinkWithText(text: string, root?: puppeteer.ElementHandle) {
651651
const element = await click(`text/${text}`, {root});
652652
assert.isTrue(
653653
await element.evaluate(e => e.classList.contains('devtools-link')),

test/e2e/memory/memory_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('The Memory Panel', function() {
319319

320320
const header = await waitForElementWithTextContent('Live Count');
321321
const table = await header.evaluateHandle(node => {
322-
return node.closest('.data-grid');
322+
return node.closest('.data-grid')!;
323323
});
324324
await waitFor('.data-grid-data-grid-node', table);
325325
});

test/shared/helper.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ declare global {
3131
const globalThis: any = global;
3232

3333
export interface ClickOptions {
34-
root?: puppeteer.JSHandle;
34+
root?: puppeteer.ElementHandle;
3535
clickOptions?: puppeteer.ClickOptions;
3636
maxPixelsFromLeft?: number;
3737
}
@@ -54,14 +54,14 @@ export const click = async (selector: string, options?: ClickOptions) => {
5454
selector, {root: options?.root}, element => element.click(options?.clickOptions));
5555
};
5656

57-
export const hover = async (selector: string, options?: {root?: puppeteer.JSHandle}) => {
57+
export const hover = async (selector: string, options?: {root?: puppeteer.ElementHandle}) => {
5858
return await performActionOnSelector(selector, {root: options?.root}, element => element.hover());
5959
};
6060

6161
type Action = (element: puppeteer.ElementHandle) => Promise<void>;
6262

6363
async function performActionOnSelector(
64-
selector: string, options: {root?: puppeteer.JSHandle}, action: Action): Promise<puppeteer.ElementHandle> {
64+
selector: string, options: {root?: puppeteer.ElementHandle}, action: Action): Promise<puppeteer.ElementHandle> {
6565
// TODO(crbug.com/1410168): we should refactor waitFor to be compatible with
6666
// Puppeteer's syntax for selectors.
6767
const queryHandlers = new Set([
@@ -138,7 +138,7 @@ export async function hoverElement(element: puppeteer.ElementHandle): Promise<vo
138138
}
139139

140140
export const doubleClick =
141-
async (selector: string, options?: {root?: puppeteer.JSHandle, clickOptions?: puppeteer.ClickOptions}) => {
141+
async (selector: string, options?: {root?: puppeteer.ElementHandle, clickOptions?: puppeteer.ClickOptions}) => {
142142
const passedClickOptions = (options && options.clickOptions) || {};
143143
const clickOptionsWithDoubleClick: puppeteer.ClickOptions = {
144144
...passedClickOptions,
@@ -200,10 +200,10 @@ export const pasteText = async (text: string) => {
200200
};
201201

202202
// Get a single element handle. Uses `pierce` handler per default for piercing Shadow DOM.
203-
export const $ =
204-
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle, handler = 'pierce') => {
203+
export const $ = async<ElementType extends Element = Element>(
204+
selector: string, root?: puppeteer.ElementHandle, handler = 'pierce') => {
205205
const {frontend} = getBrowserAndPages();
206-
const rootElement = root ? root as puppeteer.ElementHandle : frontend;
206+
const rootElement = root ? root : frontend;
207207
const element = await rootElement.$(`${handler}/${selector}`) as puppeteer.ElementHandle<ElementType>;
208208
return element;
209209
};
@@ -223,7 +223,7 @@ export const $$ =
223223
* @param textContent The text content to search for.
224224
* @param root The root of the search.
225225
*/
226-
export const $textContent = async (textContent: string, root?: puppeteer.JSHandle) => {
226+
export const $textContent = async (textContent: string, root?: puppeteer.ElementHandle) => {
227227
return $(textContent, root, 'pierceShadowText');
228228
};
229229

@@ -233,14 +233,14 @@ export const $textContent = async (textContent: string, root?: puppeteer.JSHandl
233233
* @param textContent The text content to search for.
234234
* @param root The root of the search.
235235
*/
236-
export const $$textContent = async (textContent: string, root?: puppeteer.JSHandle) => {
236+
export const $$textContent = async (textContent: string, root?: puppeteer.ElementHandle) => {
237237
return $$(textContent, root, 'pierceShadowText');
238238
};
239239

240240
export const timeout = (duration: number) => new Promise(resolve => setTimeout(resolve, duration));
241241

242242
export const getTextContent =
243-
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle) => {
243+
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.ElementHandle) => {
244244
const text = await (await $<ElementType>(selector, root))?.evaluate(node => node.textContent);
245245
return text ?? undefined;
246246
};
@@ -266,15 +266,15 @@ export const getVisibleTextContents = async (selector: string) => {
266266
};
267267

268268
export const waitFor = async<ElementType extends Element = Element>(
269-
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
269+
selector: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope(), handler?: string) => {
270270
return await asyncScope.exec(() => waitForFunction(async () => {
271271
const element = await $<ElementType>(selector, root, handler);
272272
return (element || undefined);
273273
}, asyncScope), `Waiting for element matching selector '${selector}'`);
274274
};
275275

276276
export const waitForVisible = async<ElementType extends Element = Element>(
277-
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
277+
selector: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope(), handler?: string) => {
278278
return await asyncScope.exec(() => waitForFunction(async () => {
279279
const element = await $<ElementType>(selector, root, handler);
280280
const visible = await element.evaluate(node => node.checkVisibility());
@@ -283,15 +283,16 @@ export const waitForVisible = async<ElementType extends Element = Element>(
283283
};
284284

285285
export const waitForMany = async (
286-
selector: string, count: number, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
286+
selector: string, count: number, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope(),
287+
handler?: string) => {
287288
return await asyncScope.exec(() => waitForFunction(async () => {
288289
const elements = await $$(selector, root, handler);
289290
return elements.length >= count ? elements : undefined;
290291
}, asyncScope), `Waiting for ${count} elements to match selector '${selector}'`);
291292
};
292293

293294
export const waitForNone =
294-
async (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
295+
async (selector: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope(), handler?: string) => {
295296
return await asyncScope.exec(() => waitForFunction(async () => {
296297
const elements = await $$(selector, root, handler);
297298
if (elements.length === 0) {
@@ -302,21 +303,21 @@ export const waitForNone =
302303
};
303304

304305
export const waitForAria = <ElementType extends Element = Element>(
305-
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
306+
selector: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope()) => {
306307
return waitFor<ElementType>(selector, root, asyncScope, 'aria');
307308
};
308309

309-
export const waitForAriaNone = (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
310+
export const waitForAriaNone = (selector: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope()) => {
310311
return waitForNone(selector, root, asyncScope, 'aria');
311312
};
312313

313314
export const waitForElementWithTextContent =
314-
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
315+
(textContent: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope()) => {
315316
return waitFor(textContent, root, asyncScope, 'pierceShadowText');
316317
};
317318

318319
export const waitForElementsWithTextContent =
319-
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
320+
(textContent: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope()) => {
320321
return asyncScope.exec(() => waitForFunction(async () => {
321322
const elems = await $$textContent(textContent, root);
322323
if (elems && elems.length) {
@@ -328,7 +329,7 @@ export const waitForElementsWithTextContent =
328329
};
329330

330331
export const waitForNoElementsWithTextContent =
331-
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
332+
(textContent: string, root?: puppeteer.ElementHandle, asyncScope = new AsyncScope()) => {
332333
return asyncScope.exec(() => waitForFunction(async () => {
333334
const elems = await $$textContent(textContent, root);
334335
if (elems && elems.length === 0) {
@@ -374,7 +375,7 @@ export const waitForFunctionWithTries = async<T>(
374375
};
375376

376377
export const waitForWithTries = async (
377-
selector: string, root?: puppeteer.JSHandle, options: {tries: number} = {
378+
selector: string, root?: puppeteer.ElementHandle, options: {tries: number} = {
378379
tries: Number.MAX_SAFE_INTEGER,
379380
},
380381
asyncScope = new AsyncScope(), handler?: string) => {
@@ -653,7 +654,7 @@ export const selectOption = async (select: puppeteer.ElementHandle<HTMLSelectEle
653654
}, value);
654655
};
655656

656-
export const scrollElementIntoView = async (selector: string, root?: puppeteer.JSHandle) => {
657+
export const scrollElementIntoView = async (selector: string, root?: puppeteer.ElementHandle) => {
657658
const element = await $(selector, root);
658659

659660
if (!element) {

0 commit comments

Comments
 (0)