All Projects → moos → chromate

moos / chromate

Licence: MIT License
Automate Headless Chrome.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to chromate

bookjs-eazy
web print / html to pdf so eazy ,HTML自动分页插件。用于生成PDF,前端WEB打印生成PDF或后端wkhtmltopdf、chrome headless生成
Stars: ✭ 99 (+175%)
Mutual labels:  chrome-headless
siteshooter
📷 Automate full website screenshots and PDF generation with multiple viewport support.
Stars: ✭ 63 (+75%)
Mutual labels:  phantomjs
pyCreeper
一个用来快速提取网页内容的信息采集(爬虫)框架, 实现了对网页的动态加载与控制。
Stars: ✭ 25 (-30.56%)
Mutual labels:  phantomjs
ComicSpider
动漫之家漫画站电脑版原图爬虫
Stars: ✭ 67 (+86.11%)
Mutual labels:  phantomjs
omg image
Generate PNG previews for HTML snippets (html/css/js). Any complexity.
Stars: ✭ 28 (-22.22%)
Mutual labels:  chrome-headless
screenie-server
A Node server with a pool of Puppeteer (Chrome headless) instances for scalable screenshot generation.
Stars: ✭ 19 (-47.22%)
Mutual labels:  chrome-headless
phantomic
Pipe stdin to Phantom.JS
Stars: ✭ 20 (-44.44%)
Mutual labels:  phantomjs
TqExtension
Test your Drupal 7 (D8 in progress) sites easier with TqExtension for Behat.
Stars: ✭ 13 (-63.89%)
Mutual labels:  phantomjs
phantom-lord
Handy API for Headless Chromium
Stars: ✭ 24 (-33.33%)
Mutual labels:  phantomjs
ionic-docker
An ionic image for CI
Stars: ✭ 56 (+55.56%)
Mutual labels:  chrome-headless
www-mechanize-phantomjs
Automate Javascript/Ajax websites with Perl and PhantomJS
Stars: ✭ 19 (-47.22%)
Mutual labels:  phantomjs
pyderman
Install Selenium-compatible Chrome/Firefox/Opera/PhantomJS/Edge webdrivers automatically.
Stars: ✭ 24 (-33.33%)
Mutual labels:  phantomjs
crawlkit
A crawler based on Phantom. Allows discovery of dynamic content and supports custom scrapers.
Stars: ✭ 23 (-36.11%)
Mutual labels:  phantomjs
jazeee-meteor-spiderable
Fork of Meteor Spiderable with longer timeout, caching, better server handling
Stars: ✭ 33 (-8.33%)
Mutual labels:  phantomjs
intransient capybara
A set of improvements to Minitest/Capybara/Poltergeist/PhantomJS test stack that reduces the occurrence of transient failures.
Stars: ✭ 25 (-30.56%)
Mutual labels:  phantomjs
doffy
a web auto run lib base on chrome headless
Stars: ✭ 13 (-63.89%)
Mutual labels:  chrome-headless
chesf
CHeSF is the Chrome Headless Scraping Framework, a very very alpha code to scrape javascript intensive web pages
Stars: ✭ 18 (-50%)
Mutual labels:  chrome-headless
img-cli
An interactive Command-Line Interface Build in NodeJS for downloading a single or multiple images to disk from URL
Stars: ✭ 15 (-58.33%)
Mutual labels:  phantomjs
cuic
Clojure UI testing with Chrome
Stars: ✭ 23 (-36.11%)
Mutual labels:  chrome-headless
kick-off-web-scraping-python-selenium-beautifulsoup
A tutorial-based introduction to web scraping with Python.
Stars: ✭ 18 (-50%)
Mutual labels:  phantomjs

Automate Headless Chrome -- start/stop Chrome instances, open & close tabs, and communicate with the target page.

Compatibility

  • You must use version >= 59 of Chrome (currently that means Chrome Beta) or use Chrome Canary.
  • Canary isn't supported on Linux platform.

Install

npm install chromate
npm run sample

Use

let {Chrome, Tab} = require('chromate');

// start a headless Chrome process
Chrome.start().then(chrome => {
  let tab = new Tab({
    verbose: true,
    failonerror: false
  });
  
  tab.open(targetUrl)
  .then(() => tab.evaluate('testResults'))
  .then(res => console.log) // results...
  .then(() => tab.close())
  .then(() => {
    Chrome.kill(chrome);
    process.exit(0);
  });
});

Page events

Handle events, including any chrome-remote-interface events.

new Tab(options)
 .on('ready', (tab) => console.log('tab is ready', tab.client.target.id))
 .on('load', () => console.log('page loaded'))
 .on('console', (args) => console.log('console.* called', args))
 .on('Network.requestWillBeSent', param => console.log('Getting resource', param.request.url))
 .once('Runtime.consoleAPICalled', param => console.log('Runtime.consoleAPICalled called', param))
 .open(targetUrl);

The ready event is fired once when the target client is ready (this overrides the CDP ready event). The target may fire any number of custom events.

Custom events

A target page may communicate back to the controlling process by calling console.debug(message), where message is {event, data}. This is useful for running automated tests, such as for replacing PhantomJS. See phantom-menace for example.

// useful for short messages (< 100 chars)
console.debug({
  event: 'done',
  data: JSON.stringify({foo: 1}) // must be stringify'd
});

// then, in runner process
new Tab()
  .on('done', res => console.log); // {event: 'done', data: foo:1}}

A special function __chromate(message) is injected in target page to facilitate this, so that the above can be replaced by:

// in target (useful for any length message)
if (window.__chromate) __chromate({event: 'done', data: {foo:1}});;

The format of the message is flexible, but should be sensible. If no event property is found in the message, a 'data' event is triggered.

// in target
__chromate('foo');
console.debug({a:1});

// in runner
tab.on('data', res => console.log); // 'foo' and {a:1}

Script injection

Often it's useful for the running script to inject custom JS into the target page. This can be done thorough Page.addScriptToEvaluateOnLoad() or the Runtime.evaluate() method. Two helper methods are provided: tab.evaluate() and tab.execute():

new Tab()
  .on('done', (res, tab) => {
     tab.evaluate('JSON.stringify(__coverage__)')
       .then(result => console.log(result))
       .then(() => tab.close());
  })
  .open(targetUrl);

Or execute a (named) function in target.

new Tab()
  .open(targetUrl)
  .then(tab => {
    tab.execute('getResult').then(res => console.log);
  })
  
// in target
function getResult() {
  return JSON.stringify(result);
}

tab.execute() takes additional parameters to pass as arguments to the function. If the function is expected to return a Promise, pass a {awaitPromise: true} as the last argument.

API

See API docs.

Simple CLI

Usage:

$ chromate
Usage: chromate start [<chrome flags>] | list | kill <pid> ... | killall | version | open <url> | 
    list-tabs | close <tabId> | close-tabs  [--canary | --verbose | -v]

Chrome process control

$ chromate start --window-size=800x600 --canary
42706: /Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary
  --remote-debugging-port=9222 --headless --disable-gpu
  --user-data-dir=/var/folders/jl/zr54cdxs08l1djw8s4ws2t540000gn/T/chrome-PdvzfF 
  --window-size=800x600 --canary --disable-translate --disable-extensions
  --disable-background-networking --safebrowsing-disable-auto-update --disable-sync
  --metrics-recording-only --disable-default-apps --no-first-run 
  --disable-background-timer-throttling --disable-renderer-backgrounding
  --disable-device-discovery-notifications

$ chromate list
[ { pid: '42706',
    command: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
    arguments:
     [ '--remote-debugging-port=9222',
       '--headless',
       '--disable-gpu',
       '--user-data-dir=/var/folders/jl/zr54cdxs08l1djw8s4ws2t540000gn/T/chrome-PdvzfF',
       '--window-size=800x600',
       '--canary',
       '--disable-translate',
       '--disable-extensions',
       '--disable-background-networking',
       '--safebrowsing-disable-auto-update',
       '--disable-sync',
       '--metrics-recording-only',
       '--disable-default-apps',
       '--no-first-run',
       '--disable-background-timer-throttling',
       '--disable-renderer-backgrounding',
       '--disable-device-discovery-notifications' ],
    ppid: '1' } ]
    
$ chromate killall 
2

killall returns the number of processes (including sub-processes) killed.

For list of Chrome Headless flags, see here. Of course, any Chrome flag can be specified.

To use a custom Chrome path and/or port, use:

$ CHROME_BIN=/path/to/chrome CHROME_PORT=9226 chromate start

Chrome tab control

$ chromate open https://github.com
{ description: '',
  devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:9222/devtools/page/904ddfa4-2344-4e45-a625-8261ffbee251',
  id: '904ddfa4-2344-4e45-a625-8261ffbee251',
  title: '',
  type: 'page',
  url: 'about:blank',
  webSocketDebuggerUrl: 'ws://localhost:9222/devtools/page/904ddfa4-2344-4e45-a625-8261ffbee251' }

$ chromate list-tabs
[ { description: '',
    devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:9222/devtools/page/e97b0e1e-1fb5-41be-83d2-bdb9fbc406bc',
    id: 'e97b0e1e-1fb5-41be-83d2-bdb9fbc406bc',
    title: 'The world&#39;s leading software development platform · GitHub',
    type: 'page',
    url: 'https://github.com/',
    webSocketDebuggerUrl: 'ws://localhost:9222/devtools/page/e97b0e1e-1fb5-41be-83d2-bdb9fbc406bc' },
  { description: '',
    devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:9222/devtools/page/e4c16358-7670-4deb-8b2e-29f802e599a3',
    id: 'e4c16358-7670-4deb-8b2e-29f802e599a3',
    title: 'about:blank',
    type: 'page',
    url: 'about:blank',
    webSocketDebuggerUrl: 'ws://localhost:9222/devtools/page/e4c16358-7670-4deb-8b2e-29f802e599a3' } ]

$ chromate close-tabs
2

Test

# npm i -g mocha  (if you don't already have it)
npm test

Thanks and references

Change log

  • v0.3.5 - Readme update.
  • v0.3.4 - Added Chrome.settings.userDataDir. By default a temporary user data dir is used and cleaned up.
  • v0.3.3 - fixed 'ps-node' reference
  • v0.3.2 - fixed internal print() method
  • v0.3.1 - added events 'abort', 'exception', and 'console.*'. Export chromate.version.
  • v0.3.0 - tab.open(url) takes url rather than constructor. tab.execute can take a local function. Use ps-moos with fix for spaces in path.
  • v0.2.0 - Added expression and function evaluation and __chromate global for general message passing. Events now get complete message, not just the data part. (May 2017)
  • v0.1.x - Initial version (May 2017)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].