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

setHtml() and getHtml() API #112

Merged
merged 7 commits into from
Aug 1, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- CODE_OF_CONDUCT.md, CONTRIBUTING.md
- `getHtml()` and `setHtml()` APIs. [#112](https://github.com/graphcool/chromeless/pull/112), [#74](https://github.com/graphcool/chromeless/issues/74)

### Changed
- `.evaluate()` now returns the resulting value or a Promise [#110](https://github.com/graphcool/chromeless/pull/110) @joelgriffith
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ const chromeless = new Chromeless({
- [`mousedown()`](docs/api.md#api-mousedown) - Not implemented yet
- [`mouseup()`](docs/api.md#api-mouseup) - Not implemented yet
- [`scrollTo(x: number, y: number)`](docs/api.md#api-scrollto)
- [`setHtml(html: string)`](docs/api.md#api-sethtml)
- [`viewport(width: number, height: number)`](docs/api.md#api-viewport)
- [`evaluate<U extends any>(fn: (...args: any[]) => void, ...args: any[])`](docs/api.md#api-evaluate)
- [`inputValue(selector: string)`](docs/api.md#api-inputvalue)
- [`exists(selector: string)`](docs/api.md#api-exists)
- [`screenshot()`](docs/api.md#api-screenshot)
- [`pdf()`](docs/api.md#api-pdf) - Not implemented yet
- [`getHtml()`](docs/api.md#api-gethtml)
- [`cookiesGet()`](docs/api.md#api-cookiesget)
- [`cookiesGet(name: string)`](docs/api.md#api-cookiesget-name)
- [`cookiesGet(query: CookieQuery)`](docs/api.md#api-cookiesget-query) - Not implemented yet
Expand Down
37 changes: 37 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ Chromeless provides TypeScript typings.
- [`mousedown()`](#api-mousedown) - Not implemented yet
- [`mouseup()`](#api-mouseup) - Not implemented yet
- [`scrollTo(x: number, y: number)`](#api-scrollto)
- [`setHtml(html: string)`](#api-sethtml)
- [`viewport(width: number, height: number)`](#api-viewport)
- [`evaluate<U extends any>(fn: (...args: any[]) => void, ...args: any[])`](#api-evaluate)
- [`inputValue(selector: string)`](#api-inputvalue)
- [`exists(selector: string)`](#api-exists)
- [`screenshot()`](#api-screenshot)
- [`pdf()`](#api-pdf) - Not implemented yet
- [`getHtml()`](#api-gethtml)
- [`cookiesGet()`](#api-cookiesget)
- [`cookiesGet(name: string)`](#api-cookiesget-name)
- [`cookiesGet(query: CookieQuery)`](#api-cookiesget-query) - Not implemented yet
Expand Down Expand Up @@ -254,6 +256,23 @@ await chromeless.scrollTo(500, 0)

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

<a name="api-sethtml" />

### setHtml(html: string): Chromeless<T>

Sets given markup as the document's HTML.

__Arguments__
- `html` - HTML to set as the document's markup.

__Example__

```js
await chromeless.setHtml('<h1>Hello world!</h1>')
```

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

<a name="api-viewport" />

### viewport(width: number, height: number)
Expand Down Expand Up @@ -359,6 +378,24 @@ Not implemented yet

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

<a name="api-gethtml" />

### getHtml(): Chromeless<string>

Get full HTML of the loaded page.

__Example__

```js
const html = await chromeless
.setHtml('<h1>Hello world!</h1>')
.getHtml()

console.log(html) // <html><head></head><body><h1>Hello world!</h1></body></html>
```

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

<a name="api-cookiesget" />

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

setHtml(html: string): Chromeless<T> {
this.queue.enqueue({type: 'setHtml', html})

return this
}

viewport(width: number, height: number): Chromeless<T> {
throw new Error('Not implemented yet')
}
Expand Down Expand Up @@ -170,6 +176,12 @@ export default class Chromeless<T extends any> implements Promise<T> {
return new Chromeless<string>({}, this)
}

getHtml(): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({type: 'returnHtml'})

return new Chromeless<string>({}, this)
}

/**
* Get the cookies for the current url
*/
Expand Down
16 changes: 15 additions & 1 deletion src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
click,
evaluate,
screenshot,
getHtml,
type,
getValue,
scrollTo,
setHtml,
press,
clearCookies,
getCookies,
Expand Down Expand Up @@ -49,6 +51,8 @@ export default class LocalRuntime {
return this.returnExists(command.selector)
case 'returnScreenshot':
return this.returnScreenshot()
case 'returnHtml':
return this.returnHtml()
case 'returnInputValue':
return this.returnInputValue(command.selector)
case 'type':
Expand All @@ -57,6 +61,8 @@ export default class LocalRuntime {
return this.press(command.keyCode, command.count, command.modifiers)
case 'scrollTo':
return this.scrollTo(command.x, command.y)
case 'setHtml':
return this.setHtml(command.html)
case 'cookiesClearAll':
return this.cookiesClearAll()
case 'cookiesGet':
Expand Down Expand Up @@ -114,6 +120,10 @@ export default class LocalRuntime {
return scrollTo(this.client, x, y)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the intent of this private handler is for?

Copy link
Contributor Author

@seangransee seangransee Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the same pattern as similar methods:

private async goto(url: string): Promise<void> { ... }
private async click(selector: string): Promise<void> { ... }
private async scrollTo<T>(x: number, y: number): Promise<void> { ... }
private async setHtml(html: string): Promise<void> { ... } // new method in this PR

private async setHtml(html: string): Promise<void> {
await setHtml(this.client, html)
}

async type(text: string, selector?: string): Promise<void> {
if (selector) {
if (this.chromlessOptions.implicitWait) {
Expand Down Expand Up @@ -208,10 +218,14 @@ export default class LocalRuntime {
}
}

async returnHtml(): Promise<string> {
return await getHtml(this.client)
}

private log(msg: string): void {
if (this.chromlessOptions.debug) {
console.log(msg)
}
}

}
}
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Client {
Network: any
Page: any
DOM: any
Input: any
Runtime: any
Emulation: any
Expand Down Expand Up @@ -76,11 +77,18 @@ export type Command =
| {
type: 'returnScreenshot'
}
| {
type: 'returnHtml'
}
| {
type: 'scrollTo'
x: number
y: number
}
| {
type: 'setHtml',
html: string
}
| {
type: 'press'
keyCode: number
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ export async function scrollTo(client: Client, x: number, y: number): Promise<vo
})
}

export async function setHtml(client: Client, html: string): Promise<void> {
const {Page} = client

const {frameTree: {frame: {id: frameId}}} = await Page.getResourceTree()
await Page.setDocumentContent({frameId, html})
}

export async function getCookies(client: Client, nameOrQuery?: string | Cookie): Promise<any> {
if (nameOrQuery) {
throw new Error('Not yet implemented')
Expand Down Expand Up @@ -290,6 +297,14 @@ export async function screenshot(client: Client): Promise<string> {
return screenshot.data
}

export async function getHtml(client: Client): Promise<string> {
const {DOM} = client

const {root: {nodeId}} = await DOM.getDocument()
const {outerHTML} = await DOM.getOuterHTML({nodeId})
return outerHTML
}

export function getDebugOption(): boolean {
if (process && process.env && process.env['DEBUG'] && process.env['DEBUG'].includes('chromeless')) {
return true
Expand Down