From a8c3f37188b805c2556ff43fbd750e3e08bc8a5c Mon Sep 17 00:00:00 2001 From: Eugene Gusarov Date: Sat, 12 Mar 2016 01:35:53 +0300 Subject: [PATCH] fixed definition and tests --- phantom/phantom-tests.ts | 268 ++++++++++++++++++--------------------- phantom/phantom.d.ts | 42 +++--- 2 files changed, 145 insertions(+), 165 deletions(-) diff --git a/phantom/phantom-tests.ts b/phantom/phantom-tests.ts index 41d2b87b5..e53b7a1ff 100644 --- a/phantom/phantom-tests.ts +++ b/phantom/phantom-tests.ts @@ -3,164 +3,138 @@ import phantom = require("phantom"); phantom.create().then((ph: phantom.PhantomJS): void => { - ph.createPage().then((page): void => { - page.open("http://www.google.com").then((status: string) => { - console.log("opened google? ", status); - return page.evaluate((): string => { - return document.title; - }).then((result: string): void => { - console.log('Page title is ' + result); - ph.exit(); - }); + ph.createPage().then((page): void => { + page.open("http://www.google.com").then((status: string) => { + console.log("opened google? ", status); + return page.evaluate((): string => { + return document.title; + }); + }).then((result: string): void => { + console.log('Page title is ' + result); + ph.exit(); + }); }); - }); }); - -var _ph: phantom.PhantomJS; phantom.create(["--web-security=no", "--ignore-ssl-errors=yes"]).then((ph) => { - console.log("Phantom Bridge Initiated"); - _ph = ph; -}); -var _page: phantom.WebPage; -_ph.createPage().then((page) => { - console.log("Page created!"); - _page = page; -}); + console.log("Phantom Bridge Initiated"); + ph.createPage().then((page) => { + console.log("Page created!"); -_page.open("http://www.google.com").then(function (status) { - if (status == "success") { - console.log("Page is open!"); - _page.close(); - } -}); + return page.open("http://www.google.com").then(function(status) { + if (status == "success") { + console.log("Page is open!"); + } -_page.evaluate(function() { - var title = (document.querySelector("title")).innerText - console.log("The page title is " + title) -}) -_page.evaluate(function(selector) { - var text = (document.querySelector(selector)).innerText - console.log(selector + " contains the following text: " + text) -}, "title").then(function(result) {}) -_page.evaluate(function(selector) { - var text = (document.querySelector(selector)).innerText - return text -}, "title").then(function(result) { - console.log("The element contains the following text: " + result) -}); + return page.evaluate(function() { + var title = (document.querySelector("title")).innerText; + console.log("The page title is " + title); + }); + }).then(() => { + return page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText; + console.log(selector + " contains the following text: " + text); + }, "title"); + }).then(() => { + return page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText + return text + }) + }).then(function(result) { + console.log("The element contains the following text: " + result) - -_page.set('viewportSize', { width: 1920, height: 1080 }).then(function (result) { - console.log("Viewport set to: " + result.width + "x" + result.height); + return page.property('onConsoleMessage', function(msg: string) { + console.log("Phantom Console: " + msg); + }); + }).then(() => { + return page.property('onUrlChanged', function(url: string) { + console.log("New URL: " + url); + }); + }).then(() => { + return page.property('onResourceRequested', function() { + console.log("Resource requested.."); + }); + }).then(() => { + return page.property('onResourceReceived', function(res: any) { + if (res.stage == 'end') { + console.log("Resource received!") + } + }); + }).then(() => { + return page.property('onLoadStarted', function() { + console.log("Loading started"); + }); + }).then(() => { + return page.property('onLoadFinished', function(status: string) { + console.log("Loading finished, the page is " + ((status == "success") ? "open." : "not open!")); + }); + }).then(() => { + return page.property('settings.loadImages', false); + }).then(() => { + return page.property('settings.resourceTimeout', 1000); + }).then(() => { + return page.property('settings.viewportSize', { + width: 1920, + height: 1080 + }); + }).then(() => { + return page.open("http://google.co.jp"); + }).then((status) => { + return page.render("/tmp/google-top.jpg", { + format: 'jpeg', + quality: '80' + }); + }).then(() => { + return page.close(); + }); + }).then(() => { + ph.exit(); + }); }); -_page.set('onConsoleMessage', function (msg: string) { - console.log("Phantom Console: " + msg); -}); -_page.set('onUrlChanged', function(url: string) { - console.log("New URL: "+url); -}); -_page.set('onResourceRequested', function () { - console.log("Resource requested.."); -}); -_page.set('onResourceReceived', function (res: any) { - if (res.stage == 'end') { - console.log("Resource received!") - } -}); -_page.set('onLoadStarted', function () { - console.log("Loading started"); -}); -_page.set('onLoadFinished', function (status: string) { - console.log("Loading finished, the page is " + ((status == "success") ? "open." : "not open!")); -}); -_page.set('settings.loadImages', false); -_page.set('settings.resourceTimeout', 1000); - - -_page.set('settings.viewportSize', { - width : 1920, - height : 1080 -}); -_page.open("http://google.co.jp").then((status) => { - _page.render("/tmp/google-top.jpg", { - format : 'jpeg', - quality : '80' - }); - _ph.exit(); -}); - phantom.create().then((ph) => { - ph.addCookie('cookie_name', 'cookie_value', 'localhost', () => {}); - ph.getCookies((cookies) => {}); + return ph.createPage().then((page) => { + page.open("http://localhost:9901/cookie").then((status) => { + var someFunc = (aaa: string, my_obj: Object) => { + var attribute_to_want = aaa; + var h2Arr: string[] = []; + var results = document.querySelectorAll(attribute_to_want); + for (var i = 0, len = results.length; i < len; ++i) { + var result = results[i]; + h2Arr.push(result.innerText); + } + return { + h2: h2Arr, + aaa: this.arguments, + obj: my_obj + }; + }; + var finishedFunc = (result: any) => { + ph.exit(); + }; + return page.evaluate(someFunc, 'div', { wahtt: 111 }).then(finishedFunc); + }); + }).then(() => { + return ph.createPage().then((page) => { + page.open('http://www.phantomjs.org').then((status) => { + if (status === 'success') { + page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js').then(() => { + page.injectJs('do.js').then((res) => { + page.evaluate(() => { + return document.title; + }).then((title: string) => { + console.log(title); + ph.exit(); + }); + }); + }); - ph.createPage().then((page) => { - page.set('Referer', 'http://google.com'); - page.set('settings.userAgent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1'); - - page.open("http://localhost:9901/cookie", (status) => { - var someFunc = (aaa: string, my_obj: Object) => { - var attribute_to_want = aaa; - var h2Arr: string[] = []; - var results = document.querySelectorAll(attribute_to_want); - for (var i = 0, len = results.length; i < len; ++i) { - var result = results[i]; - h2Arr.push(result.innerText); - } - return { - h2: h2Arr, - aaa: this.arguments, - obj: my_obj - }; - }; - var finishedFunc = (result: any) => { - ph.exit(); - }; - page.evaluate(someFunc, 'div', {wahtt: 111}).then(finishedFunc); - }); - }); - - ph.createPage((page) => { - page.open('http://www.phantomjs.org', (status) => { - if (status === 'success') { - page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', () => { - page.injectJs('do.js', (res) => { - page.evaluate(() => { - return document.title; - }, (title: string) => { - console.log(title); - ph.exit(); + page.sendEvent('click', 350, 320); + page.sendEvent('click', 350, 320, 'right'); + page.sendEvent('keypress', 'A', null, null, 0x02000000 | 0x08000000); + page.sendEvent('keypress', 'A'); + } }); - }); }); - - page.sendEvent('click', 350, 320); - page.sendEvent('click', 350, 320, 'right'); - page.sendEvent('keypress', 'A', null, null, 0x02000000 | 0x08000000); - page.sendEvent('keypress', 'A'); - - page.setViewportSize(800, 640); - page.setPaperSize({ - width: '200px', - height: '300px', - margin: '0px' - }); - page.setPaperSize({ - format: 'A4', - orientation: 'portrait', - margin: '1cm' - }); - page.setPaperSize({ - width: '5in', - height: '7in', - margin: { - top: '50px', - left: '20px' - } - }); - page.setZoomFactor(0.25); - } }); - }); -}); +}); \ No newline at end of file diff --git a/phantom/phantom.d.ts b/phantom/phantom.d.ts index 30398b263..f87f25782 100644 --- a/phantom/phantom.d.ts +++ b/phantom/phantom.d.ts @@ -1,6 +1,6 @@ // Type definitions for PhantomJS bridge for NodeJS // Project: https://github.com/sgentle/phantomjs-node -// Definitions by: Random +// Definitions by: horiuchi , Random // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare module "phantom" { @@ -14,37 +14,43 @@ declare module "phantom" { interface WebPage { open(url: string): Promise; - close(): void; - - get(key: string): Promise; - set(key: string, value: any): Promise; - setHeaders(headers: Object): Promise; - setViewportSize(width: number, height: number): Promise; - setPaperSize(options: IPaperSizeOptions): Promise; - setZoomFactor(factor: number): Promise; + close(): Promise; evaluate(callback: () => R): Promise; evaluate(callback: (arg: T) => void, arg: T): Promise; evaluate(callback: (arg: T) => R, arg: T): Promise; evaluate(callback: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): Promise; evaluate(callback: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): Promise; + evaluate(callback: (...args: any[]) => R, ...args: any[]): Promise; includeJs(url: string): Promise; injectJs(filename: string): Promise; - sendEvent(mouseEventType: string, mouseX: number, mouseY: number, button?: string): void; - sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): void; - uploadFile(selector: string, filename: string): void; + sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): Promise; + sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): Promise; render(filename: string): Promise; render(filename: string, options?: { format?: string; quality?: string; }): Promise; renderBase64(type: string): Promise; - goBack(): void; - goForward(): void; - reload(): void; - - getContent(): Promise; setContent(html: string, url: string): Promise; - getCookies(): Promise<{ name: string; value: string; domain?: string; }[]>; + + property(key: string): Promise; + property(key: string, value: T): Promise; + + setting(key: string): Promise; + + addCookie(cookie: ICookie): Promise; + deleteCookie(cookieName: string): Promise; + clearCookies(): Promise; + } + + interface ICookie { + name: string, + value: string, + domain?: string, + path: string, + httponly?: boolean, + secure?: boolean, + expires?: Date } interface IPaperSizeOptions {