├── .gitignore ├── README.md ├── package.json ├── LICENSE ├── index.js └── configs └── default.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nightmare-runner 2 | 3 | Allows you to easily run nightmare based on a declarative config file. 4 | 5 | [See a default config to see what options are available](https://github.com/tscanlin/nightmare-runner/blob/master/configs/default.js) 6 | 7 | ``` 8 | npm i nightmare-runner 9 | ``` 10 | 11 | ``` 12 | nightmare-runner --config ./configs/default.js 13 | ``` 14 | 15 | [Go to the nightmare.js docs to see what methods are available and what params they expect](https://github.com/segmentio/nightmare#interact-with-the-page) 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nightmare-runner", 3 | "version": "1.0.2", 4 | "description": "Nightmare.js runner", 5 | "main": "index.js", 6 | "bin": { 7 | "nightmare-runner": "index.js" 8 | }, 9 | "scripts": { 10 | "v-patch": "npm version patch && git push --tags && npm publish && git push", 11 | "v-minor": "npm version minor && git push --tags && npm publish && git push", 12 | "v-major": "npm version major && git push --tags && npm publish && git push" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/tscanlin/nightmare-runner.git" 17 | }, 18 | "keywords": [ 19 | "nightmare", 20 | "runner" 21 | ], 22 | "author": "Tim Scanlin", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/tscanlin/nightmare-runner/issues" 26 | }, 27 | "homepage": "https://github.com/tscanlin/nightmare-runner#readme", 28 | "dependencies": { 29 | "nightmare": "^2.10.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tim Scanlin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const Nightmare = require('nightmare') 4 | const path = require('path') 5 | const rootPath = path.resolve('.') 6 | 7 | // Config. 8 | let configFile = './configs/default.js' 9 | if (process.argv.length) { 10 | for (var i = 0; i < process.argv.length; i++) { 11 | switch (process.argv[i]) { 12 | case '--config': 13 | configFile = process.argv[i + 1] 14 | break 15 | default: 16 | break 17 | } 18 | } 19 | } 20 | 21 | const config = require(path.join(rootPath, configFile)) 22 | run(config) 23 | 24 | // Run function. 25 | function run (conf) { 26 | const windowKeys = Object.keys(conf.windows) 27 | 28 | // Make tasks. 29 | const tasks = windowKeys.map((key) => { 30 | const window = conf.windows[key] 31 | const nightmare = Nightmare({ show: window.show }) 32 | 33 | window.actions.reduce((p, c) => { 34 | if (c.method) { 35 | return p[c.method].apply(p, c.args) 36 | } else { 37 | return p 38 | } 39 | }, nightmare) 40 | 41 | return nightmare 42 | }) 43 | 44 | return new Promise((resolve, reject) => { 45 | let resultArray = [] 46 | 47 | // Iterate through tasks. 48 | const results = tasks 49 | // Empty promise at the end for an extra reduce iteration. 50 | .concat(() => Promise.resolve()) 51 | .reduce((p, c) => { 52 | return p.then((data) => { 53 | // Add resolved promise data to the array. 54 | data && resultArray.push(data) 55 | return c 56 | }, (err) => { 57 | err && resultArray.push(err) 58 | }) 59 | }, Promise.resolve(false)) 60 | .then((d) => { 61 | resolve(resultArray) 62 | }) 63 | 64 | return results 65 | }).then((data) => { 66 | console.log(data) 67 | }) 68 | } 69 | -------------------------------------------------------------------------------- /configs/default.js: -------------------------------------------------------------------------------- 1 | 2 | const win = { 3 | show: true, 4 | 5 | viewport: { 6 | width: 800, 7 | height: 800, 8 | }, 9 | 10 | actions: [ 11 | { 12 | method: 'goto', 13 | args: ['https://duckduckgo.com'] 14 | }, 15 | { 16 | method: 'type', 17 | args: ['#search_form_input_homepage', 'github nightmare'] 18 | }, 19 | { 20 | method: 'click', 21 | args: ['#search_button_homepage'] 22 | }, 23 | { 24 | method: 'wait', 25 | args: ['#zero_click_wrapper .c-info__title a'] 26 | }, 27 | { 28 | method: 'evaluate', 29 | args: [ 30 | function () { 31 | return document.querySelector('#zero_click_wrapper .c-info__title a').href; 32 | } 33 | ] 34 | }, 35 | { 36 | method: 'end', 37 | }, 38 | // { 39 | // method: 'then', 40 | // args: [ 41 | // function (result) { 42 | // console.log(result); 43 | // return result 44 | // } 45 | // ] 46 | // } 47 | ] 48 | } 49 | 50 | const altActions = [ 51 | { 52 | method: 'goto', 53 | args: ['https://google.com'] 54 | }, 55 | { 56 | method: 'type', 57 | args: ['#lst-ib', 'github nightmare'] 58 | }, 59 | { 60 | method: 'click', 61 | args: ['[value="Google Search"]'] 62 | }, 63 | { 64 | method: 'wait', 65 | args: ['#center_col'] 66 | }, 67 | { 68 | method: 'wait', 69 | args: [100] 70 | }, 71 | { 72 | method: 'evaluate', 73 | args: [ 74 | function () { 75 | return document.title; 76 | } 77 | ] 78 | }, 79 | { 80 | method: 'end', 81 | } 82 | ] 83 | 84 | module.exports = { 85 | debug: true, 86 | // order: ['win1'], 87 | windows: { 88 | win1: win, 89 | win2: Object.assign({}, win, { 90 | actions: altActions 91 | }), 92 | } 93 | } 94 | --------------------------------------------------------------------------------