<commit_before>/**
 * Selenium Screen Master
 * Help you create screenshots and compare they
 */

/*
 WARNING! At first install libraries for nodeJs canvas
 Ubuntu: sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
 OS X: brew install pkg-config cairo libpng jpeg giflib
 */

/*global WebDriverHTMLElement*/

// external modules
const WebDriver = require('selenium-webdriver');
const Canvas = require('canvas');
const resemble = require('node-resemble');

// system modules
const path = require('path');
const fs = require('fs');

const byCss = WebDriver.By.css;

const Image = Canvas.Image;
const BASE64_IMAGE_PREFIX = 'data:image/png;base64,';

const MODE = {
    TEST: 'TEST',
    COLLECT: 'COLLECT'
};

const CONSTANT = {
    WAIT: {
        SCREENSHOT: 5e3
    }
};

class Ssm {

    /**
     * Constructor of class (c) Captain Obvious
     */
    constructor() {

        // set/add default properties
        this.reset();

        /**
         * Available modes
         * @public
         * @type {Object}
         */
        this.MODE = MODE;

        /**
         * Constants
         * @public
         * @type {Object}
         */
        this.CONSTANT = CONSTANT;

    }

    /**
     * Set folder to save collected images
     * @public
     * @param {string} pathToReferenceFolder - path to reference folder
     * @return void
     */
    setPathToReferenceFolder(pathToReferenceFolder) {
        this._attr.pathToReferenceFolder = pathToReferenceFolder;
    }

    /**
     * Get path to reference folder
     * @public
     * @return {string}
     */
    getPathToReferenceFolder() {
        return this._attr.pathToReferenceFolder;
    }

    /**
     * @public
     * @param {WebDriver} driver
     * @return void
     */
    setDriver(driver) {
        this._attr.driver = driver;
    }

    /**
     * @public
     * @return {WebDriver}
     */
    getDriver() {
        return this._attr.driver;
    }

    /**
     * Compare images
     * @public
     * @param {string} selector - usual css selector
     * @param {string} data.image - path/to/your/image.png
     * @param {string} data.mode - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfSelector(selector, data) {

        const ssm = this;
        const driver = ssm.getDriver();

        return driver.wait(
            ssm.compareOfElement(driver.findElement(byCss(selector)), data),
            ssm.CONSTANT.WAIT.SCREENSHOT
        );

    }

    /**
     * Compare images
     * @public
     * @param {WebDriverHTMLElement} element
     * @param {string} data.image - path/to/your/image.png
     * @param {string} data.mode - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfElement(element, data) {

        const ssm = this;
        const driver = ssm.getDriver();

        return driver.wait(
            Promise
                .all([
                    element.getLocation(),
                    element.getSize()
                ])
                .then(dataList => {

                    let location = dataList[0],
                        size = dataList[1];

                    return ssm.compareOfArea(location.x, location.y, size.width, size.height, data)

                }),
            ssm.CONSTANT.WAIT.SCREENSHOT
        );

    }

    /**
     * Compare images
     * @public
     * @params {numbers} x, y, width, height - area's location
     * @param {string} data.image - path/to/your/image.png
     * @param {string} data.mode - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfArea(x, y, width, height, data) {

        const ssm = this;

        const driver = ssm.getDriver();

        const image = data.image;

        const mode = data.mode || MODE.TEST;

        const pathToImage = path.join(ssm.getPathToReferenceFolder(), image);

        let actualImage, expectImage;

        return driver.wait(
            ssm
                .takeScreenshotOfArea(x, y, width, height)
                .then(image => {

                    actualImage = image;

                    switch (mode) {
                        case MODE.TEST:
                            return pngToBase64(pathToImage).then(image => expectImage = image);
                            break;
                        case MODE.COLLECT:
                            return writeBase64ToFile(pathToImage, image);<commit_msg>add set/getMode() to optimize API<commit_after>/**
 * Selenium Screen Master
 * Help you create screenshots and compare they
 */

/*
 WARNING! At first install libraries for nodeJs canvas
 Ubuntu: sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
 OS X: brew install pkg-config cairo libpng jpeg giflib
 */

/*global WebDriverHTMLElement*/

// external modules
const WebDriver = require('selenium-webdriver');
const Canvas = require('canvas');
const resemble = require('node-resemble');

// system modules
const path = require('path');
const fs = require('fs');

const byCss = WebDriver.By.css;

const Image = Canvas.Image;
const BASE64_IMAGE_PREFIX = 'data:image/png;base64,';

const MODE = {
    TEST: 'TEST',
    COLLECT: 'COLLECT'
};

const CONSTANT = {
    WAIT: {
        SCREENSHOT: 5e3
    }
};

class Ssm {

    /**
     * Constructor of class (c) Captain Obvious
     */
    constructor() {

        // set/add default properties
        this.reset();

        /**
         * Default mode
         * @private
         * @type {String}
         */
        this._mode = MODE.TEST;

        /**
         * Available modes
         * @public
         * @type {Object}
         */
        this.MODE = MODE;

        /**
         * Constants
         * @public
         * @type {Object}
         */
        this.CONSTANT = CONSTANT;

    }

    // TODO: this functional to documentation
    /**
     * Set mode of ssm: MODE.TEST or MODE.COLLECT
     * @public
     * @param {string} mode
     * @return void
     */
    setMode(mode) {
        this._mode = mode;
    }

    /**
     * Get mode of ssm: MODE.TEST or MODE.COLLECT
     * @public
     * @return {string} mode
     */
    getMode() {
        return this._mode;
    }

    /**
     * Set folder to save collected images
     * @public
     * @param {string} pathToReferenceFolder - path to reference folder
     * @return void
     */
    setPathToReferenceFolder(pathToReferenceFolder) {
        this._attr.pathToReferenceFolder = pathToReferenceFolder;
    }

    /**
     * Get path to reference folder
     * @public
     * @return {string}
     */
    getPathToReferenceFolder() {
        return this._attr.pathToReferenceFolder;
    }

    /**
     * @public
     * @param {WebDriver} driver
     * @return void
     */
    setDriver(driver) {
        this._attr.driver = driver;
    }

    /**
     * @public
     * @return {WebDriver}
     */
    getDriver() {
        return this._attr.driver;
    }

    /**
     * Compare images
     * @public
     * @param {string} selector - usual css selector
     * @param {string} data.image - path/to/your/image.png
     * @param {string} data.mode - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfSelector(selector, data) {

        const ssm = this;
        const driver = ssm.getDriver();

        return driver.wait(
            ssm.compareOfElement(driver.findElement(byCss(selector)), data),
            ssm.CONSTANT.WAIT.SCREENSHOT
        );

    }

    /**
     * Compare images
     * @public
     * @param {WebDriverHTMLElement} element
     * @param {string} data.image - path/to/your/image.png
     * @param {string} data.mode - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfElement(element, data) {

        const ssm = this;
        const driver = ssm.getDriver();

        return driver.wait(
            Promise
                .all([
                    element.getLocation(),
                    element.getSize()
                ])
                .then(dataList => {

                    let location = dataList[0],
                        size = dataList[1];

                    return ssm.compareOfArea(location.x, location.y, size.width, size.height, data)

                }),
            ssm.CONSTANT.WAIT.SCREENSHOT
        );

    }

    // TODO: this functional to documentation
    /**
     * Compare images
     * @public
     * @params {numbers} x, y, width, height - area's location
     * @param {string} data.image - path/to/your/image.png || data {string} path to image
     * @param {string} [data.mode] - mode of comparing - 'COLLECT' or 'TEST'
     * @return {promise} will resolve with comparing data
     */
    compareOfArea(x, y, width, height, data) {

        const ssm = this;

        const driver = ssm.getDriver();

        if (typeof data === 'string') {
            data = {
                image: data,
                mode: ssm.getMode()
            }
        }

        const image = data.image;

        const mode = data.mode || ssm.getMode();

        const pathToImage = path.join(ssm.getPathToReferenceFolder(), image);

        let actualImage, expectImage;

        return driver.wait(
            ssm
                .takeScreenshotOfArea(x, y, width, height)
                .then(image => {

                    actualImage = image;

                    switch (mode) {
                        case MODE.TEST:
                            return pngToBase64(pathToImage).then(image => expectImage = image);
                            break;
                        case MODE.COLLECT:
                            return writeBase64ToFile(pathToImage, image);
{'fraction_non_alphanumeric': 0.11207768561602266, 'fraction_numerical': 0.0032369006676107627, 'mean_word_length': 1.7282008830022075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1574947', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2805, 'n_words': 714}