From 1921f31f5780ef911020d065573e977f1c0288b6 Mon Sep 17 00:00:00 2001 From: Matthew Lowry Date: Mon, 26 Jun 2023 11:54:00 +0930 Subject: [PATCH] Support custome browser profile dir for Chrome via environment var. --- docs/features.adoc | 22 ++++++++++++++++++++++ static/chrome/devtools/devtools.go | 16 +++++++++++----- static/chrome/devtools/devtools_test.go | 12 ++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/docs/features.adoc b/docs/features.adoc index b6c8ba98b..bfdd9ac0b 100644 --- a/docs/features.adoc +++ b/docs/features.adoc @@ -24,3 +24,25 @@ In that case `` will be used as certificate name in the browser certi ROOT_CA_MY_CERT="LS0tL....==" +=== Using Custom Browser Profile with Chrome + +If launching Chrome with a custom profile directory, Devtools will not work unless you +also set the `BROWSER_PROFILE_DIR` environment variable. _E.g._ when requesting the session: + +[source,json] +---- +{ + "capabilities": { + "alwaysMatch": { + "browserName": "chrome", + "browserVersion": "114.0", + "goog:chromeOptions": { + "args": [ "user-data-dir=/profiles/custom.XYZ" ] + }, + "selenoid:options": { + "env": [ "BROWSER_PROFILE_DIR=/profiles/custom.XYZ" ] + } + } + } +} +---- diff --git a/static/chrome/devtools/devtools.go b/static/chrome/devtools/devtools.go index ce53f63af..438c61f6d 100644 --- a/static/chrome/devtools/devtools.go +++ b/static/chrome/devtools/devtools.go @@ -153,13 +153,19 @@ func androidDevtoolsHost() (string, error) { func detectDevtoolsHost(baseDir string) string { var candidates []string - for _, glob := range []string{".com.google.Chrome*", ".org.chromium.Chromium*"} { - cds, err := filepath.Glob(filepath.Join(baseDir, glob)) - if err == nil { - candidates = append(candidates, cds...) - } + pd, found := os.LookupEnv("BROWSER_PROFILE_DIR") + if found { + candidates = append(candidates, pd) + } else { + for _, glob := range []string{".com.google.Chrome*", ".org.chromium.Chromium*"} { + cds, err := filepath.Glob(filepath.Join(baseDir, glob)) + if err == nil { + candidates = append(candidates, cds...) + } + } } + for _, c := range candidates { f, err := os.Stat(c) if err != nil { diff --git a/static/chrome/devtools/devtools_test.go b/static/chrome/devtools/devtools_test.go index 6105e7d68..2938b9531 100644 --- a/static/chrome/devtools/devtools_test.go +++ b/static/chrome/devtools/devtools_test.go @@ -174,3 +174,15 @@ func TestDetectDevtoolsHost(t *testing.T) { AssertThat(t, detectDevtoolsHost(name), EqualTo{"127.0.0.1:12345"}) } + +func TestDetectDevtoolsHostProfileDir(t *testing.T) { + name, _ := ioutil.TempDir("", "devtools") + defer os.RemoveAll(name) + profilePath := filepath.Join(name, "can_be_anything") + os.MkdirAll(profilePath, os.ModePerm) + portFile := filepath.Join(profilePath, "DevToolsActivePort") + ioutil.WriteFile(portFile, []byte("12345\n/devtools/browser/6f37c7fe-a0a6-4346-a6e2-80c5da0687f0"), 0644) + + t.Setenv("BROWSER_PROFILE_DIR", profilePath) + AssertThat(t, detectDevtoolsHost(name), EqualTo{"127.0.0.1:12345"}) +}