PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/nightmare/Readme.md

https://bitbucket.org/nicomee/martingale
Markdown | 948 lines | 654 code | 294 blank | 0 comment | 0 complexity | 3bc61ff9ec05af0ae8a1359b0092a7d2 MD5 | raw file
Possible License(s): 0BSD, GPL-2.0, JSON, Apache-2.0, BSD-3-Clause, Unlicense, GPL-3.0, CC-BY-SA-3.0, MIT, BSD-2-Clause
  1. [![Build Status](https://img.shields.io/circleci/project/segmentio/nightmare/master.svg)](https://circleci.com/gh/segmentio/nightmare)
  2. [![Join the chat at https://gitter.im/rosshinkley/nightmare](https://badges.gitter.im/rosshinkley/nightmare.svg)](https://gitter.im/rosshinkley/nightmare?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
  3. # Nightmare
  4. Nightmare is a high-level browser automation library from [Segment](https://segment.com).
  5. The goal is to expose a few simple methods that mimic user actions (like `goto`, `type` and `click`), with an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It was originally designed for automating tasks across sites that don't have APIs, but is most often used for UI testing and crawling.
  6. Under the covers it uses [Electron](http://electron.atom.io/), which is similar to [PhantomJS](http://phantomjs.org/) but roughly [twice as fast](https://github.com/segmentio/nightmare/issues/484#issuecomment-184519591) and more modern.
  7. **We've implemented [many](https://github.com/segmentio/nightmare/issues/1388) of the security recommendations [outlined by Electron](https://github.com/electron/electron/blob/master/docs/tutorial/security.md) to try and keep you safe, but vulnerabilities may exist in Electron that may allow a malicious website to execute code on your company. Avoid visiting untrusted websites.**
  8. [Niffy](https://github.com/segmentio/niffy) is a perceptual diffing tool built on Nightmare. It helps you detect UI changes and bugs across releases of your web app.
  9. [Daydream](https://github.com/segmentio/daydream) is a complementary chrome extension built by [@stevenmiller888](https://github.com/stevenmiller888) that generates Nightmare scripts for you while you browse.
  10. Many thanks to [@matthewmueller](https://github.com/matthewmueller) and [@rosshinkley](https://github.com/rosshinkley) for their help on Nightmare.
  11. * [Examples](#examples)
  12. * [UI Testing Quick Start](https://segment.com/blog/ui-testing-with-nightmare/)
  13. * [Perceptual Diffing with Niffy & Nightmare](https://segment.com/blog/perceptual-diffing-with-niffy/)
  14. * [API](#api)
  15. * [Set up an instance](#nightmareoptions)
  16. * [Interact with the page](#interact-with-the-page)
  17. * [Extract from the page](#extract-from-the-page)
  18. * [Cookies](#cookies)
  19. * [Proxies](#proxies)
  20. * [Promises](#promises)
  21. * [Extending Nightmare](#extending-nightmare)
  22. * [Usage](#usage)
  23. * [Debugging](#debugging)
  24. * [Additional Resources](#additional-resources)
  25. ## Examples
  26. Let's search on DuckDuckGo:
  27. ```js
  28. const Nightmare = require('nightmare')
  29. const nightmare = Nightmare({ show: true })
  30. nightmare
  31. .goto('https://duckduckgo.com')
  32. .type('#search_form_input_homepage', 'github nightmare')
  33. .click('#search_button_homepage')
  34. .wait('#r1-0 a.result__a')
  35. .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
  36. .end()
  37. .then(console.log)
  38. .catch(error => {
  39. console.error('Search failed:', error)
  40. })
  41. ```
  42. You can run this with:
  43. ```shell
  44. npm install --save nightmare
  45. node example.js
  46. ```
  47. Or, let's run some mocha tests:
  48. ```js
  49. const Nightmare = require('nightmare')
  50. const chai = require('chai')
  51. const expect = chai.expect
  52. describe('test duckduckgo search results', () => {
  53. it('should find the nightmare github link first', function(done) {
  54. this.timeout('10s')
  55. const nightmare = Nightmare()
  56. nightmare
  57. .goto('https://duckduckgo.com')
  58. .type('#search_form_input_homepage', 'github nightmare')
  59. .click('#search_button_homepage')
  60. .wait('#links .result__a')
  61. .evaluate(() => document.querySelector('#links .result__a').href)
  62. .end()
  63. .then(link => {
  64. expect(link).to.equal('https://github.com/segmentio/nightmare')
  65. done()
  66. })
  67. })
  68. })
  69. ```
  70. You can see examples of every function [in the tests here](https://github.com/segmentio/nightmare/blob/master/test/index.js).
  71. To get started with UI Testing, check out this [quick start guide](https://segment.com/blog/ui-testing-with-nightmare).
  72. ### To install dependencies
  73. ```
  74. npm install
  75. ```
  76. ### To run the mocha tests
  77. ```
  78. npm test
  79. ```
  80. ### Node versions
  81. Nightmare is intended to be run on NodeJS 4.x or higher.
  82. ## API
  83. #### Nightmare(options)
  84. Creates a new instance that can navigate around the web. The available options are [documented here](https://github.com/atom/electron/blob/master/docs/api/browser-window.md#new-browserwindowoptions), along with the following nightmare-specific options.
  85. ##### waitTimeout (default: 30s)
  86. Throws an exception if the `.wait()` didn't return `true` within the set timeframe.
  87. ```js
  88. const nightmare = Nightmare({
  89. waitTimeout: 1000 // in ms
  90. })
  91. ```
  92. ##### gotoTimeout (default: 30s)
  93. Throws an exception if the `.goto()` didn't finish loading within the set timeframe. Note that, even though `goto` normally waits for all the resources on a page to load, a timeout exception is only raised if the DOM itself has not yet loaded.
  94. ```js
  95. const nightmare = Nightmare({
  96. gotoTimeout: 1000 // in ms
  97. })
  98. ```
  99. ##### loadTimeout (default: infinite)
  100. Forces Nightmare to move on if a page transition caused by an action (eg, `.click()`) didn't finish within the set timeframe. If `loadTimeout` is shorter than `gotoTimeout`, the exceptions thrown by `gotoTimeout` will be suppressed.
  101. ```js
  102. const nightmare = Nightmare({
  103. loadTimeout: 1000 // in ms
  104. })
  105. ```
  106. ##### executionTimeout (default: 30s)
  107. The maximum amount of time to wait for an `.evaluate()` statement to complete.
  108. ```js
  109. const nightmare = Nightmare({
  110. executionTimeout: 1000 // in ms
  111. })
  112. ```
  113. ##### paths
  114. The default system paths that Electron knows about. Here's a list of available paths: https://github.com/atom/electron/blob/master/docs/api/app.md#appgetpathname
  115. You can overwrite them in Nightmare by doing the following:
  116. ```js
  117. const nightmare = Nightmare({
  118. paths: {
  119. userData: '/user/data'
  120. }
  121. })
  122. ```
  123. ##### switches
  124. The command line switches used by the Chrome browser that are also supported by Electron. Here's a list of supported Chrome command line switches:
  125. https://github.com/atom/electron/blob/master/docs/api/chrome-command-line-switches.md
  126. ```js
  127. const nightmare = Nightmare({
  128. switches: {
  129. 'proxy-server': '1.2.3.4:5678',
  130. 'ignore-certificate-errors': true
  131. }
  132. })
  133. ```
  134. ##### electronPath
  135. The path to the prebuilt Electron binary. This is useful for testing on different versions of Electron. Note that Nightmare only supports the version on which this package depends. Use this option at your own risk.
  136. ```js
  137. const nightmare = Nightmare({
  138. electronPath: require('electron')
  139. })
  140. ```
  141. ##### dock (OS X)
  142. A boolean to optionally show the Electron icon in the dock (defaults to `false`). This is useful for testing purposes.
  143. ```js
  144. const nightmare = Nightmare({
  145. dock: true
  146. })
  147. ```
  148. ##### openDevTools
  149. Optionally shows the DevTools in the Electron window using `true`, or use an object hash containing `mode: 'detach'` to show in a separate window. The hash gets passed to [`contents.openDevTools()`](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentsopendevtoolsoptions) to be handled. This is also useful for testing purposes. Note that this option is honored only if `show` is set to `true`.
  150. ```js
  151. const nightmare = Nightmare({
  152. openDevTools: {
  153. mode: 'detach'
  154. },
  155. show: true
  156. })
  157. ```
  158. ##### typeInterval (default: 100ms)
  159. How long to wait between keystrokes when using `.type()`.
  160. ```js
  161. const nightmare = Nightmare({
  162. typeInterval: 20
  163. })
  164. ```
  165. ##### pollInterval (default: 250ms)
  166. How long to wait between checks for the `.wait()` condition to be successful.
  167. ```js
  168. const nightmare = Nightmare({
  169. pollInterval: 50 //in ms
  170. })
  171. ```
  172. ##### maxAuthRetries (default: 3)
  173. Defines the number of times to retry an authentication when set up with `.authenticate()`.
  174. ```js
  175. const nightmare = Nightmare({
  176. maxAuthRetries: 3
  177. })
  178. ```
  179. #### certificateSubjectName
  180. A string to determine the client certificate selected by electron. If this options is set, the [`select-client-certificate`](https://github.com/electron/electron/blob/master/docs/api/app.md#event-select-client-certificate) event will be set to loop through the certificateList and find the first certificate that matches `subjectName` on the electron [`Certificate Object`](https://electronjs.org/docs/api/structures/certificate).
  181. ```js
  182. const nightmare = Nightmare({
  183. certificateSubjectName: 'tester'
  184. })
  185. ```
  186. #### .engineVersions()
  187. Gets the versions for Electron and Chromium.
  188. #### .useragent(useragent)
  189. Sets the `useragent` used by electron.
  190. #### .authentication(user, password)
  191. Sets the `user` and `password` for accessing a web page using basic authentication. Be sure to set it before calling `.goto(url)`.
  192. #### .end()
  193. Completes any queue operations, disconnect and close the electron process. Note that if you're using promises, `.then()` must be called after `.end()` to run the `.end()` task. Also note that if using an `.end()` callback, the `.end()` call is equivalent to calling `.end()` followed by `.then(fn)`. Consider:
  194. ```js
  195. nightmare
  196. .goto(someUrl)
  197. .end(() => 'some value')
  198. //prints "some value"
  199. .then(console.log)
  200. ```
  201. #### .halt(error, done)
  202. Clears all queued operations, kills the electron process, and passes error message or 'Nightmare Halted' to an unresolved promise. Done will be called after the process has exited.
  203. ### Interact with the Page
  204. #### .goto(url[, headers])
  205. Loads the page at `url`. Optionally, a `headers` hash can be supplied to set headers on the `goto` request.
  206. When a page load is successful, `goto` returns an object with metadata about the page load, including:
  207. * `url`: The URL that was loaded
  208. * `code`: The HTTP status code (e.g. 200, 404, 500)
  209. * `method`: The HTTP method used (e.g. "GET", "POST")
  210. * `referrer`: The page that the window was displaying prior to this load or an empty string if this is the first page load.
  211. * `headers`: An object representing the response headers for the request as in `{header1-name: header1-value, header2-name: header2-value}`
  212. If the page load fails, the error will be an object with the following properties:
  213. * `message`: A string describing the type of error
  214. * `code`: The underlying error code describing what went wrong. Note this is NOT the HTTP status code. For possible values, see https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h
  215. * `details`: A string with additional details about the error. This may be null or an empty string.
  216. * `url`: The URL that failed to load
  217. Note that any valid response from a server is considered successful. That means things like 404 not found errors are successful results for `goto`. Only things that would cause no page to appear in the browser window, such as no server responding at the given address, the server hanging up in the middle of a response, or invalid URLs, are errors.
  218. You can also adjust how long `goto` will wait before timing out by setting the [`gotoTimeout` option](#gototimeout-default-30s) on the Nightmare constructor.
  219. #### .back()
  220. Goes back to the previous page.
  221. #### .forward()
  222. Goes forward to the next page.
  223. #### .refresh()
  224. Refreshes the current page.
  225. #### .click(selector)
  226. Clicks the `selector` element once.
  227. #### .mousedown(selector)
  228. Mousedowns the `selector` element once.
  229. #### .mouseup(selector)
  230. Mouseups the `selector` element once.
  231. #### .mouseover(selector)
  232. Mouseovers the `selector` element once.
  233. #### .mouseout(selector)
  234. Mouseout the `selector` element once.
  235. #### .type(selector[, text])
  236. Enters the `text` provided into the `selector` element. Empty or falsey values provided for `text` will clear the selector's value.
  237. `.type()` mimics a user typing in a textbox and will emit the proper keyboard events.
  238. Key presses can also be fired using Unicode values with `.type()`. For example, if you wanted to fire an enter key press, you would write `.type('body', '\u000d')`.
  239. > If you don't need the keyboard events, consider using `.insert()` instead as it will be faster and more robust.
  240. #### .insert(selector[, text])
  241. Similar to `.type()`, `.insert()` enters the `text` provided into the `selector` element. Empty or falsey values provided for `text` will clear the selector's value.
  242. `.insert()` is faster than `.type()` but does not trigger the keyboard events.
  243. #### .check(selector)
  244. Checks the `selector` checkbox element.
  245. #### .uncheck(selector)
  246. Unchecks the `selector` checkbox element.
  247. #### .select(selector, option)
  248. Changes the `selector` dropdown element to the option with attribute [value=`option`]
  249. #### .scrollTo(top, left)
  250. Scrolls the page to desired position. `top` and `left` are always relative to the top left corner of the document.
  251. #### .viewport(width, height)
  252. Sets the viewport size.
  253. #### .inject(type, file)
  254. Injects a local `file` onto the current page. The file `type` must be either `js` or `css`.
  255. #### .evaluate(fn[, arg1, arg2,...])
  256. Invokes `fn` on the page with `arg1, arg2,...`. All the `args` are optional. On completion it returns the return value of `fn`. Useful for extracting information from the page. Here's an example:
  257. ```js
  258. const selector = 'h1'
  259. nightmare
  260. .evaluate(selector => {
  261. // now we're executing inside the browser scope.
  262. return document.querySelector(selector).innerText
  263. }, selector) // <-- that's how you pass parameters from Node scope to browser scope
  264. .then(text => {
  265. // ...
  266. })
  267. ```
  268. Error-first callbacks are supported as a part of `evaluate()`. If the arguments passed are one fewer than the arguments expected for the evaluated function, the evaluation will be passed a callback as the last parameter to the function. For example:
  269. ```js
  270. const selector = 'h1'
  271. nightmare
  272. .evaluate((selector, done) => {
  273. // now we're executing inside the browser scope.
  274. setTimeout(
  275. () => done(null, document.querySelector(selector).innerText),
  276. 2000
  277. )
  278. }, selector)
  279. .then(text => {
  280. // ...
  281. })
  282. ```
  283. Note that callbacks support only one value argument (eg `function(err, value)`). Ultimately, the callback will get wrapped in a native Promise and only be able to resolve a single value.
  284. Promises are also supported as a part of `evaluate()`. If the return value of the function has a `then` member, `.evaluate()` assumes it is waiting for a promise. For example:
  285. ```js
  286. const selector = 'h1';
  287. nightmare
  288. .evaluate((selector) => (
  289. new Promise((resolve, reject) => {
  290. setTimeout(() => resolve(document.querySelector(selector).innerText), 2000);
  291. )}, selector)
  292. )
  293. .then((text) => {
  294. // ...
  295. })
  296. ```
  297. #### .wait(ms)
  298. Waits for `ms` milliseconds e.g. `.wait(5000)`.
  299. #### .wait(selector)
  300. Waits until the element `selector` is present e.g. `.wait('#pay-button')`.
  301. #### .wait(fn[, arg1, arg2,...])
  302. Waits until the `fn` evaluated on the page with `arg1, arg2,...` returns `true`. All the `args` are optional. See `.evaluate()` for usage.
  303. #### .header(header, value)
  304. Adds a header override for all HTTP requests. If `header` is undefined, the header overrides will be reset.
  305. ### Extract from the Page
  306. #### .exists(selector)
  307. Returns whether the selector exists or not on the page.
  308. #### .visible(selector)
  309. Returns whether the selector is visible or not.
  310. #### .on(event, callback)
  311. Captures page events with the callback. You have to call `.on()` before calling `.goto()`. Supported events are [documented here](http://electron.atom.io/docs/api/web-contents/#class-webcontents).
  312. ##### Additional "page" events
  313. ###### .on('page', function(type="error", message, stack))
  314. This event is triggered if any javascript exception is thrown on the page. But this event is not triggered if the injected javascript code (e.g. via `.evaluate()`) is throwing an exception.
  315. ##### "page" events
  316. Listens for `window.addEventListener('error')`, `alert(...)`, `prompt(...)` & `confirm(...)`.
  317. ###### .on('page', function(type="error", message, stack))
  318. Listens for top-level page errors. This will get triggered when an error is thrown on the page.
  319. ###### .on('page', function(type="alert", message))
  320. Nightmare disables `window.alert` from popping up by default, but you can still listen for the contents of the alert dialog.
  321. ###### .on('page', function(type="prompt", message, response))
  322. Nightmare disables `window.prompt` from popping up by default, but you can still listen for the message to come up. If you need to handle the confirmation differently, you'll need to use your own preload script.
  323. ###### .on('page', function(type="confirm", message, response))
  324. Nightmare disables `window.confirm` from popping up by default, but you can still listen for the message to come up. If you need to handle the confirmation differently, you'll need to use your own preload script.
  325. ###### .on('console', function(type [, arguments, ...]))
  326. `type` will be either `log`, `warn` or `error` and `arguments` are what gets passed from the console. This event is not triggered if the injected javascript code (e.g. via `.evaluate()`) is using `console.log`.
  327. #### .once(event, callback)
  328. Similar to `.on()`, but captures page events with the callback one time.
  329. #### .removeListener(event, callback)
  330. Removes a given listener callback for an event.
  331. #### .screenshot([path][, clip])
  332. Takes a screenshot of the current page. Useful for debugging. The output is always a `png`. Both arguments are optional. If `path` is provided, it saves the image to the disk. Otherwise it returns a `Buffer` of the image data. If `clip` is provided (as [documented here](https://github.com/atom/electron/blob/master/docs/api/browser-window.md#wincapturepagerect-callback)), the image will be clipped to the rectangle.
  333. #### .html(path, saveType)
  334. Saves the current page as html as files to disk at the given path. Save type options are [here](https://github.com/atom/electron/blob/master/docs/api/web-contents.md#webcontentssavepagefullpath-savetype-callback).
  335. #### .pdf(path, options)
  336. Saves a PDF to the specified `path`. Options are [here](https://github.com/electron/electron/blob/v1.4.4/docs/api/web-contents.md#contentsprinttopdfoptions-callback).
  337. #### .title()
  338. Returns the title of the current page.
  339. #### .url()
  340. Returns the url of the current page.
  341. #### .path()
  342. Returns the path name of the current page.
  343. ### Cookies
  344. #### .cookies.get(name)
  345. Gets a cookie by it's `name`. The url will be the current url.
  346. #### .cookies.get(query)
  347. Queries multiple cookies with the `query` object. If a `query.name` is set, it will return the first cookie it finds with that name, otherwise it will query for an array of cookies. If no `query.url` is set, it will use the current url. Here's an example:
  348. ```js
  349. // get all google cookies that are secure
  350. // and have the path `/query`
  351. nightmare
  352. .goto('http://google.com')
  353. .cookies.get({
  354. path: '/query',
  355. secure: true
  356. })
  357. .then(cookies => {
  358. // do something with the cookies
  359. })
  360. ```
  361. Available properties are documented here: https://github.com/atom/electron/blob/master/docs/api/session.md#sescookiesgetdetails-callback
  362. #### .cookies.get()
  363. Gets all the cookies for the current url. If you'd like get all cookies for all urls, use: `.get({ url: null })`.
  364. #### .cookies.set(name, value)
  365. Sets a cookie's `name` and `value`. This is the most basic form, and the url will be the current url.
  366. #### .cookies.set(cookie)
  367. Sets a `cookie`. If `cookie.url` is not set, it will set the cookie on the current url. Here's an example:
  368. ```js
  369. nightmare
  370. .goto('http://google.com')
  371. .cookies.set({
  372. name: 'token',
  373. value: 'some token',
  374. path: '/query',
  375. secure: true
  376. })
  377. // ... other actions ...
  378. .then(() => {
  379. // ...
  380. })
  381. ```
  382. Available properties are documented here: https://github.com/atom/electron/blob/master/docs/api/session.md#sescookiessetdetails-callback
  383. #### .cookies.set(cookies)
  384. Sets multiple cookies at once. `cookies` is an array of `cookie` objects. Take a look at the `.cookies.set(cookie)` documentation above for a better idea of what `cookie` should look like.
  385. #### .cookies.clear([name])
  386. Clears a cookie for the current domain. If `name` is not specified, all cookies for the current domain will be cleared.
  387. ```js
  388. nightmare
  389. .goto('http://google.com')
  390. .cookies.clear('SomeCookieName')
  391. // ... other actions ...
  392. .then(() => {
  393. // ...
  394. })
  395. ```
  396. #### .cookies.clearAll()
  397. Clears all cookies for all domains.
  398. ```js
  399. nightmare
  400. .goto('http://google.com')
  401. .cookies.clearAll()
  402. // ... other actions ...
  403. .then(() => {
  404. //...
  405. })
  406. ```
  407. ### Proxies
  408. Proxies are supported in Nightmare through [switches](#switches).
  409. If your proxy requires authentication you also need the [authentication](#authenticationuser-password) call.
  410. The following example not only demonstrates how to use proxies, but you can run it to test if your proxy connection is working:
  411. ```js
  412. import Nightmare from 'nightmare';
  413. const proxyNightmare = Nightmare({
  414. switches: {
  415. 'proxy-server': 'my_proxy_server.example.com:8080' // set the proxy server here ...
  416. },
  417. show: true
  418. });
  419. proxyNightmare
  420. .authentication('proxyUsername', 'proxyPassword') // ... and authenticate here before `goto`
  421. .goto('http://www.ipchicken.com')
  422. .evaluate(() => {
  423. return document.querySelector('b').innerText.replace(/[^\d\.]/g, '');
  424. })
  425. .end()
  426. .then((ip) => { // This will log the Proxy's IP
  427. console.log('proxy IP:', ip);
  428. });
  429. // The rest is just normal Nightmare to get your local IP
  430. const regularNightmare = Nightmare({ show: true });
  431. regularNightmare
  432. .goto('http://www.ipchicken.com')
  433. .evaluate(() =>
  434. document.querySelector('b').innerText.replace(/[^\d\.]/g, '');
  435. )
  436. .end()
  437. .then((ip) => { // This will log the your local IP
  438. console.log('local IP:', ip);
  439. });
  440. ```
  441. ### Promises
  442. By default, Nightmare uses default native ES6 promises. You can plug in your favorite [ES6-style promises library](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) like [bluebird](https://www.npmjs.com/package/bluebird) or [q](https://www.npmjs.com/package/q) for convenience!
  443. Here's an example:
  444. ```js
  445. var Nightmare = require('nightmare')
  446. Nightmare.Promise = require('bluebird')
  447. // OR:
  448. Nightmare.Promise = require('q').Promise
  449. ```
  450. You can also specify a custom Promise library per-instance with the `Promise` constructor option like so:
  451. ```js
  452. var Nightmare = require('nightmare')
  453. var es6Nightmare = Nightmare()
  454. var bluebirdNightmare = Nightmare({
  455. Promise: require('bluebird')
  456. })
  457. var es6Promise = es6Nightmare
  458. .goto('https://github.com/segmentio/nightmare')
  459. .then()
  460. var bluebirdPromise = bluebirdNightmare
  461. .goto('https://github.com/segmentio/nightmare')
  462. .then()
  463. es6Promise.isFulfilled() // throws: `TypeError: es6EndPromise.isFulfilled is not a function`
  464. bluebirdPromise.isFulfilled() // returns: `true | false`
  465. ```
  466. ### Extending Nightmare
  467. #### Nightmare.action(name, [electronAction|electronNamespace], action|namespace)
  468. You can add your own custom actions to the Nightmare prototype. Here's an example:
  469. ```js
  470. Nightmare.action('size', function(done) {
  471. this.evaluate_now(() => {
  472. const w = Math.max(
  473. document.documentElement.clientWidth,
  474. window.innerWidth || 0
  475. )
  476. const h = Math.max(
  477. document.documentElement.clientHeight,
  478. window.innerHeight || 0
  479. )
  480. return {
  481. height: h,
  482. width: w
  483. }
  484. }, done)
  485. })
  486. Nightmare()
  487. .goto('http://cnn.com')
  488. .size()
  489. .then(size => {
  490. //... do something with the size information
  491. })
  492. ```
  493. > Remember, this is attached to the static class `Nightmare`, not the instance.
  494. You'll notice we used an internal function `evaluate_now`. This function is different than `nightmare.evaluate` because it runs it immediately, whereas `nightmare.evaluate` is queued.
  495. An easy way to remember: when in doubt, use `evaluate`. If you're creating custom actions, use `evaluate_now`. The technical reason is that since our action has already been queued and we're running it now, we shouldn't re-queue the evaluate function.
  496. We can also create custom namespaces. We do this internally for `nightmare.cookies.get` and `nightmare.cookies.set`. These are useful if you have a bundle of actions you want to expose, but it will clutter up the main nightmare object. Here's an example of that:
  497. ```js
  498. Nightmare.action('style', {
  499. background(done) {
  500. this.evaluate_now(
  501. () => window.getComputedStyle(document.body, null).backgroundColor,
  502. done
  503. )
  504. }
  505. })
  506. Nightmare()
  507. .goto('http://google.com')
  508. .style.background()
  509. .then(background => {
  510. // ... do something interesting with background
  511. })
  512. ```
  513. You can also add custom Electron actions. The additional Electron action or namespace actions take `name`, `options`, `parent`, `win`, `renderer`, and `done`. Note the Electron action comes first, mirroring how `.evaluate()` works. For example:
  514. ```javascript
  515. Nightmare.action(
  516. 'clearCache',
  517. (name, options, parent, win, renderer, done) => {
  518. parent.respondTo('clearCache', done => {
  519. win.webContents.session.clearCache(done)
  520. })
  521. done()
  522. },
  523. function(done) {
  524. this.child.call('clearCache', done)
  525. }
  526. )
  527. Nightmare()
  528. .clearCache()
  529. .goto('http://example.org')
  530. //... more actions ...
  531. .then(() => {
  532. // ...
  533. })
  534. ```
  535. ...would clear the browsers cache before navigating to `example.org`.
  536. See [this document](https://github.com/rosshinkley/nightmare-examples/blob/master/docs/beginner/action.md) for more details on creating custom actions.
  537. #### .use(plugin)
  538. `nightmare.use` is useful for reusing a set of tasks on an instance. Check out [nightmare-swiftly](https://github.com/segmentio/nightmare-swiftly) for some examples.
  539. #### Custom preload script
  540. If you need to do something custom when you first load the window environment, you
  541. can specify a custom preload script. Here's how you do that:
  542. ```js
  543. import path from 'path'
  544. const nightmare = Nightmare({
  545. webPreferences: {
  546. preload: path.resolve('custom-script.js')
  547. //alternative: preload: "absolute/path/to/custom-script.js"
  548. }
  549. })
  550. ```
  551. The only requirement for that script is that you'll need the following prelude:
  552. ```js
  553. window.__nightmare = {}
  554. __nightmare.ipc = require('electron').ipcRenderer
  555. ```
  556. To benefit of all of nightmare's feedback from the browser, you can instead copy the contents of nightmare's [preload script](lib/preload.js).
  557. #### Storage Persistence between nightmare instances
  558. By default nightmare will create an in-memory partition for each instance. This means that any localStorage or cookies or any other form of persistent state will be destroyed when nightmare is ended. If you would like to persist state between instances you can use the [webPreferences.partition](http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions) api in electron.
  559. ```js
  560. import Nightmare from 'nightmare';
  561. nightmare = Nightmare(); // non persistent paritition by default
  562. yield nightmare
  563. .evaluate(() => {
  564. window.localStorage.setItem('testing', 'This will not be persisted');
  565. })
  566. .end();
  567. nightmare = Nightmare({
  568. webPreferences: {
  569. partition: 'persist: testing'
  570. }
  571. });
  572. yield nightmare
  573. .evaluate(() => {
  574. window.localStorage.setItem('testing', 'This is persisted for other instances with the same paritition name');
  575. })
  576. .end();
  577. ```
  578. If you specify a `null` paritition then it will use the electron default behavior (persistent) or any string that starts with `'persist:'` will persist under that partition name, any other string will result in in-memory only storage.
  579. ## Usage
  580. #### Installation
  581. Nightmare is a Node.js module, so you'll need to [have Node.js installed](http://nodejs.org/). Then you just need to `npm install` the module:
  582. ```bash
  583. $ npm install --save nightmare
  584. ```
  585. #### Execution
  586. Nightmare is a node module that can be used in a Node.js script or module. Here's a simple script to open a web page:
  587. ```js
  588. import Nightmare from 'nightmare';
  589. const nightmare = Nightmare();
  590. nightmare.goto('http://cnn.com')
  591. .evaluate(() => {
  592. return document.title;
  593. })
  594. .end()
  595. .then((title) => {
  596. console.log(title);
  597. })
  598. ```
  599. If you save this as `cnn.js`, you can run it on the command line like this:
  600. ```bash
  601. npm install --save nightmare
  602. node cnn.js
  603. ```
  604. #### Common Execution Problems
  605. Nightmare heavily relies on [Electron](http://electron.atom.io/) for heavy lifting. And Electron in turn relies on several UI-focused dependencies (eg. libgtk+) which are often missing from server distros.
  606. For help running nightmare on your server distro check out [How to run nightmare on Amazon Linux and CentOS](https://gist.github.com/dimkir/f4afde77366ff041b66d2252b45a13db) guide.
  607. #### Debugging
  608. There are three good ways to get more information about what's happening inside the headless browser:
  609. 1. Use the `DEBUG=*` flag described below.
  610. 2. Pass `{ show: true }` to the [nightmare constructor](#nightmareoptions) to have it create a visible, rendered window where you can watch what is happening.
  611. 3. Listen for [specific events](#onevent-callback).
  612. To run the same file with debugging output, run it like this `DEBUG=nightmare node cnn.js` (on Windows use `set DEBUG=nightmare & node cnn.js`).
  613. This will print out some additional information about what's going on:
  614. ```bash
  615. nightmare queueing action "goto" +0ms
  616. nightmare queueing action "evaluate" +4ms
  617. Breaking News, U.S., World, Weather, Entertainment & Video News - CNN.com
  618. ```
  619. ##### Debug Flags
  620. All nightmare messages
  621. `DEBUG=nightmare*`
  622. Only actions
  623. `DEBUG=nightmare:actions*`
  624. Only logs
  625. `DEBUG=nightmare:log*`
  626. ## Additional Resources
  627. * [Ross Hinkley's Nightmare Examples](https://github.com/rosshinkley/nightmare-examples) is a great resource for setting up nightmare, learning about custom actions, and avoiding common pitfalls.
  628. * [Nightmarishly good scraping](https://hackernoon.com/nightmarishly-good-scraping-with-nightmare-js-and-async-await-b7b20a38438f) is a great tutorial by [Ændrew Rininsland](https://twitter.com/@aendrew) on getting up & running with Nightmare using real-life data.
  629. ## Tests
  630. Automated tests for nightmare itself are run using [Mocha](http://mochajs.org/) and Chai, both of which will be installed via `npm install`. To run nightmare's tests, just run `make test`.
  631. When the tests are done, you'll see something like this:
  632. ```bash
  633. make test
  634. ․․․․․․․․․․․․․․․․․․
  635. 18 passing (1m)
  636. ```
  637. Note that if you are using `xvfb`, `make test` will automatically run the tests under an `xvfb-run` wrapper. If you are planning to run the tests headlessly without running `xvfb` first, set the `HEADLESS` environment variable to `0`.
  638. ## License (MIT)
  639. ```
  640. WWWWWW||WWWWWW
  641. W W W||W W W
  642. ||
  643. ( OO )__________
  644. / | \
  645. /o o| MIT \
  646. \___/||_||__||_|| *
  647. || || || ||
  648. _||_|| _||_||
  649. (__|__|(__|__|
  650. ```
  651. Copyright (c) 2015 Segment.io, Inc. <mailto:friends@segment.com>
  652. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  653. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  654. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.