83 lines
9.4 KiB
JavaScript
83 lines
9.4 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const path_1 = __importDefault(require("path"));
|
|
const mustache_1 = __importDefault(require("mustache"));
|
|
const os_family_1 = __importDefault(require("os-family"));
|
|
const exec_1 = require("../utils/exec");
|
|
const fs_exists_promised_1 = __importDefault(require("../utils/fs-exists-promised"));
|
|
const errors_1 = require("../errors");
|
|
const debug_1 = __importDefault(require("debug"));
|
|
const util_1 = require("util");
|
|
const LOGGER = debug_1.default('testcafe:browser-tools:open');
|
|
function log(data) {
|
|
try {
|
|
LOGGER(util_1.inspect(data, { isTestCafeInspect: true, compact: false }));
|
|
}
|
|
catch (e) {
|
|
LOGGER(e.stack ? e.stack : String(e));
|
|
}
|
|
}
|
|
async function checkBrowserPath(browserInfo) {
|
|
if (!browserInfo.path) {
|
|
//NOTE: Path may be undefined when winOpenCmdTemplate is specified (e.g. MS Edge)
|
|
if (browserInfo.winOpenCmdTemplate)
|
|
return;
|
|
throw new errors_1.BrowserPathNotSetError();
|
|
}
|
|
var fileExists = await fs_exists_promised_1.default(browserInfo.path);
|
|
if (!fileExists)
|
|
throw new errors_1.UnableToRunBrowsersError({ path: browserInfo.path });
|
|
}
|
|
function getWinOpenCommand(browserInfo, pageUrl) {
|
|
if (browserInfo.winOpenCmdTemplate) {
|
|
return mustache_1.default.render(browserInfo.winOpenCmdTemplate, {
|
|
pageUrl: pageUrl
|
|
});
|
|
}
|
|
var browserDirPath = path_1.default.dirname(browserInfo.path);
|
|
var browserExecFileName = path_1.default.basename(browserInfo.path);
|
|
return `start /D "${browserDirPath}" .\\${browserExecFileName} ${browserInfo.cmd} ${pageUrl}`;
|
|
}
|
|
function getMacOpenCommand(browserInfo, pageUrl) {
|
|
return mustache_1.default.render(browserInfo.macOpenCmdTemplate, {
|
|
path: browserInfo.path,
|
|
cmd: browserInfo.cmd,
|
|
pageUrl: pageUrl
|
|
});
|
|
}
|
|
function getLinuxOpenCommand(browserInfo, pageUrl) {
|
|
return `"${browserInfo.path}" ${browserInfo.cmd} "${pageUrl}" 0<&- >/dev/null 2>&1 &`;
|
|
}
|
|
var getOpenCommand = null;
|
|
if (os_family_1.default.win)
|
|
getOpenCommand = getWinOpenCommand;
|
|
else if (os_family_1.default.mac)
|
|
getOpenCommand = getMacOpenCommand;
|
|
else if (os_family_1.default.linux)
|
|
getOpenCommand = getLinuxOpenCommand;
|
|
/**
|
|
* Opens the web page in a new instance of the browser.
|
|
* @function
|
|
* @async
|
|
* @name open
|
|
* @param {BrowserInfo} browserInfo - Provides information on the browser where the web page should be opened.
|
|
* @param {string} pageUrl - Specifies the web page URL.
|
|
*/
|
|
async function default_1(browserInfo, pageUrl) {
|
|
await checkBrowserPath(browserInfo);
|
|
if (!getOpenCommand)
|
|
return;
|
|
var command = getOpenCommand(browserInfo, pageUrl);
|
|
try {
|
|
log(command);
|
|
await exec_1.exec(command);
|
|
}
|
|
catch (err) {
|
|
throw new errors_1.UnableToRunBrowsersError({ path: browserInfo.path });
|
|
}
|
|
}
|
|
exports.default = default_1;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3Blbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hcGkvb3Blbi5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLGdEQUF3QjtBQUN4Qix3REFBZ0M7QUFDaEMsMERBQTJCO0FBQzNCLHdDQUFxQztBQUNyQyxxRkFBaUQ7QUFDakQsc0NBQTZFO0FBQzdFLGtEQUEwQjtBQUMxQiwrQkFBK0I7QUFFL0IsTUFBTSxNQUFNLEdBQUcsZUFBSyxDQUFDLDZCQUE2QixDQUFDLENBQUM7QUFFcEQsU0FBUyxHQUFHLENBQUUsSUFBS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