Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

provide ability to listen to network events being made and received #177

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ const chromeless = new Chromeless({
- [`clearCookies()`](docs/api.md#api-clearcookies)
- [`clearInput(selector: string)`](docs/api.md#api-clearInput)
- [`setFileInput(selector: string, files: string | string[])`](docs/api.md#api-set-file-input)
- [`onRequestWillBeSent()`](#api-onrequestwillbesent)
- [`onDataReceived()`](#api-ondatareceived)
- [`onRequestServedFromCache()`](#api-onrequestservedfromcache)
- [`onLoadingFinished()`](#api-onloadingfinished)
- [`onLoadingFailed()`](#api-onloadingfailed)
- [`onRequestIntercepted()`](#api-onrequestintercepted)
- [`onResponseReceived()`](#api-onresponsereceived)

## Configuring Development Environment

Expand Down
133 changes: 133 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Chromeless provides TypeScript typings.
- [`setCookies(cookies: Cookie[])`](#api-setcookies-many)
- [`deleteCookies(name: string)`](#api-deletecookies)
- [`clearCookies()`](#api-clearcookies)
- [`onRequestWillBeSent()`](#api-onrequestwillbesent)
- [`onDataReceived()`](#api-ondatareceived)
- [`onRequestServedFromCache()`](#api-onrequestservedfromcache)
- [`onLoadingFinished()`](#api-onloadingfinished)
- [`onLoadingFailed()`](#api-onloadingfailed)
- [`onRequestIntercepted()`](#api-onrequestintercepted)
- [`onResponseReceived()`](#api-onresponsereceived)


---------------------------------------
Expand Down Expand Up @@ -682,3 +689,129 @@ __Example__
```js
await chromeless.setFileInput('.uploader', '/User/Me/Documents/img.jpg')
```

---------------------------------------

<a name="api-onrequestwillbesent" />

### onRequestWillBeSent(processFunction: void): Chromeless<T>

Listen for `requestWillBeSent` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-requestWillBeSent) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onRequestWillBeSent(rqst => console.log(rqst))
```

---------------------------------------

<a name="api-ondatareceived" />

### onDataReceived(processFunction: void): Chromeless<T>

Listen for `dataReceived` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-dataReceived) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onDataReceived(resp => console.log(resp))
```

---------------------------------------

<a name="api-onrequestservedfromcache" />

### onRequestServedFromCache(processFunction: void): Chromeless<T>

Listen for `requestServedFromCache` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-requestServedFromCache) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onRequestServedFromCache(resp => console.log(resp))
```

---------------------------------------

<a name="api-onloadingfinished" />

### onLoadingFinished(processFunction: void): Chromeless<T>

Listen for `loadingFinished` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-loadingFinished) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onLoadingFinished(resp => console.log(resp))
```

---------------------------------------

<a name="api-onloadingfailed" />

### onLoadingFailed(processFunction: void): Chromeless<T>

Listen for `loadingFailed` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-loadingFailed) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onLoadingFailed(resp => console.log(resp))
```

---------------------------------------

<a name="api-onrequestintercepted" />

### onRequestIntercepted(processFunction: void): Chromeless<T>

Listen for `requestIntercepted` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-requestIntercepted) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onRequestIntercepted(resp => console.log(resp))
```

---------------------------------------

<a name="api-onresponsereceived" />

### onResponseReceived(processFunction: void): Chromeless<T>

Listen for `responseReceived` events to capture and inspect them.
See devtools [documentation](https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-responseReceived) for more information.

__Arguments__
- `processFunction` - a callback function to inspect the event properties

__Example__

```js
await chromeless.onResponseReceived(resp => console.log(resp))
```
42 changes: 42 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,48 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

onRequestWillBeSent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onRequestWillBeSent', func})

return this
}

onResponseReceived(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onResponseReceived', func})

return this
}

onRequestServedFromCache(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onRequestServedFromCache', func})

return this
}

onDataReceived(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onDataReceived', func})

return this
}

onLoadingFinished(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onLoadingFinished', func})

return this
}

onLoadingFailed(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onLoadingFailed', func})

return this
}

onRequestIntercepted(func: void): Chromeless<T> {
this.queue.enqueue({type: 'onRequestIntercepted', func})

return this
}

async end(): Promise<T> {
const result = await this.lastReturnPromise
await this.queue.end()
Expand Down
58 changes: 58 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,69 @@ export default class LocalRuntime {
return this.clearInput(command.selector)
case 'setFileInput':
return this.setFileInput(command.selector, command.files)
case 'onRequestWillBeSent':
return this.onRequestWillBeSent(command.func)
case 'onDataReceived':
return this.onDataReceived(command.func)
case 'onRequestServedFromCache':
return this.onRequestServedFromCache(command.func)
case 'onDataReceived':
return this.onDataReceived(command.func)
case 'onLoadingFinished':
return this.onLoadingFinished(command.func)
case 'onLoadingFailed':
return this.onLoadingFailed(command.func)
case 'onRequestIntercepted':
return this.onRequestIntercepted(command.func)
case 'onResponseReceived':
return this.onResponseReceived(command.func)
default:
throw new Error(`No such command: ${JSON.stringify(command)}`)
}
}

private async onRequestWillBeSent(func: void): Promise<void> {
const { Network } = this.client
Network.requestWillBeSent(func)
console.log('Added requestWillBeSent eventlistener')
}

private async onDataReceived(func: void): Promise<void> {
const { Network } = this.client
Network.dataReceived(func)
console.log('Added dateReceived eventlistener')
}

private async onRequestServedFromCache(func: void): Promise<void> {
const { Network } = this.client
Network.requestServedFromCache(func)
console.log('Added requestServedFromCache eventlistener')
}

private async onLoadingFinished(func: void): Promise<void> {
const { Network } = this.client
Network.loadingFinished(func)
console.log('Added loadingFinished eventlistener')
}

private async onLoadingFailed(func: void): Promise<void> {
const { Network } = this.client
Network.loadingFailed(func)
console.log('Added loadingFailed eventlistener')
}

private async onRequestIntercepted(func: void): Promise<void> {
const { Network } = this.client
Network.requestIntercepted(func)
console.log('Added requestIntercepted eventlistener')
}

private async onResponseReceived(func: void): Promise<void> {
const { Network } = this.client
Network.responseReceived(func)
console.log('Added responseReceived eventlistener')
}

private async goto(url: string): Promise<void> {
const { Network, Page } = this.client
await Promise.all([Network.enable(), Page.enable()])
Expand Down
38 changes: 35 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,41 @@ export type Command =
selector: string
}
| {
type: 'setFileInput'
selector: string
files: string[]
type: 'setFileInput'
selector: string
files: string[]
}
| {
type: 'clearInput'
selector: string
}
| {
type: 'onRequestWillBeSent'
func: void
}
| {
type: 'onDataReceived'
func: void
}
| {
type: 'onRequestServedFromCache'
func: void
}
| {
type: 'onLoadingFinished'
func: void
}
| {
type: 'onLoadingFailed'
func: void
}
| {
type: 'onRequestIntercepted'
func: void
}
| {
type: 'onResponseReceived'
func: void
}

export interface Cookie {
Expand Down