Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wdio-devtools-service: adding debuggerAddress parameter #3841

Merged
merged 2 commits into from Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/wdio-devtools-service/README.md
Expand Up @@ -35,10 +35,13 @@ In order to use the service you just need to add the service to your service lis
// wdio.conf.js
export.config = {
// ...
services: ['devtools'],
services: [['devtools', {
debuggerAddress: '10.0.0.3:9222'
}]],
// ...
};
```
- `debuggerAddress` - optional parameter, you could set host and port.

## Usage

Expand Down
13 changes: 10 additions & 3 deletions packages/wdio-devtools-service/src/index.js
Expand Up @@ -7,7 +7,8 @@ const log = logger('@wdio/devtools-service')
const UNSUPPORTED_ERROR_MESSAGE = 'The @wdio/devtools-service currently only supports Chrome version 63 and up'

export default class DevToolsService {
constructor () {
constructor (options = {}) {
this.options = options
this.isSupported = false
}

Expand All @@ -27,8 +28,14 @@ export default class DevToolsService {
}

try {
const { host, port } = await findCDPInterface()
const client = await getCDPClient(host, port)
let debuggerAddress
if (this.options.debuggerAddress) {
const [host, port] = this.options.debuggerAddress.split(':')
debuggerAddress = { host, port: parseInt(port, 10) }
} else {
debuggerAddress = await findCDPInterface()
}
const client = await getCDPClient(debuggerAddress)
this.commandHandler = new CommandHandler(client, global.browser)
} catch (err) {
log.error(`Couldn't connect to chrome: ${err.stack}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/src/utils.js
Expand Up @@ -42,7 +42,7 @@ export async function findCDPInterface () {
return { host: 'localhost', port }
}

export function getCDPClient (host, port) {
export function getCDPClient ({ host, port }) {
return new Promise((resolve) => CDP({
host,
port,
Expand Down
14 changes: 13 additions & 1 deletion packages/wdio-devtools-service/tests/service.test.js
@@ -1,6 +1,7 @@
import DevToolsService from '../src'

import logger from '@wdio/logger'
import { getCDPClient } from '../src/utils'

jest.mock('../src/commands', () => {
class CommandHandlerMock {
Expand Down Expand Up @@ -52,14 +53,25 @@ test('if not supported by browser', async () => {
})

test('if supported by browser', async () => {

const service = new DevToolsService()
service.isSupported = true
await service.before()
expect(service.commandHandler.cdp).toBeCalledWith('Network', 'enable')
expect(service.commandHandler.cdp).toBeCalledWith('Page', 'enable')
})

test('initialised with the debuggerAddress as option', async () => {
const options = {
debuggerAddress: 'localhost:4444'
}
const service = new DevToolsService(options)
service.isSupported = true
await service.before()
expect(getCDPClient).toBeCalledWith({ host: 'localhost', port: 4444 })
expect(service.commandHandler.cdp).toBeCalledWith('Network', 'enable')
expect(service.commandHandler.cdp).toBeCalledWith('Page', 'enable')
})

test('initialization fails', async () => {
jest.mock('../src/utils', () => ({
findCDPInterface: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/wdio-devtools-service/tests/utils.test.js
Expand Up @@ -11,7 +11,7 @@ jest.mock('fs', () => ({
}))

test('getCDPClient', async () => {
const cdp = await getCDPClient('localhost', 1234)
const cdp = await getCDPClient({ host: 'localhost', port: 1234 })
expect(CDP.mock.calls).toHaveLength(1)
expect(CDP.mock.calls[0][0].port).toBe(1234)
expect(CDP.mock.calls[0][0].host).toBe('localhost')
Expand Down