Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: fix installing of branded browsers under non-root (#10640)

Max Schmitt56109743128c
diff --git a/packages/playwright-core/bin/reinstall_chrome_beta_linux.sh b/packages/playwright-core/bin/reinstall_chrome_beta_linux.sh
index ccdad55339..63e9253dc0 100755
--- a/packages/playwright-core/bin/reinstall_chrome_beta_linux.sh
+++ b/packages/playwright-core/bin/reinstall_chrome_beta_linux.sh
@@ -7,26 +7,23 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
-is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
-if is_user_root; then
- maybesudo=""
-else
- maybesudo="sudo"
-fi
-
# 1. make sure to remove old beta if any.
if dpkg --get-selections | grep -q "^google-chrome-beta[[:space:]]*install$" >/dev/null; then
- $maybesudo apt-get remove -y google-chrome-beta
+ apt-get remove -y google-chrome-beta
fi
+# 2. Update apt lists (needed to install curl and chrome dependencies)
+apt-get update
+
+# 3. Install curl to download chrome
if ! command -v curl >/dev/null; then
- $maybesudo apt-get install -y curl
+ apt-get install -y curl
fi
-# 2. download chrome beta from dl.google.com and install it.
+# 4. download chrome beta from dl.google.com and install it.
cd /tmp
curl -O https://dl.google.com/linux/direct/google-chrome-beta_current_amd64.deb
-$maybesudo apt-get install -y ./google-chrome-beta_current_amd64.deb
+apt-get install -y ./google-chrome-beta_current_amd64.deb
rm -rf ./google-chrome-beta_current_amd64.deb
cd -
google-chrome-beta --version
diff --git a/packages/playwright-core/bin/reinstall_chrome_stable_linux.sh b/packages/playwright-core/bin/reinstall_chrome_stable_linux.sh
index eac37f3436..57269bd5fe 100755
--- a/packages/playwright-core/bin/reinstall_chrome_stable_linux.sh
+++ b/packages/playwright-core/bin/reinstall_chrome_stable_linux.sh
@@ -7,27 +7,23 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
-is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
-if is_user_root; then
- maybesudo=""
-else
- maybesudo="sudo"
-fi
-
-
# 1. make sure to remove old stable if any.
if dpkg --get-selections | grep -q "^google-chrome[[:space:]]*install$" >/dev/null; then
- $maybesudo apt-get remove -y google-chrome
+ apt-get remove -y google-chrome
fi
+# 2. Update apt lists (needed to install curl and chrome dependencies)
+apt-get update
+
+# 3. Install curl to download chrome
if ! command -v curl >/dev/null; then
- $maybesudo apt-get install -y curl
+ apt-get install -y curl
fi
-# 2. download chrome stable from dl.google.com and install it.
+# 4. download chrome stable from dl.google.com and install it.
cd /tmp
curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
-$maybesudo apt-get install -y ./google-chrome-stable_current_amd64.deb
+apt-get install -y ./google-chrome-stable_current_amd64.deb
rm -rf ./google-chrome-stable_current_amd64.deb
cd -
google-chrome --version
diff --git a/packages/playwright-core/bin/reinstall_msedge_beta_linux.sh b/packages/playwright-core/bin/reinstall_msedge_beta_linux.sh
index e41e96c350..ff19e52173 100755
--- a/packages/playwright-core/bin/reinstall_msedge_beta_linux.sh
+++ b/packages/playwright-core/bin/reinstall_msedge_beta_linux.sh
@@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
-is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
-if is_user_root; then
- maybesudo=""
-else
- maybesudo="sudo"
-fi
-
+# 1. make sure to remove old beta if any.
if dpkg --get-selections | grep -q "^microsoft-edge-beta[[:space:]]*install$" >/dev/null; then
- $maybesudo apt-get remove -y microsoft-edge-beta
+ apt-get remove -y microsoft-edge-beta
fi
+# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
- $maybesudo apt-get install -y curl
+ apt-get update
+ apt-get install -y curl
fi
+# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
-$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
-$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
+install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
+sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
rm /tmp/microsoft.gpg
-$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-beta
+apt-get update && apt-get install -y microsoft-edge-beta
microsoft-edge-beta --version
diff --git a/packages/playwright-core/bin/reinstall_msedge_dev_linux.sh b/packages/playwright-core/bin/reinstall_msedge_dev_linux.sh
index d201154884..9c3447fe69 100755
--- a/packages/playwright-core/bin/reinstall_msedge_dev_linux.sh
+++ b/packages/playwright-core/bin/reinstall_msedge_dev_linux.sh
@@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
-is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
-if is_user_root; then
- maybesudo=""
-else
- maybesudo="sudo"
-fi
-
+# 1. make sure to remove old dev if any.
if dpkg --get-selections | grep -q "^microsoft-edge-dev[[:space:]]*install$" >/dev/null; then
- $maybesudo apt-get remove -y microsoft-edge-dev
+ apt-get remove -y microsoft-edge-dev
fi
+# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
- $maybesudo apt-get install -y curl
+ apt-get update
+ apt-get install -y curl
fi
+# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
-$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
-$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
+install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
+sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
rm /tmp/microsoft.gpg
-$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-dev
+apt-get update && apt-get install -y microsoft-edge-dev
microsoft-edge-dev --version
diff --git a/packages/playwright-core/bin/reinstall_msedge_stable_linux.sh b/packages/playwright-core/bin/reinstall_msedge_stable_linux.sh
index 19628cac8b..19dc354b26 100755
--- a/packages/playwright-core/bin/reinstall_msedge_stable_linux.sh
+++ b/packages/playwright-core/bin/reinstall_msedge_stable_linux.sh
@@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
-is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
-if is_user_root; then
- maybesudo=""
-else
- maybesudo="sudo"
-fi
-
+# 1. make sure to remove old stable if any.
if dpkg --get-selections | grep -q "^microsoft-edge-stable[[:space:]]*install$" >/dev/null; then
- $maybesudo apt-get remove -y microsoft-edge-stable
+ apt-get remove -y microsoft-edge-stable
fi
+# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
- $maybesudo apt-get install -y curl
+ apt-get update
+ apt-get install -y curl
fi
+# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
-$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
-$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-stable.list'
+install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
+sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-stable.list'
rm /tmp/microsoft.gpg
-$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-stable
+apt-get update && apt-get install -y microsoft-edge-stable
microsoft-edge-stable --version
diff --git a/packages/playwright-core/src/utils/dependencies.ts b/packages/playwright-core/src/utils/dependencies.ts
index 0b69b36e31..7f18ea3aac 100644
--- a/packages/playwright-core/src/utils/dependencies.ts
+++ b/packages/playwright-core/src/utils/dependencies.ts
@@ -70,11 +70,13 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>, dr
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
...uniqueLibraries,
].join(' '));
- const [command, args] = await buildAptProcessArgs(commands);
+ const { command, args, elevatedPermissions } = await utils.transformCommandsForRoot(commands);
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
+ if (elevatedPermissions)
+ console.log('Switching to root user to install dependencies...'); // eslint-disable-line no-console
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
await new Promise((resolve, reject) => {
child.on('exit', resolve);
@@ -82,16 +84,6 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>, dr
});
}
-async function buildAptProcessArgs(commands: string[]): Promise<[string, string[]]> {
- const isRoot = process.getuid() === 0;
- if (isRoot)
- return ['sh', ['-c', `${commands.join('&& ')}`]];
- const sudoExists = await utils.spawnAsync('which', ['sudo']);
- if (sudoExists.code === 0)
- return ['sudo', ['--', 'sh', '-c', `${commands.join('&& ')}`]];
- return ['su', ['root', '-c', `${commands.join('&& ')}`]];
-}
-
export async function validateDependenciesWindows(windowsExeAndDllDirectories: string[]) {
const directoryPaths = windowsExeAndDllDirectories;
const lddPaths: string[] = [];
diff --git a/packages/playwright-core/src/utils/registry.ts b/packages/playwright-core/src/utils/registry.ts
index 777e564d10..fa5964b304 100644
--- a/packages/playwright-core/src/utils/registry.ts
+++ b/packages/playwright-core/src/utils/registry.ts
@@ -21,7 +21,7 @@ import * as util from 'util';
import * as fs from 'fs';
import lockfile from 'proper-lockfile';
import { getUbuntuVersion } from './ubuntuVersion';
-import { getFromENV, getAsBooleanFromENV, calculateSha1, removeFolders, existsAsync, hostPlatform, canAccessFile, spawnAsync, fetchData, wrapInASCIIBox } from './utils';
+import { getFromENV, getAsBooleanFromENV, calculateSha1, removeFolders, existsAsync, hostPlatform, canAccessFile, spawnAsync, fetchData, wrapInASCIIBox, transformCommandsForRoot } from './utils';
import { DependencyGroup, installDependenciesLinux, installDependenciesWindows, validateDependenciesLinux, validateDependenciesWindows } from './dependencies';
import { downloadBrowserWithProgressBar, logPolitely } from './browserFetcher';
@@ -645,16 +645,25 @@ export class Registry {
const scriptName = scripts[process.platform as 'linux' | 'darwin' | 'win32'];
if (!scriptName)
throw new Error(`Cannot install ${channel} on ${process.platform}`);
+ const cwd = BIN_PATH;
const isPowerShell = scriptName.endsWith('.ps1');
- const shell = isPowerShell ? 'powershell.exe' : 'bash';
- const args = [
- ...(isPowerShell ? ['-ExecutionPolicy', 'Bypass', '-File'] : []),
- path.join(BIN_PATH, scriptName),
- ...scriptArgs
- ];
- const { code } = await spawnAsync(shell, args, { cwd: BIN_PATH, stdio: 'inherit' });
- if (code !== 0)
- throw new Error(`Failed to install ${channel}`);
+ if (isPowerShell) {
+ const args = [
+ '-ExecutionPolicy', 'Bypass', '-File',
+ path.join(BIN_PATH, scriptName),
+ ...scriptArgs
+ ];
+ const { code } = await spawnAsync('powershell.exe', args, { cwd, stdio: 'inherit' });
+ if (code !== 0)
+ throw new Error(`Failed to install ${channel}`);
+ } else {
+ const { command, args, elevatedPermissions } = await transformCommandsForRoot([`bash ${path.join(BIN_PATH, scriptName)} ${scriptArgs.join('')}`]);
+ if (elevatedPermissions)
+ console.log('Switching to root user to install dependencies...'); // eslint-disable-line no-console
+ const { code } = await spawnAsync(command, args, { cwd, stdio: 'inherit' });
+ if (code !== 0)
+ throw new Error(`Failed to install ${channel}`);
+ }
}
private async _validateInstallationCache(linksDir: string) {
diff --git a/packages/playwright-core/src/utils/utils.ts b/packages/playwright-core/src/utils/utils.ts
index 2e36790f4b..2fd7339ea1 100644
--- a/packages/playwright-core/src/utils/utils.ts
+++ b/packages/playwright-core/src/utils/utils.ts
@@ -520,3 +520,13 @@ export function streamToString(stream: stream.Readable): Promise<string> {
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}
+
+export async function transformCommandsForRoot(commands: string[]): Promise<{ command: string, args: string[], elevatedPermissions: boolean}> {
+ const isRoot = process.getuid() === 0;
+ if (isRoot)
+ return { command: 'sh', args: ['-c', `${commands.join('&& ')}`], elevatedPermissions: false };
+ const sudoExists = await spawnAsync('which', ['sudo']);
+ if (sudoExists.code === 0)
+ return { command: 'sudo', args: ['--', 'sh', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
+ return { command: 'su', args: ['root', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
+}