Path: blob/trunk/javascript/selenium-webdriver/common/seleniumManager.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* This implementation is still in beta, and may change.19*20* Wrapper for getting information from the Selenium Manager binaries21*/2223const { platform } = require('node:process')24const path = require('node:path')25const fs = require('node:fs')26const spawnSync = require('node:child_process').spawnSync27const logging = require('../lib/logging')2829const log_ = logging.getLogger(logging.Type.DRIVER)30let debugMessagePrinted = false3132/**33* Determines the path of the correct Selenium Manager binary34* @returns {string}35*/36function getBinary() {37const directory = {38darwin: 'macos',39win32: 'windows',40cygwin: 'windows',41linux: 'linux',42}[platform]4344const file = directory === 'windows' ? 'selenium-manager.exe' : 'selenium-manager'4546let seleniumManagerBasePath = path.join(__dirname, '..', '/bin')4748const filePath = process.env.SE_MANAGER_PATH || path.join(seleniumManagerBasePath, directory, file)4950if (!fs.existsSync(filePath)) {51throw new Error(`Unable to obtain Selenium Manager at ${filePath}`)52}5354if (!debugMessagePrinted) {55log_.debug(`Selenium Manager binary found at ${filePath}`)56debugMessagePrinted = true // Set the flag to true after printing the debug message57}5859return filePath60}6162/**63* Determines the path of the correct driver64* @param {string[]} args arguments to invoke Selenium Manager65* @returns {{browserPath: string, driverPath: string}} path of the driver and66* browser location67*/6869function binaryPaths(args) {70const smBinary = getBinary()71const spawnResult = spawnSync(smBinary, args)72let output73if (spawnResult.status) {74let errorMessage75if (spawnResult.stderr.toString()) {76errorMessage = spawnResult.stderr.toString()77}78if (spawnResult.stdout.toString()) {79try {80output = JSON.parse(spawnResult.stdout.toString())81logOutput(output)82errorMessage = output.result.message83} catch (e) {84errorMessage = e.toString()85}86}87throw new Error(`Error executing command for ${smBinary} with ${args}: ${errorMessage}`)88}89try {90output = JSON.parse(spawnResult.stdout.toString())91} catch (e) {92throw new Error(`Error executing command for ${smBinary} with ${args}: ${e.toString()}`)93}9495logOutput(output)96return {97driverPath: output.result.driver_path,98browserPath: output.result.browser_path,99}100}101102function logOutput(output) {103for (const key in output.logs) {104if (output.logs[key].level === 'WARN') {105log_.warning(`${output.logs[key].message}`)106}107if (['DEBUG', 'INFO'].includes(output.logs[key].level)) {108log_.debug(`${output.logs[key].message}`)109}110}111}112113// PUBLIC API114module.exports = { binaryPaths }115116117