All files / lib steps.js

95.74% Statements 90/94
85.29% Branches 29/34
100% Functions 6/6
97.8% Lines 89/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274                1x 1x   1x 1x 1x 1x 1x 1x   1x 1x   1x                                                     7x 1x 1x     6x 6x   6x   6x   6x 6x 6x   6x 6x 6x   6x 5x         5x   5x 5x   5x                                       6x 1x 1x     5x 5x   5x 5x   5x 5x   5x 4x         4x   4x 4x   4x                               7x   7x     6x 6x   6x   6x 3x     2x         4x 4x   4x                               7x 2x 2x     5x 5x   5x 5x   5x   5x 4x         4x 4x   4x                       6x 6x 6x       6x   3x   3x     3x     2x 2x 2x     2x         3x     5x 5x   5x                               6x   6x 6x   6x             1x  
"use strict";
/**
 * Steps for video recording.
 *
 * @mixin VideoSteps
 * @prop {VideoRecorder} video - Video recorder instance.
 */
 
var fs = require("fs");
var path = require("path");
 
var _ = require("lodash");
var expect = require("chai").expect;
var resolution = require("screen-resolution");
var uuid = require("uuid/v4");
var U = require("glace-utils");
var LOG = U.logger;
 
require("./fixtures");
var VideoRecorder = require("./video");
 
var VideoSteps = {
    /* modules */
    __fs: fs,
    __resolution: resolution,
    __VideoRecorder: VideoRecorder,
 
    startVideo: async function (opts) {
        /**
         * Step to start video recording. Step recall will be skipped if video
         *  recording wasn't stopped before.
         *
         * @async
         * @memberOf VideoSteps
         * @method startVideo
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {string} [opts.name] - File name. Extension `.avi` will be
         *  added automatically. Default name will be generated with `uuid`
         *  algorithm.
         * @arg {string} [opts.dir] - Folder to save video.
         * @arg {boolean} [opts.check=true] - Flag to check that video recording
         *  was launched.
         * @return {Promise<boolean>} `true` if step was executed, `false` if
         *  was skipped.
         * @throws {AssertionError} If video recording wasn't launched.
         */
 
        if (this._isVideoStarted) {
            LOG.warn("Step to start video recording was passed already");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        LOG.info("Starting video recording...");
 
        this.video = this.video || new this.__VideoRecorder();
 
        var videoOpts = await this._videoLocation();
        videoOpts.path = this._makeVideoPath(opts);
        allure.step(`Record video to ${videoOpts.path}`);
 
        this.video.configure(videoOpts);
        this.video.start();
        await this.pause(1, "it needs a time to start recording");
 
        if (check) {
            expect(this.video.isRunning,
                "Video recording wasn't launched")
                .to.be.true;
        };
 
        this._isVideoStarted = true;
 
        LOG.info("Video recording is started");
        allure.pass();
 
        return true;
    },
 
    stopVideo: async function (opts) {
        /**
         * Step to stop video recording. Step call will be skipped if video
         *  recording wasn't launched before.
         *
         * @async
         * @memberOf VideoSteps
         * @method stopVideo
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that video recording
         *  was stopped.
         * @return {Promise<string>} Path to recorded video.
         * @return {Promise<boolean>} `false` if step was skipped.
         * @throws {AssertionError} If video recording wasn't stopped.
         */
 
        if (!this._isVideoStarted) {
            LOG.warn("Step to start video recording wasn't passed yet");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Stop video recording");
        LOG.info("Stopping video recording...");
 
        await this.pause(1, "it needs a time to gather latest frames");
        await this.video.stop();
 
        if (check) {
            expect(this.video.isRunning,
                "Video recording was still running")
                .to.be.false;
        };
 
        this._isVideoStarted = false;
 
        LOG.info("Video recording is stopped");
        allure.pass();
 
        return this.video.filePath;
    },
 
    getVideo: function (opts) {
        /**
         * Step to get path to recorded video.
         *
         * @memberOf VideoSteps
         * @method getVideo
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that video is recorded
         *  and path exists.
         * @return {string} Path to recorded video.
         */
 
        allure.step("Get video file");
 
        expect(this.video,
            "Video recorder isn't initialized").to.exist;
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        LOG.info("Getting recorded video path...");
 
        if (check) {
            expect(this.video.isRunning,
                "Can't get recorded video file path, " +
                   "because video is still recording").to.be.false;
            expect(this.video.filePath,
                "Can't get recorded video file path, " +
                   "because it's empty").to.not.be.empty;
        };
 
        LOG.info("Recorded video path is got");
        allure.pass();
 
        return this.video.filePath;
    },
 
    removeVideo: function (opts) {
        /**
         * Step to remove recorded video.
         *
         * @memberOf VideoSteps
         * @method removeVideo
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check step result.
         * @return {boolean} `true` if step was executed, `false` if was skipped.
         * @throws {AssertionError} If video file wasn't removed.
         */
 
        if (!this.video.filePath || !this.__fs.existsSync(this.video.filePath)) {
            LOG.warn("Video file doesn't exist");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Remove recorded video");
        LOG.info("Removing recorded video file...");
 
        this.__fs.unlinkSync(this.video.filePath);
 
        if (check) {
            expect(this.__fs.existsSync(this.video.filePath),
                `Video file '${this.video.filePath}' wasn't removed`)
                .to.be.false;
        };
 
        LOG.info("Recorded video file is removed");
        allure.pass();
 
        return true;
    },
    /**
     * Helper to get video location.
     * 
     * @async
     * @method
     * @instance
     * @protected
     * @return {Promise<object>} Dict of location.
     */
    _videoLocation: async function () {
        var loc = {};
        var screen = await this.__resolution.get();
        var screenLoc = { x: 0, y: 0,
            width: screen.width,
            height: screen.height };
 
        if (this.webdriver && await this.webdriver.session()) {
 
            _.assign(loc,
                (await this.webdriver.windowHandlePosition()).value);
            _.assign(loc,
                (await this.webdriver.windowHandleSize()).value);
 
            expect(U.isInScreen(loc, screenLoc),
                "Browser is outside of screen").to.be.true;
 
            Iif (loc.x < 0) loc.x = 0;
            Iif (loc.y < 0) loc.y = 0;
            Iif (loc.x + loc.width > screen.width) {
                loc.width = screen.width - loc.x;
            };
            Iif (loc.y + loc.height > screen.height) {
                loc.height = screen.height - loc.y;
            };
 
        } else {
            loc = screenLoc;
        };
 
        expect(loc.width, "Invalid video width").to.be.above(0);
        expect(loc.height, "Invalid video height").to.be.above(0);
 
        return loc;
    },
    /**
     * Helper to make video path.
     *
     * @method
     * @instance
     * @protected
     * @arg {object} [opts] - Step options.
     * @arg {string} [opts.name] - File name. Extension `.avi` will be
     *  added automatically. Default name will be generated with `uuid`
     *  algorithm.
     * @arg {string} [opts.dir] - Folder to save video.
     * @return {string} Full video path.
     */
    _makeVideoPath: function (opts) {
        opts = U.defVal(opts, {});
 
        var fileName = U.toKebab(U.defVal(opts.name, uuid()));
        if (!fileName.endsWith(".avi")) fileName += ".avi";
 
        return U.mkpath(
            U.defVal(opts.dir,
                path.resolve(CONF.report.testDir || CONF.report.dir, "videos")),
            fileName);
    },
};
 
module.exports = VideoSteps;