|
| 1 | +/* |
| 2 | + * Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The SFC licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Wrapper for getting information from the Selenium Manager binaries |
| 22 | + */ |
| 23 | + |
| 24 | +const { platform } = require('process') |
| 25 | +const path = require('path') |
| 26 | +const fs = require('fs') |
| 27 | +const execSync = require('child_process').execSync |
| 28 | + |
| 29 | +/** |
| 30 | + * currently supported browsers for selenium-manager |
| 31 | + * @type {string[]} |
| 32 | + */ |
| 33 | +const Browser = ['chrome', 'firefox', 'edge'] |
| 34 | + |
| 35 | +/** |
| 36 | + * Determines the path of the correct Selenium Manager binary |
| 37 | + * @returns {string} |
| 38 | + */ |
| 39 | +function getBinary() { |
| 40 | + const directory = { |
| 41 | + darwin: 'macos', |
| 42 | + win32: 'windows', |
| 43 | + cygwin: 'windows', |
| 44 | + linux: 'linux', |
| 45 | + }[platform] |
| 46 | + |
| 47 | + const file = |
| 48 | + directory === 'windows' ? 'selenium-manager.exe' : 'selenium-manager' |
| 49 | + |
| 50 | + const filePath = path.join(__dirname, '..', '/bin', directory, file) |
| 51 | + |
| 52 | + if (!fs.existsSync(filePath)) { |
| 53 | + throw new Error(`Unable to obtain Selenium Manager`) |
| 54 | + } |
| 55 | + |
| 56 | + return filePath |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Determines the path of the correct driver |
| 61 | + * @param {Browser|string} browser name to fetch the driver |
| 62 | + * @returns {string} path of the driver location |
| 63 | + */ |
| 64 | + |
| 65 | +function driverLocation(browser) { |
| 66 | + if (!Browser.includes(browser.toLocaleString())) { |
| 67 | + throw new Error( |
| 68 | + `Unable to locate driver associated with browser name: ${browser}` |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + let args = [getBinary(), '--browser', browser] |
| 73 | + let result |
| 74 | + |
| 75 | + try { |
| 76 | + result = execSync(args.join(' ')).toString() |
| 77 | + } catch (e) { |
| 78 | + throw new Error( |
| 79 | + `Error executing command with ${args} : Error ${e.stderr.toString()}` |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + if (!result.startsWith('INFO\t')) { |
| 84 | + throw new Error(`Unsuccessful command executed ${args}}`) |
| 85 | + } |
| 86 | + |
| 87 | + return result.replace('INFO\t', '').trim() |
| 88 | +} |
| 89 | + |
| 90 | +// PUBLIC API |
| 91 | +module.exports = { driverLocation } |
0 commit comments