@eyeo/get-browser-binary 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.dockerignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules/
2
+ browser-snapshots/
3
+ docs/
package/.gitlab-ci.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  default:
2
- image: registry.gitlab.com/eyeo/docker/adblockplus-ci:node16-no-python
2
+ image: node:16-bullseye-slim
3
3
  interruptible: true
4
4
 
5
5
  stages:
@@ -29,18 +29,15 @@ test:basic:
29
29
 
30
30
  test:browsers:linux:
31
31
  stage: test
32
+ image: docker:20.10.16
33
+ services:
34
+ - docker:20.10.16-dind
35
+ variables:
36
+ DOCKER_DRIVER: overlay2
32
37
  before_script:
33
- - apt-get update && apt-get install -y procps
34
- - npm install
35
- # https://www.how2shout.com/linux/install-microsoft-edge-on-linux/
36
- - wget https://packages.microsoft.com/keys/microsoft.asc
37
- - cat microsoft.asc | gpg --dearmor > microsoft.gpg
38
- - install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
39
- - echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list
40
- - rm microsoft.*
41
- - apt-get update && apt-get install -y microsoft-edge-stable
38
+ - docker build -f test/docker/Dockerfile -t browsers .
42
39
  script:
43
- - xvfb-run -a npm test -- --grep "Browser"
40
+ - docker run browsers
44
41
 
45
42
  test:browsers:windows:
46
43
  stage: test
@@ -54,10 +51,19 @@ test:browsers:windows:
54
51
  - Start-Process msiexec
55
52
  -ArgumentList "/i MicrosoftEdgeEnterpriseX64.msi /norestart /qn" -Wait
56
53
  - choco upgrade -y nodejs --version 16.10.0
54
+ - npm install -g npm
57
55
  - npm install
58
56
  script:
59
- - npm test -- --grep "chromium"
60
- - npm test -- --grep "edge"
57
+ # Running only a subset of Firefox and Opera tests to avoid low OS resources error
58
+ # https://gitlab.com/eyeo/developer-experience/get-browser-binary/-/issues/2
59
+ # Regarding grep, running npm v8 on powershell has issues when the grep
60
+ # value contains the pipe (|) literal. Storing that string as a verbatim
61
+ # string (single quotes) and then sorrounding it with four double quotes
62
+ # does the trick.
63
+ - $subset_tests = '(firefox|opera).*latest.*downloads'
64
+ - npm test -- --grep """"$subset_tests""""
65
+ - $full_tests = '(chromium|edge.*latest)'
66
+ - npm test -- --grep """"$full_tests""""
61
67
  tags:
62
68
  - shared-windows
63
69
  - windows
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ code-of-conduct@eyeo.com.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available
126
+ at [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
package/README.md CHANGED
@@ -1,11 +1,12 @@
1
1
  # get-browser-binary
2
2
 
3
- Download specific browser versions for Chromium, Firefox and Edge, and their
4
- matching [selenium webdriver](https://www.selenium.dev/selenium/docs/api/javascript/index.html).
3
+ Download specific browser versions for Chromium, Firefox, Edge and Opera, and
4
+ their matching [selenium webdriver](https://www.selenium.dev/selenium/docs/api/javascript/index.html).
5
5
 
6
- Note: Edge download is not implemented, and it is assumed to be already
7
- installed on the running system. In that case, the matching `msedgedriver`
8
- will be downloaded accordingly.
6
+ Note: Edge download is only supported on Linux. On other platforms it is assumed
7
+ to be already installed.
8
+
9
+ Note: Using the `operadriver` on Windows is not supported.
9
10
 
10
11
  ## Getting started
11
12
 
@@ -69,11 +70,34 @@ npm test
69
70
  The `grep` option filters the tests to run with a regular expression. Example:
70
71
 
71
72
  ```shell
72
- npm test -- --grep "chromium latest"
73
+ npm test -- --grep "chromium.*latest"
73
74
  ```
74
75
 
76
+ ### Running tests on Docker
77
+
78
+ Useful to reproduce the CI environment of the `test:browsers:linux` job:
79
+
80
+ ```shell
81
+ docker build -f test/docker/Dockerfile -t browsers .
82
+ docker run -it browsers
83
+ ```
84
+
85
+ The `grep` option can also be used on Docker via the `TEST_ARGS` parameter:
86
+
87
+ ```shell
88
+ docker run -e TEST_ARGS="--grep chromium.*latest" -it browsers
89
+ ```
90
+
91
+ Note: For a full automated run, `opera` tests should not use the interactive
92
+ tty `-it` flag.
93
+
75
94
  ## Building the documentation
76
95
 
77
96
  ```shell
78
97
  npm run docs
79
98
  ```
99
+
100
+ ## Code of Conduct
101
+
102
+ All contributors to this project are required to read and follow our
103
+ [code of conduct](./CODE_OF_CONDUCT.md).
package/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.4.0
2
+
3
+ - Adds Opera as a supported browser (#7)
4
+ - Enables Edge binary downloads on Linux (!18)
5
+ - Adds a code of conduct (#15)
6
+
1
7
  # 0.3.0
2
8
 
3
9
  - Adds incognito mode support to all browsers (#5)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eyeo/get-browser-binary",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Download browser binaries and matching webdrivers",
5
5
  "repository": {
6
6
  "type": "git",
package/src/browsers.js CHANGED
@@ -16,9 +16,10 @@
16
16
  */
17
17
 
18
18
  import path from "path";
19
- import {exec, execFile} from "child_process";
19
+ import {exec, execFile, spawn} from "child_process";
20
20
  import {promisify} from "util";
21
21
  import fs from "fs";
22
+ import fsExtra from "fs-extra";
22
23
 
23
24
  import got from "got";
24
25
  import webdriver from "selenium-webdriver";
@@ -27,8 +28,8 @@ import firefox from "selenium-webdriver/firefox.js";
27
28
  import command from "selenium-webdriver/lib/command.js";
28
29
  import extractZip from "extract-zip";
29
30
 
30
- import {download, extractTar, extractDmg, runWinInstaller, getBrowserVersion,
31
- killDriverProcess} from "./utils.js";
31
+ import {download, extractTar, extractDmg, getBrowserVersion, killDriverProcess,
32
+ wait} from "./utils.js";
32
33
 
33
34
  /**
34
35
  * Root folder where browser and webdriver binaries get downloaded.
@@ -49,7 +50,9 @@ let platform = `${process.platform}-${process.arch}`;
49
50
  class Browser {
50
51
  /**
51
52
  * @typedef {Object} BrowserBinary
52
- * @property {string} binary - The path to the Chromium binary.
53
+ * @property {string} binary - The path to the browser binary.
54
+ * @property {string} versionNumber - The version number of the browser
55
+ * binary.
53
56
  */
54
57
 
55
58
  /**
@@ -62,6 +65,25 @@ class Browser {
62
65
  throw new Error(ERROR_DOWNLOAD_NOT_SUPPORTED);
63
66
  }
64
67
 
68
+ /**
69
+ * Gets the installed version returned by the browser binary.
70
+ * @param {string} binary - The path to the browser binary.
71
+ * @return {string} Installed browser version.
72
+ */
73
+ static async getInstalledVersion(binary) {
74
+ let stdout;
75
+ if (process.platform == "win32") {
76
+ ({stdout} = await promisify(exec)(
77
+ `(Get-ItemProperty ${binary}).VersionInfo.ProductVersion`,
78
+ {shell: "powershell.exe"})
79
+ );
80
+ }
81
+ else {
82
+ ({stdout} = await promisify(execFile)(binary, ["--version"]));
83
+ }
84
+ return stdout;
85
+ }
86
+
65
87
  /**
66
88
  * @typedef {Object} webdriver
67
89
  * @see https://www.selenium.dev/selenium/docs/api/javascript/index.html
@@ -225,7 +247,7 @@ class Chromium extends Browser {
225
247
  ({version, base} = await Chromium.#getLatestVersion(version));
226
248
 
227
249
  let {binary, revision} = await Chromium.#downloadChromium(base);
228
- return {binary, downloadedVersion: version, revision};
250
+ return {binary, versionNumber: version, revision};
229
251
  }
230
252
 
231
253
  static async #installDriver(revision, version) {
@@ -245,7 +267,6 @@ class Chromium extends Browser {
245
267
  await download(`https://commondatastorage.googleapis.com/chromium-browser-snapshots/${dir}/${revision}/${zip}`,
246
268
  archive);
247
269
  await extractZip(archive, {dir: cacheDir});
248
- // avoid driver copy failing if the file would be locked
249
270
  await killDriverProcess(Chromium.#DRIVER);
250
271
  await fs.promises.mkdir(destinationDir, {recursive: true});
251
272
  await fs.promises.copyFile(path.join(cacheDir, zip.split(".")[0], driver),
@@ -256,9 +277,9 @@ class Chromium extends Browser {
256
277
  static async getDriver(version, {headless = true, extensionPaths = [],
257
278
  incognito = false, insecure = false,
258
279
  extraArgs = []} = {}) {
259
- let {binary, revision, downloadedVersion} =
280
+ let {binary, revision, versionNumber} =
260
281
  await Chromium.downloadBinary(version);
261
- await Chromium.#installDriver(revision, downloadedVersion);
282
+ await Chromium.#installDriver(revision, versionNumber);
262
283
 
263
284
  let options = new chrome.Options().addArguments("no-sandbox", ...extraArgs);
264
285
  if (extensionPaths.length > 0)
@@ -281,7 +302,8 @@ class Chromium extends Browser {
281
302
  /** @see Browser.enableExtensionInIncognito */
282
303
  static async enableExtensionInIncognito(driver, extensionTitle) {
283
304
  let version = await getBrowserVersion(driver);
284
- if (version < 75)
305
+ // Webdriver capabilities don't include a browser version for Opera
306
+ if (version && version < 75)
285
307
  // The UI workaround needs a chromedriver >= 75
286
308
  throw new Error(`Only supported on Chromium >= 75. Current version: ${version}`);
287
309
 
@@ -319,7 +341,7 @@ class Chromium extends Browser {
319
341
  * @hideconstructor
320
342
  * @extends Browser
321
343
  */
322
- class Firefox {
344
+ class Firefox extends Browser {
323
345
  static async #getLatestVersion(branch) {
324
346
  let data = await got("https://product-details.mozilla.org/1.0/firefox_versions.json").json();
325
347
  return branch == "beta" ?
@@ -342,7 +364,9 @@ class Firefox {
342
364
  static #extractFirefoxArchive(archive, dir) {
343
365
  switch (process.platform) {
344
366
  case "win32":
345
- return runWinInstaller(archive, dir);
367
+ // Procedure inspired from mozinstall:
368
+ // https://hg.mozilla.org/mozilla-central/file/tip/testing/mozbase/mozinstall/mozinstall/mozinstall.py
369
+ return promisify(exec)(`"${archive}" /extractdir=${dir}`);
346
370
  case "linux":
347
371
  return extractTar(archive, dir);
348
372
  case "darwin":
@@ -395,7 +419,7 @@ class Firefox {
395
419
  version = await Firefox.#getLatestVersion(version);
396
420
 
397
421
  let binary = await Firefox.#downloadFirefox(version);
398
- return {binary};
422
+ return {binary, versionNumber: version};
399
423
  }
400
424
 
401
425
  /** @see Browser.getDriver */
@@ -415,10 +439,23 @@ class Firefox {
415
439
  options.addArguments(...extraArgs);
416
440
  options.setBinary(binary);
417
441
 
418
- let driver = await new webdriver.Builder()
419
- .forBrowser("firefox")
420
- .setFirefoxOptions(options)
421
- .build();
442
+ let driver;
443
+ // The OS may be low on resources, that's why building the driver is retried
444
+ // https://github.com/mozilla/geckodriver/issues/1560
445
+ await wait(async() => {
446
+ try {
447
+ driver = await new webdriver.Builder()
448
+ .forBrowser("firefox")
449
+ .setFirefoxOptions(options)
450
+ .build();
451
+ return true;
452
+ }
453
+ catch (err) {
454
+ if (err.message != "Failed to decode response from marionette")
455
+ throw err;
456
+ await killDriverProcess("geckodriver");
457
+ }
458
+ }, 30000, "geckodriver didn't start, likely due to low OS resources", 1000);
422
459
 
423
460
  for (let extensionPath of extensionPaths) {
424
461
  await driver.execute(
@@ -457,32 +494,87 @@ class Firefox {
457
494
  * @hideconstructor
458
495
  * @extends Browser
459
496
  */
460
- class Edge {
497
+ class Edge extends Browser {
461
498
  static #DRIVER = "msedgedriver";
462
499
 
463
- static async #installDriver() {
464
- let stdout;
465
- if (process.platform == "win32") {
466
- let cmd = "(Get-ItemProperty ${Env:ProgramFiles(x86)}\\Microsoft\\" +
467
- "Edge\\Application\\msedge.exe).VersionInfo.ProductVersion";
468
- ({stdout} = await promisify(exec)(cmd, {shell: "powershell.exe"}));
500
+ static async #getVersionForChannel(version) {
501
+ if (!["latest", "beta", "dev"].includes(version))
502
+ return {versionNumber: version, channel: "stable"};
503
+
504
+ let channel = version == "latest" ? "stable" : version;
505
+ let {body} = await got(`https://packages.microsoft.com/repos/edge/pool/main/m/microsoft-edge-${channel}`);
506
+ let regex = /href="microsoft-edge-(stable|beta|dev)_(.*?)-1_/gm;
507
+ let matches;
508
+ let versionNumbers = [];
509
+ while ((matches = regex.exec(body)) !== null)
510
+ versionNumbers.push(matches[2]);
511
+
512
+ let compareVersions = (v1, v2) =>
513
+ parseInt(v1.split(".")[0], 10) < parseInt(v2.split(".")[0], 10) ? 1 : -1;
514
+ let versionNumber = versionNumbers.sort(compareVersions)[0];
515
+
516
+ return {versionNumber, channel};
517
+ }
518
+
519
+ /**
520
+ * Downloads the browser binary file.
521
+ * @param {string} version - Either "latest", "beta", "dev" or a full version
522
+ * number (i.e. "95.0.1020.53"). Defaults to "latest". This is only
523
+ * available on Linux.
524
+ * @return {BrowserBinary}
525
+ */
526
+ static async downloadBinary(version = "latest") {
527
+ if (process.platform != "linux")
528
+ throw new Error("Edge download is only supported on Linux");
529
+
530
+ let {versionNumber, channel} = await Edge.#getVersionForChannel(version);
531
+
532
+ let snapshotsDir = path.join(snapshotsBaseDir, "edge");
533
+ let filename = `microsoft-edge-${channel}_${versionNumber}-1_amd64.deb`;
534
+ let archive = path.join(snapshotsDir, "cache", filename);
535
+ let binary = {
536
+ stable: "microsoft-edge",
537
+ beta: "microsoft-edge-beta",
538
+ dev: "microsoft-edge-dev"
539
+ }[channel];
540
+
541
+ try {
542
+ if (await Edge.#getInstalledVersionNumber(binary) == versionNumber)
543
+ return {binary, versionNumber};
469
544
  }
470
- else if (process.platform == "darwin") {
471
- ({stdout} = await promisify(execFile)(
472
- "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
473
- ["--version"]
474
- ));
545
+ catch (e) {}
546
+
547
+ try {
548
+ await download(
549
+ `https://packages.microsoft.com/repos/edge/pool/main/m/microsoft-edge-${channel}/${filename}`,
550
+ archive
551
+ );
475
552
  }
476
- else if (process.platform == "linux") {
477
- ({stdout} = await promisify(execFile)("microsoft-edge", ["--version"]));
553
+ catch (err) {
554
+ throw new Error(`Edge download failed: ${err}`);
478
555
  }
556
+ await promisify(exec)(`dpkg -i ${archive}`);
557
+
558
+ return {binary, versionNumber};
559
+ }
479
560
 
480
- let version = stdout.trim().replace(/.*\s/, "");
561
+ static async #getInstalledVersionNumber(binary) {
562
+ let installedVersion = await Edge.getInstalledVersion(binary);
563
+ return installedVersion.replace("beta", "").replace("dev", "")
564
+ .trim().replace(/.*\s/, "");
565
+ }
566
+
567
+ static async #installDriver() {
568
+ let binary = {
569
+ win32:
570
+ "${Env:ProgramFiles(x86)}\\Microsoft\\Edge\\Application\\msedge.exe",
571
+ darwin: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
572
+ linux: "microsoft-edge"
573
+ }[process.platform];
574
+ let version = await Edge.#getInstalledVersionNumber(binary);
481
575
  if (!version)
482
576
  throw new Error("Edge is not installed");
483
577
 
484
- // Based on "node_modules/msedgedriver/install.js", adding a fallback
485
- // mechanism when msedgedriver doesn't exist for the latest Edge version.
486
578
  let [zip, driver] = {
487
579
  "win32-ia32": ["edgedriver_win32.zip", "msedgedriver.exe"],
488
580
  "win32-x64": ["edgedriver_win64.zip", "msedgedriver.exe"],
@@ -515,7 +607,6 @@ class Edge {
515
607
  throw new Error(`msedgedriver was not found for Edge ${version}`);
516
608
 
517
609
  await extractZip(archive, {dir: cacheDir});
518
- // avoid driver copy failing if the file would be locked
519
610
  await killDriverProcess(Edge.#DRIVER);
520
611
  for (let destinationDir of [driverBinDir, driverLibDir]) {
521
612
  await fs.promises.mkdir(destinationDir, {recursive: true});
@@ -528,6 +619,9 @@ class Edge {
528
619
  static async getDriver(version, {headless = true, extensionPaths = [],
529
620
  incognito = false, insecure = false,
530
621
  extraArgs = []} = {}) {
622
+ if (process.platform == "linux")
623
+ await Edge.downloadBinary(version);
624
+
531
625
  await Edge.#installDriver();
532
626
 
533
627
  let args = ["no-sandbox", ...extraArgs];
@@ -577,6 +671,217 @@ class Edge {
577
671
  }
578
672
  }
579
673
 
674
+ /**
675
+ * Download functionality for Opera. This class can be used statically.
676
+ * @hideconstructor
677
+ * @extends Browser
678
+ */
679
+ class Opera extends Browser {
680
+ static async #getVersionForChannel(version) {
681
+ let channelPath = "opera/desktop";
682
+ let filePrefix = "Opera";
683
+ if (version != "latest")
684
+ return {versionNumber: version, channelPath, filePrefix};
685
+
686
+ let {body} = await got(`https://ftp.opera.com/pub/${channelPath}`);
687
+ let regex = /href="(\d.*)\/"/gm;
688
+ let matches = body.match(regex);
689
+ let versionNumber = regex.exec(matches[matches.length - 1])[1];
690
+
691
+ return {versionNumber, channelPath, filePrefix};
692
+ }
693
+
694
+ static #getOperaBinary(dir) {
695
+ switch (process.platform) {
696
+ case "win32":
697
+ return path.join(dir, "launcher.exe");
698
+ case "linux":
699
+ return path.join("/", "usr", "bin", "opera");
700
+ case "darwin":
701
+ return path.join(dir, "Opera.app", "Contents", "MacOS", "Opera");
702
+ default:
703
+ throw new Error(`Unexpected platform: ${process.platform}`);
704
+ }
705
+ }
706
+
707
+ static async #extractDeb(archive) {
708
+ let child = spawn("dpkg", ["-i", archive]);
709
+
710
+ child.stdout.on("data", data => {
711
+ if (data.toString().startsWith("Do you want to update Opera")) {
712
+ process.stdin.pipe(child.stdin);
713
+ child.stdin.write("no\r\n");
714
+ }
715
+ });
716
+
717
+ child.stderr.on("data", data => {
718
+ let expectedWarnings = [
719
+ "debconf: unable to initialize frontend",
720
+ "dpkg: warning: downgrading opera-stable",
721
+ "update-alternatives",
722
+ "using /usr/bin/opera",
723
+ "skip creation of",
724
+ "\r\n"
725
+ ];
726
+ if (!expectedWarnings.find(err => data.toString().includes(err.trim())))
727
+ console.error(`stderr: ${data.toString()}`);
728
+ });
729
+
730
+ await new Promise((resolve, reject) => child.on("close", code => {
731
+ if (code != 0)
732
+ reject(`dpkg process exited with code ${code}`);
733
+
734
+ resolve();
735
+ }));
736
+ }
737
+
738
+ static async #installOnWindows(archive, dir, filename) {
739
+ let archiveCopy = path.join(dir, filename);
740
+ await fsExtra.copy(archive, archiveCopy);
741
+ await promisify(exec)(`"${archiveCopy}"`);
742
+ }
743
+
744
+ static #extractOperaArchive(archive, dir, filename) {
745
+ switch (process.platform) {
746
+ case "win32":
747
+ return Opera.#installOnWindows(archive, dir, filename);
748
+ case "linux":
749
+ return Opera.#extractDeb(archive, dir);
750
+ case "darwin":
751
+ return extractTar(archive, dir);
752
+ default:
753
+ throw new Error(`Unexpected platform: ${process.platform}`);
754
+ }
755
+ }
756
+
757
+ /**
758
+ * Downloads the browser binary file.
759
+ * @param {string} version - Either "latest" or a full version number
760
+ * (i.e. "64.0.3417.92"). Defaults to "latest".
761
+ * @return {BrowserBinary}
762
+ */
763
+ static async downloadBinary(version = "latest") {
764
+ let {versionNumber, channelPath, filePrefix} =
765
+ await Opera.#getVersionForChannel(version);
766
+
767
+ let [platformDir, fileSuffix] = {
768
+ "win32-ia32": ["win", "Autoupdate.exe"],
769
+ "win32-x64": ["win", "Autoupdate_x64.exe"],
770
+ "linux-x64": ["linux", "amd64.deb"],
771
+ "darwin-x64": ["mac", "Autoupdate.tar.xz"],
772
+ "dawrin-arm64": ["mac", "Autoupdate_arm64.tar.xz"]
773
+ }[platform];
774
+
775
+ let snapshotsDir = path.join(snapshotsBaseDir, "opera");
776
+ let browserDir = path.join(snapshotsDir, `opera-${platform}-${versionNumber}`);
777
+ let filename = `${filePrefix}_${versionNumber}_${fileSuffix}`;
778
+ let archive = path.join(snapshotsDir, "cache", filename);
779
+
780
+ let binary = Opera.#getOperaBinary(browserDir);
781
+ try {
782
+ await fs.promises.access(browserDir);
783
+ return {binary, versionNumber};
784
+ }
785
+ catch (e) {}
786
+
787
+ await fs.promises.mkdir(path.dirname(browserDir), {recursive: true});
788
+ try {
789
+ await fs.promises.access(archive);
790
+ }
791
+ catch (e) {
792
+ await download(
793
+ `https://ftp.opera.com/pub/${channelPath}/${versionNumber}/${platformDir}/${filename}`,
794
+ archive
795
+ );
796
+ }
797
+
798
+ await Opera.#extractOperaArchive(archive, browserDir, filename);
799
+ return {binary, versionNumber};
800
+ }
801
+
802
+ static async #installDriver(version, originalVersion) {
803
+ let [zip, driver] = {
804
+ "win32-ia32": ["operadriver_win32.zip", "operadriver.exe"],
805
+ "win32-x64": ["operadriver_win64.zip", "operadriver.exe"],
806
+ "linux-x64": ["operadriver_linux64.zip", "operadriver"],
807
+ "darwin-x64": ["operadriver_mac64.zip", "operadriver"],
808
+ "darwin-arm64": ["operadriver_mac64.zip", "operadriver"]
809
+ }[platform];
810
+
811
+ let {versionNumber} = await Opera.#getVersionForChannel(version);
812
+ versionNumber = versionNumber.split(".")[0];
813
+
814
+ let cacheDir = path.join(snapshotsBaseDir, "opera", "cache",
815
+ `driver-for-opera-${versionNumber}`);
816
+ let archive = path.join(cacheDir, zip);
817
+
818
+ let {body} = await got(`https://github.com/operasoftware/operachromiumdriver/releases?q=Opera+${versionNumber}&expanded=true`);
819
+ let regex = /release-card[\s\S]*Link--primary.*>(.*)<\/a/gm;
820
+ let matches = body.match(regex);
821
+ if (!matches || matches.length == 0)
822
+ throw new Error(`Driver for Opera ${version} was not found`);
823
+ let driverVersion = regex.exec(matches[matches.length - 1])[1];
824
+
825
+ try {
826
+ await download(`https://github.com/operasoftware/operachromiumdriver/releases/download/v.${driverVersion}/${zip}`,
827
+ archive);
828
+ }
829
+ catch (err) {
830
+ throw new Error(`Downloading operadriver failed: ${err}`);
831
+ }
832
+
833
+ await killDriverProcess("operadriver");
834
+ let driverPath = path.join(cacheDir, zip.split(".")[0], driver);
835
+ try {
836
+ await fs.promises.rm(driverPath, {recursive: true});
837
+ }
838
+ catch (e) {} // file does not exist
839
+ await extractZip(archive, {dir: cacheDir});
840
+ await fs.promises.chmod(driverPath, 577);
841
+
842
+ return driverPath;
843
+ }
844
+
845
+ /** @see Browser.getDriver */
846
+ static async getDriver(version = "latest", {
847
+ headless = true, extensionPaths = [], incognito = false, insecure = false,
848
+ extraArgs = []
849
+ } = {}) {
850
+ let {binary, versionNumber} = await Opera.downloadBinary(version);
851
+ let driverPath = await Opera.#installDriver(versionNumber, version);
852
+ // operadriver uses chrome as a service builder:
853
+ // https://github.com/operasoftware/operachromiumdriver/blob/master/docs/desktop.md
854
+ let serviceBuilder = new chrome.ServiceBuilder(driverPath);
855
+ let service = serviceBuilder.build();
856
+ await service.start();
857
+
858
+ let options = new chrome.Options().addArguments("no-sandbox", ...extraArgs);
859
+ if (extensionPaths.length > 0)
860
+ options.addArguments(`load-extension=${extensionPaths.join(",")}`);
861
+ if (headless)
862
+ options.headless();
863
+ if (insecure)
864
+ options.addArguments("ignore-certificate-errors");
865
+ if (incognito)
866
+ options.addArguments("incognito");
867
+ options.setChromeBinaryPath(binary);
868
+ // https://github.com/operasoftware/operachromiumdriver/issues/61#issuecomment-579331657
869
+ options.addArguments("remote-debugging-port=9222");
870
+
871
+ let builder = new webdriver.Builder();
872
+ builder.forBrowser("chrome");
873
+ builder.setChromeOptions(options);
874
+ builder.setChromeService(serviceBuilder);
875
+
876
+ return builder.build();
877
+ }
878
+
879
+ /** @see Browser.enableExtensionInIncognito */
880
+ static async enableExtensionInIncognito(driver, extensionTitle) {
881
+ // Extensions page in Opera has the same web elements as Chromium
882
+ await Chromium.enableExtensionInIncognito(driver, extensionTitle);
883
+ }
884
+ }
580
885
 
581
886
  /**
582
887
  * @type {Object}
@@ -587,5 +892,6 @@ class Edge {
587
892
  export const BROWSERS = {
588
893
  chromium: Chromium,
589
894
  firefox: Firefox,
590
- edge: Edge
895
+ edge: Edge,
896
+ opera: Opera
591
897
  };
package/src/utils.js CHANGED
@@ -56,7 +56,7 @@ export async function download(url, destFile) {
56
56
 
57
57
  export async function extractTar(archive, dir) {
58
58
  await fs.promises.mkdir(dir);
59
- await promisify(exec)(["tar", "-jxf", archive, "-C", dir].join(" "));
59
+ await promisify(exec)(`tar -jxf ${archive} -C ${dir}`);
60
60
  }
61
61
 
62
62
  export async function extractDmg(archive, dir) {
@@ -78,17 +78,12 @@ export async function extractDmg(archive, dir) {
78
78
  }
79
79
  }
80
80
 
81
- export async function runWinInstaller(archive, dir) {
82
- // Procedure inspired from mozinstall. Uninstaller will also need to be run.
83
- // https://hg.mozilla.org/mozilla-central/file/tip/testing/mozbase/mozinstall/mozinstall/mozinstall.py
84
- await promisify(exec)(`"${archive}" /extractdir=${dir}`);
85
- }
86
-
87
81
  export async function getBrowserVersion(driver) {
88
82
  let version = (await driver.getCapabilities()).getBrowserVersion();
89
- return parseInt(version.split(".")[0], 10);
83
+ return version ? parseInt(version.split(".")[0], 10) : null;
90
84
  }
91
85
 
86
+ // Useful to unlock the driver file before replacing it or executing it
92
87
  export async function killDriverProcess(driverName) {
93
88
  let cmd = `kill $(pgrep ${driverName})`;
94
89
  let shell;
@@ -106,3 +101,51 @@ export async function killDriverProcess(driverName) {
106
101
  throw err;
107
102
  }
108
103
  }
104
+
105
+ export function wait(condition, timeout = 0, message, pollTimeout = 100) {
106
+ if (typeof condition !== "function")
107
+ throw TypeError("Wait condition must be a function");
108
+
109
+ function evaluateCondition() {
110
+ return new Promise((resolve, reject) => {
111
+ try {
112
+ resolve(condition(this));
113
+ }
114
+ catch (ex) {
115
+ reject(ex);
116
+ }
117
+ });
118
+ }
119
+
120
+ let result = new Promise((resolve, reject) => {
121
+ let startTime = Date.now();
122
+ let pollCondition = async() => {
123
+ evaluateCondition().then(value => {
124
+ let elapsed = Date.now() - startTime;
125
+ if (value) {
126
+ resolve(value);
127
+ }
128
+ else if (timeout && elapsed >= timeout) {
129
+ try {
130
+ let timeoutMessage = message ?
131
+ `${typeof message === "function" ? message() : message}\n` : "";
132
+ reject(
133
+ new Error(`${timeoutMessage}Wait timed out after ${elapsed}ms`)
134
+ );
135
+ }
136
+ catch (ex) {
137
+ reject(
138
+ new Error(`${ex.message}\nWait timed out after ${elapsed}ms`)
139
+ );
140
+ }
141
+ }
142
+ else {
143
+ setTimeout(pollCondition, pollTimeout);
144
+ }
145
+ }, reject);
146
+ };
147
+ pollCondition();
148
+ });
149
+
150
+ return result;
151
+ }
package/test/browsers.js CHANGED
@@ -24,13 +24,16 @@ import {killDriverProcess} from "../src/utils.js";
24
24
 
25
25
  // Required to set the driver path on Windows
26
26
  import "chromedriver";
27
+ import "geckodriver";
27
28
  import "msedgedriver";
28
29
 
29
30
  const VERSIONS = {
30
- chromium: [void 0, "beta", "dev", "77.0.3865.0"],
31
- firefox: [void 0, "beta", "68.0"],
32
- edge: [void 0]
31
+ chromium: [void 0, "77.0.3865.0", "beta", "dev"],
32
+ firefox: [void 0, "68.0", "beta"],
33
+ edge: [void 0, "95.0.1020.53", "beta", "dev"],
34
+ opera: [void 0, "64.0.3417.92"]
33
35
  };
36
+ const TEST_URL = "https://gitlab.com/eyeo/developer-experience/get-browser-binary";
34
37
  let extensionPaths = [path.resolve(process.cwd(), "test", "extension")];
35
38
 
36
39
  async function switchToHandle(driver, testFn) {
@@ -62,85 +65,99 @@ async function getHandle(driver, page) {
62
65
  return handle;
63
66
  }
64
67
 
65
- for (let browser of Object.keys(BROWSERS)) {
66
- for (let version of VERSIONS[browser]) {
67
- describe(`Browser: ${browser} ${version || "latest"}`, function() {
68
- this.timeout(20000);
69
-
70
- before(async() => {
71
- try {
72
- await fs.promises.rm(snapshotsBaseDir, {recursive: true});
73
- }
74
- catch (e) {}
75
- });
76
-
77
- let driver = null;
78
- afterEach(async() => {
79
- if (!driver)
80
- return;
81
-
82
- await driver.quit();
83
- driver = null;
84
- // Some platforms don't immediately kill the chromedriver process
85
- if (browser == "chromium")
86
- await killDriverProcess("chromedriver");
87
- });
88
-
89
- it("downloads", async function() {
90
- if (browser == "edge")
91
- // Edge download is not implemented. It is assumed to be installed.
92
- this.skip();
93
-
94
- this.timeout(40000);
95
- let {binary} = await BROWSERS[browser].downloadBinary(version);
96
-
97
- await fs.promises.access(binary);
98
- expect(binary).toEqual(
99
- expect.stringContaining(path.join(snapshotsBaseDir, browser)));
100
- });
101
-
102
- it("runs", async() => {
103
- let names = {
104
- chromium: "chrome",
105
- firefox: "firefox",
106
- edge: /(MicrosoftEdge|msedge)/
107
- };
108
-
109
- driver = await BROWSERS[browser].getDriver(version);
110
- await driver.navigate().to("about:blank");
111
-
112
- expect((await driver.getCapabilities()).getBrowserName())
113
- .toEqual(expect.stringMatching(names[browser]));
114
- });
115
-
116
- it("supports extra args", async() => {
117
- driver = await BROWSERS[browser].getDriver(
118
- version, {extraArgs: ["auto-open-devtools-for-tabs"]});
119
- await driver.navigate().to("about:blank");
120
- });
121
-
122
- it("loads an extension", async() => {
123
- let headless = browser == "firefox";
124
-
125
- driver = await BROWSERS[browser].getDriver(
126
- version, {headless, extensionPaths});
127
- await getHandle(driver, "/index.html");
128
- });
129
-
130
- it("loads an extension in incognito mode", async function() {
131
- if (browser == "firefox" && version == "68.0")
132
- this.skip();
68
+ function normalize(version) {
69
+ // Discards any numbers after the third dot. For example, "103.0.5060.134"
70
+ // will return "103.0.5060", and "68.0" will return "68.0"
71
+ let normalized = version.split(".").slice(0, 3).join(".");
72
+ // On Windows, Firefox beta versions look like "103.0b9", but the installed
73
+ // version returned by the browser is actually "103.0"
74
+ return normalized.split("b")[0];
75
+ }
133
76
 
134
- if (process.platform == "win32")
135
- this.timeout(30000);
77
+ for (let browser of Object.keys(BROWSERS)) {
78
+ describe(`Browser: ${browser}`, function() {
79
+ this.timeout(40000);
80
+
81
+ before(async() => {
82
+ try {
83
+ await fs.promises.rm(snapshotsBaseDir, {recursive: true});
84
+ }
85
+ catch (e) {}
86
+ });
136
87
 
137
- driver = await BROWSERS[browser].getDriver(
138
- version, {headless: false, extensionPaths, incognito: true});
139
- await BROWSERS[browser].enableExtensionInIncognito(
140
- driver, "Browser download test extension"
141
- );
142
- await getHandle(driver, "/index.html");
88
+ for (let version of VERSIONS[browser]) {
89
+ describe(`Version: ${version || "latest"}`, () => {
90
+ let driver = null;
91
+ afterEach(async() => {
92
+ if (!driver)
93
+ return;
94
+
95
+ await driver.quit();
96
+ driver = null;
97
+ // Some platforms don't immediately kill the driver process
98
+ if (browser == "chromium")
99
+ await killDriverProcess("chromedriver");
100
+ else if (browser == "firefox")
101
+ await killDriverProcess("geckodriver");
102
+ });
103
+
104
+ it("downloads", async function() {
105
+ if (browser == "edge" && process.platform != "linux")
106
+ this.skip();
107
+
108
+ let {binary, versionNumber} =
109
+ await BROWSERS[browser].downloadBinary(version);
110
+ let browserDir = browser == "opera" ? /(opera|Opera)/ : browser;
111
+ expect(binary).toEqual(expect.stringMatching(browserDir));
112
+
113
+ let installedVersion =
114
+ await BROWSERS[browser].getInstalledVersion(binary);
115
+ expect(installedVersion).toEqual(
116
+ expect.stringContaining(normalize(versionNumber)));
117
+ });
118
+
119
+ it("runs", async() => {
120
+ let names = {
121
+ chromium: "chrome",
122
+ firefox: "firefox",
123
+ edge: /(MicrosoftEdge|msedge)/,
124
+ opera: /(opera|chrome)/
125
+ };
126
+
127
+ driver = await BROWSERS[browser].getDriver(version);
128
+ await driver.navigate().to(TEST_URL);
129
+
130
+ expect((await driver.getCapabilities()).getBrowserName())
131
+ .toEqual(expect.stringMatching(names[browser]));
132
+ });
133
+
134
+ it("supports extra args", async() => {
135
+ let extraArgs = browser == "firefox" ?
136
+ ["--devtools"] : ["auto-open-devtools-for-tabs"];
137
+ driver = await BROWSERS[browser].getDriver(version, {extraArgs});
138
+ await driver.navigate().to(TEST_URL);
139
+ });
140
+
141
+ it("loads an extension", async() => {
142
+ let headless = browser == "firefox";
143
+
144
+ driver = await BROWSERS[browser].getDriver(
145
+ version, {headless, extensionPaths});
146
+ await getHandle(driver, "/index.html");
147
+ });
148
+
149
+ it("loads an extension in incognito mode", async function() {
150
+ if (browser == "firefox" && version == "68.0")
151
+ this.skip();
152
+
153
+ driver = await BROWSERS[browser].getDriver(
154
+ version, {headless: false, extensionPaths, incognito: true});
155
+ await BROWSERS[browser].enableExtensionInIncognito(
156
+ driver, "Browser download test extension"
157
+ );
158
+ await getHandle(driver, "/index.html");
159
+ });
143
160
  });
144
- });
145
- }
161
+ }
162
+ });
146
163
  }
@@ -0,0 +1,20 @@
1
+ FROM node:16-bullseye-slim
2
+
3
+ # General packages
4
+ RUN apt-get update && apt-get install -y git procps wget unzip bzip2 gnupg
5
+ # xvfb (headful browser run)
6
+ RUN apt-get install -y libgtk-3-0 libxt6 xvfb libnss3 libxss1
7
+ # General browser dependencies
8
+ RUN apt-get install -y libgconf-2-4 libasound2 libgbm1
9
+ # Edge dependencies
10
+ RUN apt-get install -y fonts-liberation libatomic1 xdg-utils
11
+ # Opera dependencies
12
+ RUN apt-get install -y libcurl4 libgdk-pixbuf2.0-0
13
+
14
+ COPY package*.json get-browser-binary/
15
+ RUN cd get-browser-binary && npm install
16
+
17
+ COPY . get-browser-binary/
18
+
19
+ ENV TEST_ARGS="--grep Browser"
20
+ ENTRYPOINT get-browser-binary/test/docker/entrypoint.sh
@@ -0,0 +1,4 @@
1
+ #! /bin/bash
2
+
3
+ cd get-browser-binary
4
+ xvfb-run -a npm test -- $TEST_ARGS