From 5ddb9fdd8cc42f89539c9375614d7072a6f7c3f7 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Mon, 31 Jul 2017 14:10:01 -0500 Subject: [PATCH 1/7] implement setDocumentContent() API --- docs/api.md | 18 ++++++++++++++++++ src/api.ts | 6 ++++++ src/chrome/local-runtime.ts | 9 ++++++++- src/types.ts | 4 ++++ src/util.ts | 7 +++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 7a750826..23dc3196 100644 --- a/docs/api.md +++ b/docs/api.md @@ -20,6 +20,7 @@ Chromeless provides TypeScript typings. - [`mousedown()`](#api-mousedown) - Not implemented yet - [`mouseup()`](#api-mouseup) - Not implemented yet - [`scrollTo(x: number, y: number)`](#api-scrollto) +- [`setDocumentContent(html: string)`](#api-setdocumentcontent) - [`viewport(width: number, height: number)`](#api-viewport) - [`evaluate(fn: (...args: any[]) => void, ...args: any[])`](#api-evaluate) - [`inputValue(selector: string)`](#api-inputvalue) @@ -254,6 +255,23 @@ await chromeless.scrollTo(500, 0) --------------------------------------- + + +### setDocumentContent(html: string): Chromeless + +Sets given markup as the document's HTML. + +__Arguments__ +- `html` - HTML to set as the document's markup. + +__Example__ + +```js +await chromeless.setDocumentContent('

Hello world!

') +``` + +--------------------------------------- +
### viewport(width: number, height: number) diff --git a/src/api.ts b/src/api.ts index 75d8c184..b5eeb71e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -142,6 +142,12 @@ export default class Chromeless implements Promise { return this } + setDocumentContent(html: string): Chromeless { + this.queue.enqueue({type: 'setDocumentContent', html}) + + return this + } + viewport(width: number, height: number): Chromeless { throw new Error('Not implemented yet') } diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index 8060be57..8f79fe39 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -12,6 +12,7 @@ import { type, getValue, scrollTo, + setDocumentContent, press, clearCookies, getCookies, @@ -57,6 +58,8 @@ export default class LocalRuntime { return this.press(command.keyCode, command.count, command.modifiers) case 'scrollTo': return this.scrollTo(command.x, command.y) + case 'setDocumentContent': + return this.setDocumentContent(command.html) case 'cookiesClearAll': return this.cookiesClearAll() case 'cookiesGet': @@ -114,6 +117,10 @@ export default class LocalRuntime { return scrollTo(this.client, x, y) } + private async setDocumentContent(html: string): Promise { + await setDocumentContent(this.client, html) + } + async type(text: string, selector?: string): Promise { if (selector) { if (this.chromlessOptions.implicitWait) { @@ -214,4 +221,4 @@ export default class LocalRuntime { } } -} \ No newline at end of file +} diff --git a/src/types.ts b/src/types.ts index f0bc1ac5..5ed8d6a8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,6 +81,10 @@ export type Command = x: number y: number } + | { + type: 'setDocumentContent', + html: string + } | { type: 'press' keyCode: number diff --git a/src/util.ts b/src/util.ts index 1b54a855..b410a63d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -238,6 +238,13 @@ export async function scrollTo(client: Client, x: number, y: number): Promise { + 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 { if (nameOrQuery) { throw new Error('Not yet implemented') From 42f51aa5660aefd0a0d60dbb470393b72babf974 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Mon, 31 Jul 2017 15:07:06 -0500 Subject: [PATCH 2/7] rename setDocumentContent() to setHtml() --- src/api.ts | 4 ++-- src/chrome/local-runtime.ts | 10 +++++----- src/types.ts | 2 +- src/util.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/api.ts b/src/api.ts index b5eeb71e..4ef3a951 100644 --- a/src/api.ts +++ b/src/api.ts @@ -142,8 +142,8 @@ export default class Chromeless implements Promise { return this } - setDocumentContent(html: string): Chromeless { - this.queue.enqueue({type: 'setDocumentContent', html}) + setHtml(html: string): Chromeless { + this.queue.enqueue({type: 'setHtml', html}) return this } diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index 8f79fe39..830e95ac 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -12,7 +12,7 @@ import { type, getValue, scrollTo, - setDocumentContent, + setHtml, press, clearCookies, getCookies, @@ -58,8 +58,8 @@ export default class LocalRuntime { return this.press(command.keyCode, command.count, command.modifiers) case 'scrollTo': return this.scrollTo(command.x, command.y) - case 'setDocumentContent': - return this.setDocumentContent(command.html) + case 'setHtml': + return this.setHtml(command.html) case 'cookiesClearAll': return this.cookiesClearAll() case 'cookiesGet': @@ -117,8 +117,8 @@ export default class LocalRuntime { return scrollTo(this.client, x, y) } - private async setDocumentContent(html: string): Promise { - await setDocumentContent(this.client, html) + private async setHtml(html: string): Promise { + await setHtml(this.client, html) } async type(text: string, selector?: string): Promise { diff --git a/src/types.ts b/src/types.ts index 5ed8d6a8..ea86dcd7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -82,7 +82,7 @@ export type Command = y: number } | { - type: 'setDocumentContent', + type: 'setHtml', html: string } | { diff --git a/src/util.ts b/src/util.ts index b410a63d..deb7ceb1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -238,7 +238,7 @@ export async function scrollTo(client: Client, x: number, y: number): Promise { +export async function setHtml(client: Client, html: string): Promise { const {Page} = client const {frameTree: {frame: {id: frameId}}} = await Page.getResourceTree() From 95f4f34e4725d18df9ecd62ce0fa33b66c1ac5d6 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Mon, 31 Jul 2017 15:33:32 -0500 Subject: [PATCH 3/7] implement getHtml() --- docs/api.md | 19 +++++++++++++++++++ src/api.ts | 6 ++++++ src/chrome/local-runtime.ts | 7 +++++++ src/types.ts | 4 ++++ src/util.ts | 8 ++++++++ 5 files changed, 44 insertions(+) diff --git a/docs/api.md b/docs/api.md index 23dc3196..a8ad73b3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -27,6 +27,7 @@ Chromeless provides TypeScript typings. - [`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 @@ -377,6 +378,24 @@ Not implemented yet --------------------------------------- + + +### getHtml(): Chromeless + +Get full HTML of the loaded page. + +__Example__ + +```js +const html = await chromeless + .setHtml('

Hello world!

') + .getHtml() + +console.log(html) //

Hello world!

+``` + +--------------------------------------- +
### cookiesGet(): Chromeless diff --git a/src/api.ts b/src/api.ts index 4ef3a951..49714299 100644 --- a/src/api.ts +++ b/src/api.ts @@ -176,6 +176,12 @@ export default class Chromeless implements Promise { return new Chromeless({}, this) } + getHtml(): Chromeless { + this.lastReturnPromise = this.queue.process({type: 'returnHtml'}) + + return new Chromeless({}, this) + } + /** * Get the cookies for the current url */ diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index 830e95ac..1d2d76dd 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -9,6 +9,7 @@ import { click, evaluate, screenshot, + html, type, getValue, scrollTo, @@ -50,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': @@ -215,6 +218,10 @@ export default class LocalRuntime { } } + async returnHtml(): Promise { + return await html(this.client) + } + private log(msg: string): void { if (this.chromlessOptions.debug) { console.log(msg) diff --git a/src/types.ts b/src/types.ts index ea86dcd7..7a9b25d7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,7 @@ export interface Client { Network: any Page: any + DOM: any Input: any Runtime: any Emulation: any @@ -76,6 +77,9 @@ export type Command = | { type: 'returnScreenshot' } + | { + type: 'returnHtml' + } | { type: 'scrollTo' x: number diff --git a/src/util.ts b/src/util.ts index deb7ceb1..d87ccab6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -297,6 +297,14 @@ export async function screenshot(client: Client): Promise { return screenshot.data } +export async function html(client: Client): Promise { + 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 From dfd446f9f96685c7911e4bf9b61d37ef24328eb4 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Mon, 31 Jul 2017 15:42:47 -0500 Subject: [PATCH 4/7] fix docs --- docs/api.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/api.md b/docs/api.md index a8ad73b3..43e1bcb5 100644 --- a/docs/api.md +++ b/docs/api.md @@ -20,7 +20,7 @@ Chromeless provides TypeScript typings. - [`mousedown()`](#api-mousedown) - Not implemented yet - [`mouseup()`](#api-mouseup) - Not implemented yet - [`scrollTo(x: number, y: number)`](#api-scrollto) -- [`setDocumentContent(html: string)`](#api-setdocumentcontent) +- [`setHtml(html: string)`](#api-sethtml) - [`viewport(width: number, height: number)`](#api-viewport) - [`evaluate(fn: (...args: any[]) => void, ...args: any[])`](#api-evaluate) - [`inputValue(selector: string)`](#api-inputvalue) @@ -256,9 +256,9 @@ await chromeless.scrollTo(500, 0) --------------------------------------- - + -### setDocumentContent(html: string): Chromeless +### setHtml(html: string): Chromeless Sets given markup as the document's HTML. @@ -268,7 +268,7 @@ __Arguments__ __Example__ ```js -await chromeless.setDocumentContent('

Hello world!

') +await chromeless.setHtml('

Hello world!

') ``` --------------------------------------- From a9fdcf33886c2b8122b4f45dc82e452381d99f65 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Mon, 31 Jul 2017 15:52:53 -0500 Subject: [PATCH 5/7] rename html() to getHtml() --- src/chrome/local-runtime.ts | 4 ++-- src/util.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index 1d2d76dd..38d8de5f 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -9,7 +9,7 @@ import { click, evaluate, screenshot, - html, + getHtml, type, getValue, scrollTo, @@ -219,7 +219,7 @@ export default class LocalRuntime { } async returnHtml(): Promise { - return await html(this.client) + return await getHtml(this.client) } private log(msg: string): void { diff --git a/src/util.ts b/src/util.ts index d87ccab6..8434eaa6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -297,7 +297,7 @@ export async function screenshot(client: Client): Promise { return screenshot.data } -export async function html(client: Client): Promise { +export async function getHtml(client: Client): Promise { const {DOM} = client const {root: {nodeId}} = await DOM.getDocument() From e1d8b47d383efadf21863b01c5b9ebda42cfebb6 Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Tue, 1 Aug 2017 09:14:40 -0500 Subject: [PATCH 6/7] update the API TOC in the project's main README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7b3f11fa..34938203 100644 --- a/README.md +++ b/README.md @@ -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(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 From bee325a87d84b5699a1317246e78269d5f47513b Mon Sep 17 00:00:00 2001 From: Sean Gransee Date: Tue, 1 Aug 2017 09:20:44 -0500 Subject: [PATCH 7/7] add to Unreleased section of CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 832d506b..e478090a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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