diff --git a/types/slimerjs/index.d.ts b/types/slimerjs/index.d.ts new file mode 100644 index 0000000000..b45d6d9495 --- /dev/null +++ b/types/slimerjs/index.d.ts @@ -0,0 +1,359 @@ +// Type definitions for SlimerJS 0.10.3 +// Project: https://docs.slimerjs.org/current/index.html#api-reference +// Definitions by: Alex Wall +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + + +declare var phantom: IPhantom; +declare var slimer: ISlimer; + +interface ISlimer { + version: number; + geckoVersion: number; + + clearHttpAuth(): void; + isExiting(): boolean; + hasFeature(featureName: string): boolean; + exit(returnValue?: number): void; + wait(milliseconds: number): void; +} + +interface IPhantom { + + // Properties + args: string[]; // DEPRECATED + cookies: ICookie[]; + cookiesEnabled: boolean; + libraryPath: string; + scriptName: string; // DEPRECATED + version: { + major: number; + minor: number; + patch: number; + }; + + // Functions + addCookie(cookie: ICookie): boolean; + clearCookies(): void; + deleteCookie(cookieName: string): boolean; + exit(returnValue?: any): boolean; + injectJs(filename: string): boolean; + + // Callbacks + onError: (msg: string, trace: string[]) => any; +} + +interface ISystem { + pid: number; + platform: string; + os: { + architecture: string; + name: string; + version: string; + }; + env: { [name: string]: string; }; + args: string[]; +} + +interface IHttpConf { + operation: string; + data: any; + headers: any +} + +interface IWebPage { + + // Properties + canGoBack: boolean; + canGoForward: boolean; + clipRect: IClipRect; + content: string; + cookies: ICookie[]; + customHeaders: { [name: string]: string; }; + event: any; // :TODO: elaborate this when documentation improves + focusedFrameName: string; + frameContent: string; + frameName: string; + framePlainText: string; + frameTitle: string; + frameUrl: string; + framesCount: number; + framesName: any; // :TODO: elaborate this when documentation improves + libraryPath: string; + navigationLocked: boolean; + offlineStoragePath: string; + offlineStorageQuota: number; + ownsPages: boolean; + pages: IWebPage[]; + pagesWindowName: string; + paperSize: IPaperSize; + plainText: string; + scrollPosition: ITopLeft; + settings: IWebPageSettings; + title: string; + url: string; + viewportSize: ISize; + windowName: string; + zoomFactor: number; + captureContent: Array; // slimerjs only + + // Functions + addCookie(cookie: ICookie): boolean; + childFramesCount(): number; // DEPRECATED + childFramesName(): string; // DEPRECATED + clearCookies(): void; + close(): Promise; + close(): void; + currentFrameName(): string; // DEPRECATED + deleteCookie(cookieName: string): boolean; + // evaluate(callback: () => R): 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; + evaluate(callback: () => R, ...args: any[]): R; + evaluateAsync(fn: Function): void; + evaluateJavaScript(str: string): any; // :TODO: elaborate this when documentation improves + getPage(windowName: string): IWebPage; + go(index: number): void; + goBack(): void; + goForward(): void; + includeJs(url: string): Promise; + includeJs(url: string, callback: Function): void; + injectJs(filename: string): Promise; + injectJs(filename: string): boolean; + open(url: string): Promise; + open(url: string, callback: (status: string) => any): Promise; + open(url: string, method: string, callback: (status: string) => any): Promise; // maybe data is missing + open(url: string, method: string, data: any, callback?: (status: string) => any): Promise; + open(url: string, method: string, data: any, headers: any, callback: (status: string) => any): Promise; + openUrl(url: string, httpConf: IHttpConf, settings: any): Promise; // :TODO: elaborate this when documentation improves + release(): void; // DEPRECATED + reload(): void; + render(filename: string): Promise; + render(filename: string, options?: { format?: string; quality?: string; ratio?: number; onlyViewport?: boolean }): Promise; + renderBase64(type: string): Promise; + render(filename: string): void; + renderBase64(format: string): string; + sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): Promise; + sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): Promise; + sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): void; + sendEvent(keyboardEventType: string, keyOrKeys: any, aNull?: any, bNull?: any, modifier?: number): void; + setContent(html: string, url: string): Promise; + setContent(content: string, url: string): void; + stop(): void; + switchToFocusedFrame(): void; + switchToFrame(frameName: string): void; + switchToFrame(framePosition: number): void; + switchToChildFrame(frameName: string): void; + switchToChildFrame(framePosition: number): void; + switchToMainFrame(): void; // DEPRECATED + switchToParentFrame(): void; // DEPRECATED + uploadFile(selector: string, filename: string): void; + + // Callbacks + onAlert: (msg: string) => any; + onCallback: Function; // EXPERIMENTAL + onClosing: (closingPage: IWebPage) => any; + onConfirm: (msg: string) => boolean; + onConsoleMessage: (msg: string, lineNum?: number, sourceId?: string) => any; + onError: (msg: string, trace: string[]) => any; + onFilePicker: (oldFile: string) => string; + onInitialized: () => any; + onLoadFinished: (status: string) => any; + onLoadStarted: () => any; + onNavigationRequested: (url: string, type: string, willNavigate: boolean, main: boolean) => any; + onPageCreated: (newPage: IWebPage) => any; + onPrompt: (msg: string, defaultVal: string) => string; + onResourceError: (resourceError: IResourceError) => any; + onResourceReceived: (response: IResourceResponse) => any; + onResourceRequested: (requestData: IResourceRequest, networkRequest: INetworkRequest) => any; + onUrlChanged: (targetUrl: string) => any; + + // Callback triggers + closing(closingPage: IWebPage): void; + initialized(): void; + javaScriptAlertSent(msg: string): void; + javaScriptConsoleMessageSent(msg: string, lineNum?: number, sourceId?: string): void; + loadFinished(status: string): void; + loadStarted(): void; + navigationRequested(url: string, type: string, willNavigate: boolean, main: boolean): void; + rawPageCreated(newPage: IWebPage): void; + resourceReceived(response: IResourceResponse): void; + resourceRequested(requestData: IResourceRequest, networkRequest: INetworkRequest): void; + urlChanged(targetUrl: string): void; +} + +interface IResourceError { + id: number; + url: string; + errorCode: string; + errorString: string; +} + +interface IHttpVersion { + major: number, + minor: number +} + +interface IResourceResponse { + id: number; + url: string; + time: Date; + headers: { [name: string]: string; }; + bodySize: number; + contentType?: string; + redirectURL?: string; + stage: string; + status: number; + statusText: string; + contentCharset: string; + referrer: string; + isFileDownloading: boolean; + body: any; + httpVersion: IHttpVersion; +} + +interface IResourceRequest { + id: number; + method: string; + url: string; + time: Date; + headers: { [name: string]: string; }; +} + +interface INetworkRequest { + abort(): void; + changeUrl(url: string): void; + setHeader(name: string, value: string): void; +} + +interface IPaperSize { + width?: string; + height?: string; + border: string; + format?: string; + orientation?: string; + margin?: any; // string | { top?: string; left?: string; bottom?: string; right?: string; } +} + +interface IWebPageSettings { + javascriptEnabled: boolean; + loadImages: boolean; + localToRemoteUrlAccessEnabled: boolean; + userAgent: string; + userName: string; + password: string; + XSSAuditingEnabled: boolean; + webSecurityEnabled: boolean; + resourceTimeout: number; +} + +interface IFileSystem { + + // Properties + separator: string; + workingDirectory: string; + + // Functions + + // Query Functions + list(path: string): string[]; + absolute(path: string): string; + exists(path: string): boolean; + isDirectory(path: string): boolean; + isFile(path: string): boolean; + isAbsolute(path: string): boolean; + isExecutable(path: string): boolean; + isReadable(path: string): boolean; + isWritable(path: string): boolean; + isLink(path: string): boolean; + readLink(path: string): string; + + // Directory Functions + changeWorkingDirectory(path: string): void; + makeDirectory(path: string): void; + makeTree(path: string): void; + removeDirectory(path: string): void; + removeTree(path: string): void; + copyTree(source: string, destination: string): void; + + // File Functions + open(path: string, mode: string): IStream; + open(path: string, options: { mode: string; charset?: string; }): IStream; + read(path: string): string; + write(path: string, content: string, mode: string): void; + size(path: string): number; + remove(path: string): void; + copy(source: string, destination: string): void; + move(source: string, destination: string): void; + touch(path: string): void; +} + +interface IStream { + atEnd(): boolean; + close(): void; + flush(): void; + read(): string; + readLine(): string; + seek(position: number): void; + write(data: string): void; + writeLine(data: string): void; +} + +interface IWebServer { + port: number; + listen(port: number, cb?: (request: IWebServerRequest, response: IWebServerResponse) => void): boolean; + listen(ipAddressPort: string, cb?: (request: IWebServerRequest, response: IWebServerResponse) => void): boolean; + close(): void; +} + +interface IWebServerRequest { + method: string; + url: string; + httpVersion: number; + headers: { [name: string]: string; }; + post: string; + postRaw: string; +} + +interface IWebServerResponse { + headers: { [name: string]: string; }; + setHeader(name: string, value: string): void; + header(name: string): string; + statusCode: number; + setEncoding(encoding: string): void; + write(data: string): void; + writeHead(statusCode: number, headers?: { [name: string]: string; }): void; + close(): void; + closeGracefully(): void; +} + +interface ITopLeft { + top: number; + left: number; +} + +interface ISize { + width: number; + height: number; +} + +interface IClipRect extends ITopLeft, ISize { +} + +interface ICookie { + name: string, + value: string, + domain?: string, + path: string, + httponly?: boolean, + secure?: boolean, + expires?: string, + expiry: number +} + +declare module "webpage" { + export function create(): IWebPage; + export function exit(returnValue?: number): void; +} diff --git a/types/slimerjs/slimerjs-tests.ts b/types/slimerjs/slimerjs-tests.ts new file mode 100644 index 0000000000..f3de79d963 --- /dev/null +++ b/types/slimerjs/slimerjs-tests.ts @@ -0,0 +1,64 @@ +import webpagemod = require('webpage') +let page = webpagemod.create(); +let vUrl = 'https://www.w3c.org'; + +let vUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8' +page.settings.userAgent = vUserAgent; + +page.onConsoleMessage = function(vMsg) { +}; + +page.onAlert = function(vMsg) { +}; + +page.onLoadStarted = function() { +}; + +page.captureContent = [ /json/ ] + +page.onResourceReceived = function(oResponse) { + oResponse.id; + oResponse.bodySize; + oResponse.body; + oResponse.contentType; + oResponse.headers; +}; + +page.onResourceRequested = function(oRequestData, oNetworkRequest) { + oRequestData.headers; + oRequestData.id; + oRequestData.method; + oRequestData.time; + oRequestData.url; +}; + + +page.onLoadFinished = function(vStatus) { + phantom.exit(); +}; + +page.onInitialized = function() { +}; + +page.onPageCreated = function(oPage) { + let oCookie: ICookie + oPage.addCookie(oCookie); +}; + + +page.open(vUrl) // loads a page + .then(function(){ // executed after loading + page.viewportSize = { width:414, height:736 }; + + + let vFilename = `../data/page01.png`; + page.render(vFilename, {onlyViewport:true}) + + // then open a second page + return page.open('http://'); + }) + .then(function(){ + // click somewhere on the second page + page.sendEvent("click", 5, 5, 'left', 0); + slimer.exit() + }); diff --git a/types/slimerjs/tsconfig.json b/types/slimerjs/tsconfig.json new file mode 100644 index 0000000000..d108eae45c --- /dev/null +++ b/types/slimerjs/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "./" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "slimerjs-tests.ts" + ] +}