Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Support devtools and custom profile directory for Chrome via environment variable #628

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ In that case `<cert-name>` 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" ]
}
}
}
}
----
16 changes: 11 additions & 5 deletions static/chrome/devtools/devtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions static/chrome/devtools/devtools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}