diff --git a/nightmare/nightmare-tests.ts b/nightmare/nightmare-tests.ts new file mode 100644 index 0000000000..ba3034f628 --- /dev/null +++ b/nightmare/nightmare-tests.ts @@ -0,0 +1,320 @@ +/// +/// + +import Nightmare = require("nightmare"); + + +new Nightmare() + .goto('http://yahoo.com') + .type('input[title="Search"]', 'github nightmare') + .click('.searchsubmit') + .run((err: any, nightmare: Nightmare) => { + if (err) return console.log(err); + console.log('Done!'); + }); + +new Nightmare() + .goto('http://yahoo.com') + .type('input[title="Search"]', 'github nightmare') + .click('.searchsubmit') + .wait('.url.breadcrumb') + .evaluate(() => { + return (document.querySelector('.url.breadcrumb')).innerText; + }, (breadcrumb: string) => { + // expect(breadcrumb).to.equal('github.com'); + }) + .run((err: any, nightmare: Nightmare) => {}); + + +var done = (err: any) => {}; + +new Nightmare() + .goto('http://www.google.com/') + .click('a') + .back() + .forward() + .run(done); + +new Nightmare() + .goto('http://www.wikipedia.org/') + .refresh() + .run(done); + +new Nightmare() + .goto('http://www.wikipedia.org/') + .url(function (url) { + }) + .run(done); + +new Nightmare() + .goto('http://www.wikipedia.org/') + .exists('a.link-box', function (exists) { + }) + .exists('a.blahblahblah', function (exists) { + }) + .run(done); + +new Nightmare() + .goto('http://www.wikipedia.org/') + .title(function (title) { + }) + .run(done); + +new Nightmare() + .goto('http://www.wikipedia.org/') + .visible('input[type="hidden"]', function (visible) { + }) + .visible('#asdfasdfasdf', function (visible) { + }) + .visible('#searchInput', function (visible) { + }) + .run(done); + + +new Nightmare() + .goto('http://yahoo.com') + .evaluate(function (parameter) { + return document.title + ' -- ' + parameter; + }, function (title) { + }, 'testparameter') + .run(done); + +new Nightmare() + .goto('http://google.com') + .inject('js', 'test/files/jquery-2.1.1.min.js') + .evaluate(function () { + return $('a').length; + }, function (numAnchors) { + }) + .run(done); + +new Nightmare() + .goto('http://google.com') + .inject('js', 'test/files/jquery-2.1.1.min.js') + .inject('css', 'test/files/test.css') + .evaluate(function () { + return $('body').css('background-color'); + }, function (color) { + }) + .run(done); + +new Nightmare() + .goto('http://google.com') + .inject('js', 'test/files/jquery-2.1.1.min.js') + .inject('pdf', 'test/files/test.css') + .evaluate(function () { + return $('body').css('background-color'); + }, function (color) { + }) + .run(done); + +new Nightmare() + .goto('http://yahoo.com') + .type('input[title="Search"]', 'github nightmare') + .click('.searchsubmit') + .wait() + .evaluate(function () { + return document.title; + }, function (title) { + }) + .run(done); + +new Nightmare() + .goto('http://yahoo.com') + .type('input[title="Search"]', 'github nightmare') + .click('.searchsubmit') + .wait() + .click('.breadcrumb_link') + .wait() + .evaluate(function () { + return document.title; + }, function (title) { + }) + .run(done); + +new Nightmare() + .viewport(320, 320) + .goto('http://www.yahoo.com') + .wait() + .evaluate(function () { + return { + top: document.body.scrollTop, + left: document.body.scrollLeft + }; + }, function (coordinates) { + }) + .scrollTo(100,50) + .evaluate(function () { + return { + top: document.body.scrollTop, + left: document.body.scrollLeft + }; + }, function (coordinates) { + }) + .run(done); + +new Nightmare() + .goto('http://validator.w3.org/#validate_by_upload') + .upload('#uploaded_file', 'test/files/jquery-2.1.1.min.js') + .evaluate(function () { + return (document.getElementById('uploaded_file')).value; + }, function (value) { + }) + .run(done); + +new Nightmare() + .goto('http://yahoo.com') + .screenshot('test/test.png') + .run(done); + +new Nightmare() + .goto('http://yahoo.com') + .pdf('test/test.pdf') + .run(done); + +new Nightmare() + .goto('http://www.google.com/') + .wait('input') + .run(done); + + +var seconds = function () { + var gifs = document.querySelectorAll('img'); + var split = (gifs[gifs.length-2]).src.split('.gif')[0]; + var seconds = split.split('.com/c')[1]; + return parseInt(seconds, 10); +}; +new Nightmare() + .goto('http://onlineclock.net/') + .wait(seconds, 1) + .run(done); + +var seconds = function () { + var text = document.querySelectorAll('b')[0].textContent; + var splits = text.split(/\s/); + var seconds = splits[splits.length-2].split(':')[2]; + return parseInt(seconds, 10)%10; +}; +new Nightmare() + .goto('http://www.whattimeisit.com/') + .wait(seconds, 1, 1500) + .run(done); + +new Nightmare({ + timeout: 1000 + }) + .on('timeout', function (msg) { + }) + .goto('http://www.google.com/') + .wait('bbb') + .run(done); + +new Nightmare() + .on('initialized', function () { + }) + .on('loadStarted', function () { + }) + .on('loadFinished', function (status) { + }) + .on('resourceRequested', function () { + }) + .on('resourceReceived', function () { + }) + .on('navigationRequested', function (url) { + }) + .on('urlChanged', function (url) { + }) + .on('consoleMessage', function () { + }) + .on('alert', function () { + }) + .on('prompt', function () { + }) + .on('error', function () { + }) + .goto('http://www.yahoo.com') + .run(done); + +new Nightmare() + .useragent('firefox') + .goto('http://www.wikipedia.org/') + .evaluate(function () { + return window.navigator.userAgent; + }, function (res) { + }) + .run(done); + +new Nightmare() + .authentication('my','auth') + .goto('http://httpbin.org/basic-auth/my/auth') + .evaluate(function () { + return document.body.innerHTML; + }, function (data){ + }) + .run(done); + +var size = { width : 400, height: 1000 }; +new Nightmare() + .viewport(size.width, size.height) + .goto('http://www.wikipedia.org/') + .evaluate(function () { + return { + width: window.innerWidth, + height: window.innerHeight + }; + }, function (res) { + }) + .run(done); + +new Nightmare() + .viewport(1600, 900) + .goto('http://www.wikipedia.org') + .wait() + .screenshot('test/testScaleDefault.png') + .viewport(3200, 1800) + .zoom(2) + .goto('http://www.wikipedia.org') + .wait() + .screenshot('test/testScaleIs2.png') + .run(done); + +var headers = { + 'X-Nightmare-Header': 'hello world' +}; +new Nightmare() + .headers(headers) + .goto('http://httpbin.org/headers') + .evaluate(function () { + return (document.body.children[0]).innerHTML; + }, function (data: string) { + }) + .run(done); + + +var nightmare = new Nightmare().goto('http://yahoo.com'); +nightmare.run(); + + +function search(term: string) { + return function (nightmare: Nightmare) { + nightmare + .goto('http://yahoo.com') + .type('.input-query', term) + .click('.searchsubmit') + .wait(); + }; +} +function testTitle(term: string) { + return function (nightmare: Nightmare) { + nightmare + .evaluate(function () { + return document.title; + }, function (title: string) { + }); + }; +} +new Nightmare() + .use(search('test term')) + .use(testTitle('test term')) + .run(done); + diff --git a/nightmare/nightmare.d.ts b/nightmare/nightmare.d.ts new file mode 100644 index 0000000000..f52b73cfc3 --- /dev/null +++ b/nightmare/nightmare.d.ts @@ -0,0 +1,121 @@ +// Type definitions for Nightmare 1.6.5 +// Project: https://github.com/segmentio/nightmare +// Definitions by: horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "nightmare" { + + class Nightmare { + constructor(options?: Nightmare.IConstructorOptions); + + // Interact + goto(url: string): Nightmare; + back(): Nightmare; + forward(): Nightmare; + refresh(): Nightmare; + click(selector: string): Nightmare; + type(selector: string, text: string): Nightmare; + upload(selector: string, path: string): Nightmare; + scrollTo(top: number, left: number): Nightmare; + inject(type: string, file: string): Nightmare; + evaluate(fn: () => void): Nightmare; + evaluate(fn: () => R, cb: (result: R) => void): Nightmare; + evaluate(fn: (arg: T) => void, cb: () => void, arg: T): Nightmare; + evaluate(fn: (arg: T) => R, cb: (result: R) => void, arg: T): Nightmare; + evaluate(fn: (arg1: T1, arg2: T2) => R, cb: (result: R) => void, arg1: T1, arg2: T2): Nightmare; + evaluate(fn: (arg1: T1, arg2: T2, arg3: T3) => R, cb: (result: R) => void, arg1: T1, arg2: T2, arg3: T3): Nightmare; + wait(): Nightmare; + wait(ms: number): Nightmare; + wait(selector: string): Nightmare; + wait(fn: () => any, value: any, delay?: number): Nightmare; + use(plugin: (nightmare: Nightmare) => void): Nightmare; + run(cb?: (err: any, nightmare: Nightmare) => void): Nightmare; + + // Extract + exists(selector: string, cb: (result: boolean) => void): Nightmare; + visible(selector: string, cb: (result: boolean) => void): Nightmare; + on(event: string, cb: () => void): Nightmare; + on(event: 'initialized', cb: () => void): Nightmare; + on(event: 'loadStarted', cb: () => void): Nightmare; + on(event: 'loadFinished', cb: (status: string) => void): Nightmare; + on(event: 'urlChanged', cb: (targetUrl: string) => void): Nightmare; + on(event: 'navigationRequested', cb: (url: string, type: string, willNavigate: boolean, main: boolean) => void): Nightmare; + on(event: 'resourceRequested', cb: (requestData: Nightmare.IRequest, networkRequest: Nightmare.INetwordRequest) => void): Nightmare; + on(event: 'resourceReceived', cb: (response: Nightmare.IResponse) => void): Nightmare; + on(event: 'resourceError', cb: (resourceError: Nightmare.IResourceError) => void): Nightmare; + on(event: 'consoleMessage', cb: (msg: string, lineNumber: number, sourceId: number) => void): Nightmare; + on(event: 'alert', cb: (msg: string) => void): Nightmare; + on(event: 'confirm', cb: (msg: string) => void): Nightmare; + on(event: 'prompt', cb: (msg: string, defaultValue?: string) => void): Nightmare; + on(event: 'error', cb: (msg: string, trace?: Nightmare.IStackTrace[]) => void): Nightmare; + on(event: 'timeout', cb: (msg: string) => void): Nightmare; + screenshot(path: string): Nightmare; + pdf(path: string): Nightmare; + title(cb: (title: string) => void): Nightmare; + url(cb: (url: string) => void): Nightmare; + + // Settings + authentication(user: string, password: string): Nightmare; + useragent(useragent: string): Nightmare; + viewport(width: number, height: number): Nightmare; + zoom(zoomFactor: number): Nightmare; + headers(headers: Object): Nightmare; + } + + module Nightmare { + export interface IConstructorOptions { + timeout?: any; // number | string; + interval?: any; // number | string; + port?: number; + weak?: boolean; + loadImages?: boolean; + ignoreSslErrors?: boolean; + sslProtocol?: string; + webSecurity?: boolean; + proxy?: string; + proxyType?: string; + proxyAuth?: string; + cookiesFile?: string; + phantomPath?: string; + } + + export interface IRequest { + id: number; + method: string; + url: string; + time: Date; + headers: Object; + } + export interface INetwordRequest { + abort(): void; + changeUrl(url: string): void; + setHeader(key: string, value: string): void; + } + export interface IResponse { + id: number; + url: string; + time: Date; + headers: Object; + bodySize: number; + contentType: string; + redirectURL: string; + stage: string; + status: number; + statusText: string; + } + export interface IResourceError { + id: number; + url: string; + errorCode: number; + errorString: string; + } + export interface IStackTrace { + file: string; + line: number; + function?: string; + } + } + + export = Nightmare; +} +