From fcd327a039a069cb3236e425f710224d908a8de7 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 5 Jan 2025 02:52:01 +0000 Subject: [PATCH] Add safari browser, using AppleScript to start/stop --- .github/workflows/CI.yaml | 4 ++ src/browsers.js | 107 +++++++++++++++++++++++++++++++++++--- src/server.js | 8 +-- 3 files changed, 108 insertions(+), 11 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index bd9ac82..a5f478a 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -57,3 +57,7 @@ jobs: - name: Check system browsers run: node bin/qtap.js -v -b firefox -b chrome -b chromium -b edge test/pass.html + + - name: Check system browsers (Safari) + if: ${{ runner.os == 'macOS' }} + run: node bin/qtap.js -v -b safari test/pass.html diff --git a/src/browsers.js b/src/browsers.js index 64e9d36..4796b15 100644 --- a/src/browsers.js +++ b/src/browsers.js @@ -96,13 +96,16 @@ const LocalBrowser = { /** * Create a new temporary directory and return its name. * - * The newly created directory will automatically will cleaned up. + * This defaults to creating subdirectories inside Node.js `os.tmpdir`, which honors + * any TMPDIR, TMP, or TEMP environment variable. + * + * The newly created directory is automatically cleaned up at the end of the process. * * @returns {string} */ - makeTempDir () { + makeTempDir (parentDir = os.tmpdir()) { // Use mkdtemp (instead of only tmpdir) to avoid clash with past or concurrent qtap procesess. - const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'qtap_')); + const dir = fs.mkdtempSync(path.join(parentDir, 'qtap_')); tempDirs.push(dir); return dir; }, @@ -279,12 +282,104 @@ async function chromium (paths, url, signal, logger) { '--disable-gpu', '--disable-dev-shm-usage' ]), - ...(process.env.CHROMIUM_FLAGS ? process.env.CHROMIUM_FLAGS.split(/\s+/) : []), + ...(process.env.CHROMIUM_FLAGS ? process.env.CHROMIUM_FLAGS.split(/\s+/) : ( + process.env.CI ? ['--no-sandbox'] : []) + ), url ]; await LocalBrowser.spawn(paths, args, signal, logger); } +/** + * Known approaches: + * + * - `Safari `. This does not allow URLs. + * + * - `Safari redirect.html`, without other arguments. This worked from 2012-2018 as used by Karma. + * Safari allows only local files to be passed, no URLs. Karma created a temporary HTML file + * with ``, and opened that instead. + * https://github.com/karma-runner/karma-safari-launcher/blob/v1.0.0/index.js + * https://github.com/karma-runner/karma/blob/v0.3.5/lib/launcher.js#L213 + * https://github.com/karma-runner/karma/commit/5513fd66ae + * + * This is no longer viable after macOS 10.14 Mojave, because macOS SIP prompts the user + * due to our temporary file being outside `~/Library/Containers/com.apple.Safari`. + * https://github.com/karma-runner/karma-safari-launcher/issues/29 + * + * - `Safari container/redirect.html`. macOS SIP denies this by default for the same reason. + * It is used by by some packages, with the caveat that you grant an exemption to Terminal + * to write to Safari's container and/or grant it Full Disk Access. + * https://github.com/marcoscaceres/karma-safaritechpreview-launcher/issues/7 + * + * - `open -F -W -n -b com.apple.Safari `. This starts correctly but exposes + * no PID to cleanly end the process. + * https://github.com/karma-runner/karma-safari-launcher/issues/29#issuecomment-501101074 + * + * - + * + * - `osascript -e