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

provide .clearCache() functionality #122

Merged
merged 8 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const chromeless = new Chromeless({
- [`wait(timeout: number)`](docs/api.md#api-wait-timeout)
- [`wait(selector: string)`](docs/api.md#api-wait-selector)
- [`wait(fn: (...args: any[]) => boolean, ...args: any[])`] - Not implemented yet
- [`clearCache()`](docs/api.md#api-clearcache)
- [`focus(selector: string)`](docs/api.md#api-focus)
- [`press(keyCode: number, count?: number, modifiers?: any)`](docs/api.md#api-press)
- [`type(input: string, selector?: string)`](docs/api.md#api-type)
Expand Down
15 changes: 15 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Chromeless provides TypeScript typings.
- [`wait(timeout: number)`](#api-wait-timeout)
- [`wait(selector: string)`](#api-wait-selector)
- [`wait(fn: (...args: any[]) => boolean, ...args: any[])`] - Not implemented yet
- [`clearCache()`](docs/api.md#api-clearcache)
- [`focus(selector: string)`](#api-focus)
- [`press(keyCode: number, count?: number, modifiers?: any)`](#api-press)
- [`type(input: string, selector?: string)`](#api-type)
Expand Down Expand Up @@ -160,6 +161,20 @@ await chromeless.wait(() => { return console.log('@TODO: put a better example he

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

<a name="api-clearcache" />

### clearCache(): Chromeless<T>

Clear browser cache.

__Example__

```js
await chromeless.clearCache()
```

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

<a name="api-focus" />

### focus(selector: string): Chromeless<T>
Expand Down
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

clearCache(): Chromeless<T> {
this.queue.enqueue({ type: 'clearCache' })

return this
}

focus(selector: string): Chromeless<T> {
this.queue.enqueue({ type: 'focus', selector })
return this
Expand Down
13 changes: 13 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class LocalRuntime {
throw new Error('waitFn not yet implemented')
}
}
case 'clearCache':
return this.clearCache()
case 'setUserAgent':
return this.setUserAgent(command.useragent)
case 'click':
Expand Down Expand Up @@ -115,6 +117,17 @@ export default class LocalRuntime {
this.log(`Navigated to ${url}`)
}

private async clearCache(): Promise<void> {
const {Network} = this.client
const canClearCache = await Network.canClearBrowserCache
if (canClearCache) {
await Network.clearBrowserCache()
this.log(`Cache is cleared`)
} else {
this.log(`Cache could not be cleared`)
}
}

private async setUserAgent(useragent: string): Promise<void> {
this.userAgentValue = useragent
await this.log(`Set useragent to ${this.userAgentValue}`)
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export type Command =
type: 'goto'
url: string
}
| {
type: 'clearCache'
}
| {
type: 'setViewport'
options: DeviceMetrics
Expand Down