Skip to content

Latest commit

 

History

History
80 lines (60 loc) · 3.58 KB

README.MD

File metadata and controls

80 lines (60 loc) · 3.58 KB

chromium-binary Build Status XO code style npm version Platforms

Chromium binaries for your NodeJS project

This is fork of node-chromium by Dmytro Tolstyi.

node-chromium allows you to easily add Chromium binaries of desired version to your project and use it for automation, testing, web scraping or just for fun. Chromium version will correspond to package version, i.e. if package version is 60.0.3112-0 then chromium 60.0.3112.0 will be installed.

Why Chromium?

Chromium is an open-source web browser developed and maintained by The Chromium Project. Google Chrome, also released in 2008, is a proprietary web browser developed and maintained by Google. The reason why Chrome and Chromium are tied to each other is that Chome borrows Chromium’s source code.
The main benefit of using Chromium is that it doesn't include all the proprietary modifications made by Google, thus it's more lightweight and more suitable for automation purposes.
You can see full list of differences in Fossbytes article.

Usage

Depending on your needs, you can install module into devDependencies (--save-dev) or production dependencies (--save).

npm install --save chromium-binary

During the installation process node-chromium will download corresponded chromium version and extract into libraries folder. As soon as installation is finished, you are ready to use Chromium in your project:

const chromiumBinary = require('chromium-binary');
const {execFile} = require('child_process');

execFile(chromiumBinary.path, ['https://google.com'], err => {
	console.log('Hello Google!');
});

Selenium WebDriver Headless (without UI) tests

It's extremely easy to use node-chromium with selenium-webdriver to perform e2e tests without spawning browser UI. First, install all dependencies

npm install --save chromium-binary@60.0.3112-0 chromedriver@2.33.0 selenium-webdriver@3.6.0

After the installation is finished, create simple script that opens Google Search home page and takes it's screenshot in headless mode.

const fs = require('fs');
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromiumBinary = require('chromium-binary');
require('chromedriver');

async function start() {

    let options = new chrome.Options();
    options.setChromeBinaryPath(chromiumBinary.path);
    options.addArguments('--headless');
    options.addArguments('--disable-gpu');
    options.addArguments('--window-size=1280,960');

    const driver = await new webdriver.Builder()
        .forBrowser('chrome')
        .setChromeOptions(options)
        .build();
		
    await driver.get('http://google.com');
    console.log('Hello Google!');
    await takeScreenshot(driver, 'google-start-page');
    
    await driver.quit();
}

async function takeScreenshot(driver, name) {
	await driver.takeScreenshot().then((data) => {
        fs.writeFileSync(name + '.png', data, 'base64');
        console.log('Screenshot is saved');
    });
}

start();

License

MIT