/**
 *
 * Creates a new Selenium session with your current capabilities. This is useful if you
 * test highly stateful application where you need to clean the browser session between
 * the tests in your spec file to avoid creating hundrets of single test files with WDIO.
 * Be careful though. This command affects your test time tremendously since spawning
 * new Selenium session is very time consuming especially when using cloud services.
 *
 * <example>
    :refreshSync.js
    it('should refresh my session', function () {
        console.log(browser.sessionId); // outputs: e042b3f3cd5a479da4e171825e96e655
        browser.refresh();
        console.log(browser.sessionId); // outputs: 9a0d9bf9d4864160aa982c50cf18a573
    })
 * </example>
 *
 * @alias browser.refresh
 * @type utility
 *
 */

"use strict";

var _Promise = require("babel-runtime/core-js/promise")["default"];

Object.defineProperty(exports, "__esModule", {
    value: true
});
var refresh = function refresh() {
    var _this = this;

    var oldSessionId = this.requestHandler.sessionID;

    return this.end().init().then(function (res) {
        var newSessionId = _this.requestHandler.sessionID;

        if (!Array.isArray(_this.options.onRefresh)) {
            return _Promise.resolve();
        }

        return _Promise.all(_this.options.onRefresh.map(function (hook) {
            return hook(oldSessionId, newSessionId);
        }));
    })["catch"](function (e) {
        console.log("Error in onRefresh hook: \"" + e.stack + "\"");
    });
};

exports["default"] = refresh;
module.exports = exports["default"];
