UNPKG

vscode-extension-tester

Version:

ExTester is a package that is designed to help you run UI tests for your Visual Studio Code extensions using selenium-webdriver.

229 lines 9.19 kB
"use strict"; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License", destination); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DriverUtil = void 0; const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const childProcess = __importStar(require("child_process")); const unpack_1 = require("./unpack"); const download_1 = require("./download"); const extester_1 = require("../extester"); /** * Handles version checks and download of ChromeDriver */ class DriverUtil { downloadFolder; /** * Create an instance of chrome driver handler * @param folder path to a folder to store all artifacts */ constructor(folder = extester_1.DEFAULT_STORAGE_FOLDER) { this.downloadFolder = path.resolve(folder); } /** * Find a matching ChromeDriver version for a given Chromium version and download it. * @param chromiumVersion version of Chromium to match the ChromeDriver against */ async downloadChromeDriverForChromiumVersion(chromiumVersion, noCache = false) { const version = await this.getChromeDriverVersion(chromiumVersion); return await this.downloadChromeDriver(version, noCache); } /** * Download a given version ChromeDriver * @param version version to download */ async downloadChromeDriver(version, noCache = false) { const url = this.getChromeDriverURL(version); const driverBinary = this.getChromeDriverBinaryPath(version); if (!noCache && fs.existsSync(driverBinary)) { let localVersion = ''; try { localVersion = await this.getLocalDriverVersion(version); } catch (err) { // ignore and download } if (localVersion.startsWith(version)) { console.log(`ChromeDriver ${version} exists in local cache, skipping download`); return ''; } } fs.mkdirpSync(this.downloadFolder); const fileName = path.join(this.downloadFolder, noCache ? `${path.basename(url)}` : `${version}-${path.basename(url)}`); if (!noCache && fs.existsSync(fileName)) { console.log(`ChromeDriver ${version} exists in storage folder, skipping download`); } else { console.log(`Downloading ChromeDriver ${version} from: ${url}`); await download_1.Download.getFile(url, fileName, true); } console.log(`Unpacking ChromeDriver ${version} into ${this.downloadFolder}`); await unpack_1.Unpack.unpack(fileName, this.downloadFolder); if (process.platform !== 'win32') { fs.chmodSync(driverBinary, 0o755); } if (noCache) { await fs.remove(fileName); console.log('Removed downloaded archive as --no_cache is active'); } console.log('Success!'); return driverBinary; } getChromeDriverBinaryPath(version) { const majorVersion = this.getMajorVersion(version); const binary = process.platform === 'win32' ? 'chromedriver.exe' : 'chromedriver'; let driverBinaryPath = path.join(this.downloadFolder, binary); if (+majorVersion > 114) { driverBinaryPath = path.join(this.downloadFolder, `chromedriver-${DriverUtil.getChromeDriverPlatform()}`, binary); } return driverBinaryPath; } static getChromeDriverPlatform() { switch (process.platform) { case 'darwin': return `mac-${process.arch}`; case 'win32': return process.arch === 'x64' ? 'win64' : 'win32'; case 'linux': return 'linux64'; default: break; } return undefined; } static getChromeDriverPlatformOLD() { switch (process.platform) { case 'darwin': return process.arch === 'arm64' ? 'mac_arm64' : 'mac64'; case 'win32': return 'win32'; case 'linux': return 'linux64'; default: break; } return undefined; } getChromeDriverURL(version) { const majorVersion = this.getMajorVersion(version); let driverPlatform = DriverUtil.getChromeDriverPlatformOLD(); let url = `https://chromedriver.storage.googleapis.com/${version}/chromedriver_${driverPlatform}.zip`; if (+majorVersion > 114) { driverPlatform = DriverUtil.getChromeDriverPlatform(); url = `https://storage.googleapis.com/chrome-for-testing-public/${version}/${driverPlatform}/chromedriver-${driverPlatform}.zip`; } return url; } async checkDriverVersionOffline(version) { try { return await this.getLocalDriverVersion(version); } catch (err) { console.log('ERROR: Cannot find a copy of ChromeDriver in local cache in offline mode, exiting.'); throw err; } } /** * Check local chrome driver version */ async getLocalDriverVersion(version) { const command = `${this.getChromeDriverBinaryPath(version)} -v`; return new Promise((resolve, reject) => { childProcess.exec(command, (err, stdout) => { if (err) { return reject(new Error(err.message)); } resolve(stdout.split(' ')[1]); }); }); } /** * Find a matching version of ChromeDriver for a given Chromium version * @param chromiumVersion Chromium version to check against */ async getChromeDriverVersion(chromiumVersion) { const majorVersion = this.getMajorVersion(chromiumVersion); // chrome driver versioning has changed for chrome 70+ if (+majorVersion < 70) { if (this.chromiumVersionMap[+majorVersion]) { return this.chromiumVersionMap[+majorVersion]; } else { throw new Error(`Chromium version ${chromiumVersion} not supported`); } } let url = `https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${majorVersion}`; if (+majorVersion > 114) { url = `https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_${majorVersion}`; } const fileName = 'driverVersion'; await download_1.Download.getFile(url, path.join(this.downloadFolder, fileName)); return fs.readFileSync(path.join(this.downloadFolder, fileName)).toString(); } getMajorVersion(version) { return version.split('.')[0]; } // older chromedriver versions do not match chrome versions chromiumVersionMap = { 69: '2.38', 68: '2.38', 67: '2.38', 66: '2.38', 65: '2.37', 64: '2.36', 63: '2.35', 62: '2.34', 61: '2.33', 60: '2.32', }; } exports.DriverUtil = DriverUtil; //# sourceMappingURL=driverUtil.js.map