Skip to content

Commit

Permalink
Search chrome binary in different locations on different OS systems. (#…
Browse files Browse the repository at this point in the history
…811)

This change provides a slightly more efficient way of
discovering the `chrome` binary path. It also solves
a discovery issue on `darwin` OS when `chromium` is
installed through `homebrew` #752.
  • Loading branch information
fabio42 committed May 4, 2021
1 parent 6a53e09 commit 9191ea2
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions allocate.go
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
"time"
)
Expand Down Expand Up @@ -325,32 +326,43 @@ func ExecPath(path string) ExecAllocatorOption {
}

// findExecPath tries to find the Chrome browser somewhere in the current
// system. It performs a rather agressive search, which is the same in all
// systems. That may make it a bit slow, but it will only be run when creating a
// new ExecAllocator.
// system. It finds in different locations on different OS systems.
// It could perform a rather aggressive search. That may make it a bit slow,
// but it will only be run when creating a new ExecAllocator.
func findExecPath() string {
for _, path := range [...]string{
// Unix-like
"headless_shell",
"headless-shell",
"chromium",
"chromium-browser",
"google-chrome",
"google-chrome-stable",
"google-chrome-beta",
"google-chrome-unstable",
"/usr/bin/google-chrome",

// Windows
"chrome",
"chrome.exe", // in case PATHEXT is misconfigured
`C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
`C:\Program Files\Google\Chrome\Application\chrome.exe`,
filepath.Join(os.Getenv("USERPROFILE"), `AppData\Local\Google\Chrome\Application\chrome.exe`),

// Mac
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
} {
var locations []string
switch runtime.GOOS {
case "darwin":
locations = []string{
// Mac
"/Applications/Chromium.app/Contents/MacOS/Chromium",
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
}
case "windows":
locations = []string{
// Windows
"chrome",
"chrome.exe", // in case PATHEXT is misconfigured
`C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
`C:\Program Files\Google\Chrome\Application\chrome.exe`,
filepath.Join(os.Getenv("USERPROFILE"), `AppData\Local\Google\Chrome\Application\chrome.exe`),
}
default:
locations = []string{
// Unix-like
"headless_shell",
"headless-shell",
"chromium",
"chromium-browser",
"google-chrome",
"google-chrome-stable",
"google-chrome-beta",
"google-chrome-unstable",
"/usr/bin/google-chrome",
}
}

for _, path := range locations {
found, err := exec.LookPath(path)
if err == nil {
return found
Expand Down

0 comments on commit 9191ea2

Please sign in to comment.