Skip to content

Commit

Permalink
chore: use correct types from puppeteer-core
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Dec 13, 2022
1 parent 4a21f8c commit 4609583
Show file tree
Hide file tree
Showing 25 changed files with 82 additions and 69 deletions.
4 changes: 2 additions & 2 deletions packages/devtools/src/commands/elementSendKeys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { UNICODE_CHARACTERS } from '@wdio/utils'
import type { KeyInput } from 'puppeteer-core'
import type { ElementHandle, KeyInput } from 'puppeteer-core'

import { getStaleElementError } from '../utils'
import type DevToolsDriver from '../devtoolsdriver'
Expand All @@ -20,7 +20,7 @@ export default async function elementSendKeys (
this: DevToolsDriver,
{ elementId, text }: { elementId: string, text: string }
) {
const elementHandle = await this.elementStore.get(elementId)
const elementHandle = await this.elementStore.get(elementId) as any as ElementHandle<HTMLInputElement>

if (!elementHandle) {
throw getStaleElementError(elementId)
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/src/commands/performActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { keyDefinitions, KeyInput } from 'puppeteer-core/lib/cjs/puppeteer/common/USKeyboardLayout'
import { _keyDefinitions, KeyInput } from 'puppeteer-core/lib/cjs/puppeteer/common/USKeyboardLayout'
import type { Keyboard, Mouse } from 'puppeteer-core/lib/cjs/puppeteer/common/Input'

import getElementRect from './getElementRect'
Expand Down Expand Up @@ -81,7 +81,7 @@ export default async function performActions(
* for special characters like emojis 😉 we need to
* send in the value as text because it is not unicode
*/
if (!keyDefinitions[singleAction.value as unknown as KeyInput]) {
if (!_keyDefinitions[singleAction.value as unknown as KeyInput]) {
await page.keyboard.sendCharacter(singleAction.value as unknown as KeyInput)
skipChars.push(singleAction.value)
continue
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/src/commands/switchToFrame.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/FrameManager'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/Frame'
import type { ElementReference } from '@wdio/protocols'

import { ELEMENT_KEY } from '../constants'
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/src/commands/switchToParentFrame.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/FrameManager'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/Frame'
import type DevToolsDriver from '../devtoolsdriver'

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools/src/devtoolsdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import path from 'path'
import { v4 as uuidv4 } from 'uuid'

import logger from '@wdio/logger'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/common/Browser'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/api/Browser'
import type { Dialog } from 'puppeteer-core/lib/cjs/puppeteer/common/Dialog'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { Target } from 'puppeteer-core/lib/cjs/puppeteer/common/Target'
import type { CommandEndpoint } from '@wdio/protocols'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/FrameManager'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/Frame'

import ElementStore from './elementstore'
import { validate, sanitizeError } from './utils'
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools/src/elementstore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ElementHandle } from 'puppeteer-core/lib/cjs/puppeteer/common/JSHandle'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/FrameManager'
import type { ElementHandle } from 'puppeteer-core/lib/cjs/puppeteer/common/ElementHandle'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/Frame'

export default class ElementStore {
private _index = 0
Expand All @@ -9,7 +9,7 @@ export default class ElementStore {
set (elementHandle: ElementHandle) {
const index = `ELEMENT-${++this._index}`
this._elementMap.set(index, elementHandle)
const frame = elementHandle.executionContext().frame()
const frame = elementHandle.executionContext()['_world']?.frame()
if (frame) {
let elementIndexes = this._frameMap.get(frame)
if (!elementIndexes) {
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { webdriverMonad, devtoolsEnvironmentDetector } from '@wdio/utils'
import { validateConfig } from '@wdio/config'
import type { CommandEndpoint } from '@wdio/protocols'
import type { Options, Capabilities } from '@wdio/types'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/common/Browser'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/api/Browser'

import DevToolsDriver from './devtoolsdriver'
import launch from './launcher'
Expand Down
14 changes: 7 additions & 7 deletions packages/devtools/src/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { launch as launchChromeBrowser } from 'chrome-launcher'
import puppeteer, { PuppeteerNodeLaunchOptions } from 'puppeteer-core'
import puppeteer, { PuppeteerNodeLaunchOptions, KnownDevices, Puppeteer, ConnectOptions } from 'puppeteer-core'
import logger from '@wdio/logger'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/common/Browser'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/api/Browser'
import type { Capabilities } from '@wdio/types'
import { QueryHandler } from 'query-selector-shadow-dom/plugins/puppeteer'

Expand All @@ -26,7 +26,7 @@ import type { ExtendedCapabilities, DevToolsOptions } from './types'

const log = logger('devtools')

const DEVICE_NAMES = Object.values(puppeteer.devices).map((device) => device.name)
const DEVICE_NAMES = Object.keys(KnownDevices)

/**
* launches Chrome and returns a Puppeteer browser instance
Expand All @@ -51,7 +51,7 @@ async function launchChrome (capabilities: ExtendedCapabilities) {
let headless = (chromeOptions as any).headless || devtoolsOptions.headless

if (typeof mobileEmulation.deviceName === 'string') {
const deviceProperties = Object.values(puppeteer.devices).find(device => device.name === mobileEmulation.deviceName)
const deviceProperties = KnownDevices[mobileEmulation.deviceName as keyof typeof KnownDevices]

if (!deviceProperties) {
throw new Error(`Unknown device name "${mobileEmulation.deviceName}", available: ${DEVICE_NAMES.join(', ')}`)
Expand Down Expand Up @@ -199,16 +199,16 @@ function launchBrowser (capabilities: ExtendedCapabilities, browserType: 'edge'
function connectBrowser (connectionUrl: string, capabilities: ExtendedCapabilities) {
const connectionProp = connectionUrl.startsWith('http') ? 'browserURL' : 'browserWSEndpoint'
const devtoolsOptions = capabilities['wdio:devtoolsOptions']
const options: puppeteer.ConnectOptions = {
const options: ConnectOptions = {
[connectionProp]: connectionUrl,
...devtoolsOptions
}
return puppeteer.connect(options) as unknown as Promise<Browser>
}

export default async function launch (capabilities: ExtendedCapabilities) {
puppeteer.unregisterCustomQueryHandler('shadow')
puppeteer.registerCustomQueryHandler('shadow', QueryHandler)
Puppeteer.unregisterCustomQueryHandler('shadow')
Puppeteer.registerCustomQueryHandler('shadow', QueryHandler as any)
const browserName = capabilities.browserName?.toLowerCase()

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/devtools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import logger from '@wdio/logger'
import { commandCallStructure, isValidParameter, getArgumentType, canAccess } from '@wdio/utils'
import { WebDriverProtocol, CommandParameters, CommandPathVariables, ElementReference } from '@wdio/protocols'
import type { Logger } from '@wdio/logger'
import type { ElementHandle } from 'puppeteer-core/lib/cjs/puppeteer/common/JSHandle'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/common/Browser'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/FrameManager'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { ElementHandle } from 'puppeteer-core/lib/cjs/puppeteer/common/ElementHandle'
import type { Browser } from 'puppeteer-core/lib/cjs/puppeteer/api/Browser'
import type { Frame } from 'puppeteer-core/lib/cjs/puppeteer/common/Frame'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

import cleanUp from './scripts/cleanUpSerializationSelector'
import { ELEMENT_KEY, SERIALIZE_PROPERTY, SERIALIZE_FLAG, ERROR_MESSAGES, PPTR_LOG_PREFIX } from './constants'
Expand Down Expand Up @@ -110,10 +110,10 @@ export async function findElement (
await waitForFn.call(context, value, { timeout: implicitTimeout })
}

let element
let element: ElementHandle<Element> | null = null
try {
element = using === 'xpath'
? (await context.$x(value))[0]
? (await context.$x(value))[0] as ElementHandle<Element>
: await context.$(value)
} catch (err: any) {
/**
Expand Down Expand Up @@ -151,7 +151,7 @@ export async function findElements (
}

const elements = using === 'xpath'
? await context.$x(value)
? await context.$x(value) as ElementHandle<Element>[]
: await context.$$(value)

if (elements.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wdio-browserstack-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default class BrowserstackService implements Services.ServiceInstance {
log.info(`Update (reloaded) job with sessionId ${oldSessionId}, ${status}`)
} else {
const browserName = (this._browser as MultiRemoteBrowser<'async'>).instances.filter(
(browserName) => this._browser && (this._browser as MultiRemoteBrowser<'async'>)[browserName].sessionId === newSessionId)[0]
(browserName: string) => this._browser && (this._browser as MultiRemoteBrowser<'async'>)[browserName].sessionId === newSessionId)[0]
log.info(`Update (reloaded) multiremote job for browser "${browserName}" and sessionId ${oldSessionId}, ${status}`)
}

Expand Down Expand Up @@ -241,7 +241,7 @@ export default class BrowserstackService implements Services.ServiceInstance {
}

return Promise.all(_browser.instances
.filter(browserName => {
.filter((browserName: string) => {
const cap = getBrowserCapabilities(_browser, (this._caps as Capabilities.MultiRemoteCapabilities), browserName)
return isBrowserstackCapability(cap)
})
Expand Down
4 changes: 3 additions & 1 deletion packages/wdio-devtools-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/core": "^7.12.10",
"@tracerbench/trace-event": "^7.0.0",
"@types/node": "^18.0.0",
"@types/ws": "^8.5.3",
"@wdio/logger": "7.26.0",
"@wdio/types": "7.26.0",
"babel-plugin-istanbul": "^6.0.0",
Expand All @@ -41,7 +42,8 @@
"puppeteer-core": "^19.4.0",
"speedline": "^1.4.1",
"stable": "^0.1.8",
"webdriverio": "7.27.0"
"webdriverio": "7.27.0",
"ws": "^8.8.1"
},
"peerDependencies": {
"@wdio/cli": "^7.0.0"
Expand Down
5 changes: 4 additions & 1 deletion packages/wdio-devtools-service/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Browser, MultiRemoteBrowser } from 'webdriverio'

import type { TraceEvent } from '@tracerbench/trace-event'
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { TracingOptions } from 'puppeteer-core/lib/cjs/puppeteer/common/Tracing'

import NetworkHandler, { RequestPayload } from './handler/network'
Expand Down Expand Up @@ -102,6 +102,9 @@ export default class CommandHandler {

try {
const traceBuffer = await this._page.tracing.stop()
if (!traceBuffer) {
throw new Error('No tracebuffer captured')
}
this._traceEvents = JSON.parse(traceBuffer.toString('utf8'))
this._isTracing = false
} catch (err: any) {
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/src/gatherer/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import reports from 'istanbul-reports'

import logger from '@wdio/logger'

import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'

import type { CoverageReporterOptions, Coverage } from '../types'
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/src/gatherer/pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ViewportDimensions from 'lighthouse/lighthouse-core/gather/gatherers/view
import serviceWorkers from 'lighthouse/lighthouse-core/gather/driver/service-workers'

import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

import collectMetaElements from '../scripts/collectMetaElements'
import { NETWORK_RECORDER_EVENTS } from '../constants'
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/src/gatherer/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type Protocol from 'devtools-protocol'
import type { TraceEvent, TraceEventArgs } from '@tracerbench/trace-event'
import type { HTTPRequest } from 'puppeteer-core/lib/cjs/puppeteer/common/HTTPRequest'
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

import registerPerformanceObserverInPage from '../scripts/registerPerformanceObserverInPage'

Expand Down
38 changes: 21 additions & 17 deletions packages/wdio-devtools-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logger from '@wdio/logger'
import puppeteerCore from 'puppeteer-core'
import { KnownDevices } from 'puppeteer-core'
import WebSocket from 'ws'

import type { Browser, MultiRemoteBrowser } from 'webdriverio'
import type { Capabilities, Services, FunctionProperties, ThenArg } from '@wdio/types'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Browser as PuppeteerBrowser } from 'puppeteer-core/lib/cjs/puppeteer/common/Browser'
import type { Browser as PuppeteerBrowser } from 'puppeteer-core/lib/cjs/puppeteer/api/Browser'
import type { Target } from 'puppeteer-core/lib/cjs/puppeteer/common/Target'

import CommandHandler from './commands'
Expand Down Expand Up @@ -166,10 +167,10 @@ export default class DevToolsService implements Services.ServiceInstance {
}

if (typeof device === 'string') {
const deviceName = device + (inLandscape ? ' landscape' : '')
const deviceCapabilities = puppeteerCore.devices[deviceName]
const deviceName = device + (inLandscape ? ' landscape' : '') as keyof typeof KnownDevices
const deviceCapabilities = KnownDevices[deviceName]
if (!deviceCapabilities) {
const deviceNames = Object.values(puppeteerCore.devices as any)
const deviceNames = Object.values(KnownDevices)
.map((device: Device) => device.name)
.filter((device: string) => !device.endsWith('landscape'))
throw new Error(`Unknown device, available options: ${deviceNames.join(', ')}`)
Expand Down Expand Up @@ -225,7 +226,7 @@ export default class DevToolsService implements Services.ServiceInstance {

this._target = await this._puppeteer.waitForTarget(
/* istanbul ignore next */
(t) => t.type() === 'page' || t['_targetInfo'].browserContextId)
(t) => t.type() === 'page' || Boolean(t._getTargetInfo().browserContextId))
/* istanbul ignore next */
if (!this._target) {
throw new Error('No page target found')
Expand Down Expand Up @@ -267,16 +268,8 @@ export default class DevToolsService implements Services.ServiceInstance {
}

this._devtoolsGatherer = new DevtoolsGatherer()
this._puppeteer['_connection']._transport._ws.addEventListener('message', (event: { data: string }) => {
const data: CDPSessionOnMessageObject = JSON.parse(event.data)
this._devtoolsGatherer?.onMessage(data)
const method = data.method || 'event'
log.debug(`cdp event: ${method} with params ${JSON.stringify(data.params)}`)

if (this._browser) {
this._browser.emit(method, data.params)
}
})
const cdpWS = new WebSocket(this._puppeteer.wsEndpoint())
cdpWS.on('message', this._propagateWSEvents.bind(this))

this._browser.addCommand('enablePerformanceAudits', this._enablePerformanceAudits.bind(this))
this._browser.addCommand('disablePerformanceAudits', this._disablePerformanceAudits.bind(this))
Expand All @@ -285,6 +278,17 @@ export default class DevToolsService implements Services.ServiceInstance {
this._pwaGatherer = new PWAGatherer(this._session, this._page, this._driver)
this._browser.addCommand('checkPWA', this._checkPWA.bind(this))
}

private _propagateWSEvents (event: { data: string }) {
const data: CDPSessionOnMessageObject = JSON.parse(event.data)
this._devtoolsGatherer?.onMessage(data)
const method = data.method || 'event'
log.debug(`cdp event: ${method} with params ${JSON.stringify(data.params)}`)

if (this._browser) {
this._browser.emit(method, data.params)
}
}
}

export * from './types'
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/tests/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

import CommandHandler from '../src/commands'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CoverageGatherer from '../../src/gatherer/coverage'
import libReport from 'istanbul-lib-report'
import reports from 'istanbul-reports'
import { transformAsync as babelTransform } from '@babel/core'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

jest.useFakeTimers()
jest.spyOn(global, 'setInterval')
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/tests/gatherer/pwa.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'

import PWAGatherer from '../../src/gatherer/pwa'
import type { GathererDriver } from '../../src/types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Protocol from 'devtools-protocol'
import type { CDPSession } from 'puppeteer-core/lib/cjs/puppeteer/common/Connection'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import type { Page } from 'puppeteer-core/lib/cjs/puppeteer/api/Page'
import type { HTTPRequest } from 'puppeteer-core/lib/cjs/puppeteer/common/HTTPRequest'

import TraceGatherer from '../../src/gatherer/trace'
Expand Down

0 comments on commit 4609583

Please sign in to comment.