Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 6e9234d

Browse files
authored
Merge pull request #148 from grafana/fix/147-update-chrome-launch-flags
Fix/147 update chrome launch flags
2 parents 18eb9e4 + c6e5fd7 commit 6e9234d

File tree

2 files changed

+181
-41
lines changed

2 files changed

+181
-41
lines changed

chromium/browser_type.go

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"fmt"
2727
"os"
2828
"regexp"
29+
"runtime"
2930

3031
"github.com/dop251/goja"
3132
"github.com/grafana/xk6-browser/api"
@@ -85,47 +86,7 @@ func (b *BrowserType) Launch(opts goja.Value) api.Browser {
8586
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
8687
}
8788

88-
flags := map[string]interface{}{
89-
//chromedp.ProxyServer(""),
90-
//chromedp.UserAgent(""),
91-
//chromedp.UserDataDir(""),
92-
//chromedp.DisableGPU,
93-
94-
"no-first-run": true,
95-
"no-default-browser-check": true,
96-
"no-sandbox": true,
97-
"headless": launchOpts.Headless,
98-
"auto-open-devtools-for-tabs": launchOpts.Devtools,
99-
"window-size": fmt.Sprintf("%d,%d", 800, 600),
100-
101-
// After Puppeteer's and Playwright's default behavior.
102-
"disable-background-networking": true,
103-
"enable-features": "NetworkService,NetworkServiceInProcess",
104-
"disable-background-timer-throttling": true,
105-
"disable-backgrounding-occluded-windows": true,
106-
"disable-breakpad": true,
107-
"disable-client-side-phishing-detection": true,
108-
"disable-component-extensions-with-background-pages": true,
109-
"disable-default-apps": true,
110-
"disable-dev-shm-usage": true,
111-
"disable-extensions": true,
112-
"disable-features": "TranslateUI,BlinkGenPropertyTrees,ImprovedCookieControls,SameSiteByDefaultCookies,LazyFrameLoading",
113-
"disable-hang-monitor": true,
114-
"disable-ipc-flooding-protection": true,
115-
"disable-popup-blocking": true,
116-
"disable-prompt-on-repost": true,
117-
"disable-renderer-backgrounding": true,
118-
"disable-sync": true,
119-
"force-color-profile": "srgb",
120-
"metrics-recording-only": true,
121-
"safebrowsing-disable-auto-update": true,
122-
"enable-automation": true,
123-
"password-store": "basic",
124-
"use-mock-keychain": true,
125-
"no-startup-window": true,
126-
}
127-
128-
allocator := NewAllocator(flags, envs)
89+
allocator := NewAllocator(b.flags(launchOpts), envs)
12990
browserProc, err := allocator.Allocate(b.Ctx, launchOpts)
13091
if browserProc == nil {
13192
k6common.Throw(rt, fmt.Errorf("cannot allocate browser: %w", err))
@@ -158,6 +119,53 @@ func (b *BrowserType) Name() string {
158119
return "chromium"
159120
}
160121

122+
func (b *BrowserType) flags(lopts *common.LaunchOptions) map[string]interface{} {
123+
// After Puppeteer's and Playwright's default behavior.
124+
f := map[string]interface{}{
125+
"disable-background-networking": true,
126+
"enable-features": "NetworkService,NetworkServiceInProcess",
127+
"disable-background-timer-throttling": true,
128+
"disable-backgrounding-occluded-windows": true,
129+
"disable-breakpad": true,
130+
"disable-client-side-phishing-detection": true,
131+
"disable-component-extensions-with-background-pages": true,
132+
"disable-default-apps": true,
133+
"disable-dev-shm-usage": true,
134+
"disable-extensions": true,
135+
"disable-features": "ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,AcceptCHFrame",
136+
"disable-hang-monitor": true,
137+
"disable-ipc-flooding-protection": true,
138+
"disable-popup-blocking": true,
139+
"disable-prompt-on-repost": true,
140+
"disable-renderer-backgrounding": true,
141+
"disable-sync": true,
142+
"force-color-profile": "srgb",
143+
"metrics-recording-only": true,
144+
"no-first-run": true,
145+
"safebrowsing-disable-auto-update": true,
146+
"enable-automation": true,
147+
"password-store": "basic",
148+
"use-mock-keychain": true,
149+
"no-service-autorun": true,
150+
151+
"no-startup-window": true,
152+
"no-default-browser-check": true,
153+
"no-sandbox": true,
154+
"headless": lopts.Headless,
155+
"auto-open-devtools-for-tabs": lopts.Devtools,
156+
"window-size": fmt.Sprintf("%d,%d", 800, 600),
157+
}
158+
if runtime.GOOS == "darwin" {
159+
f["enable-use-zoom-for-dsf"] = false
160+
}
161+
if lopts.Headless {
162+
f["hide-scrollbars"] = true
163+
f["mute-audio"] = true
164+
f["blink-settings"] = "primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4"
165+
}
166+
return f
167+
}
168+
161169
// makes an extension wide logger
162170
func makeLogger(ctx context.Context, launchOpts *common.LaunchOptions) (*common.Logger, error) {
163171
var (

chromium/browser_type_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
*
3+
* xk6-browser - a browser automation extension for k6
4+
* Copyright (C) 2021 Load Impact
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
package chromium
22+
23+
import (
24+
"runtime"
25+
"testing"
26+
27+
"github.com/grafana/xk6-browser/common"
28+
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func TestBrowserTypeFlags(t *testing.T) {
33+
t.Parallel()
34+
35+
t.Run("devtools", func(t *testing.T) {
36+
t.Parallel()
37+
38+
const devToolsFlag = "auto-open-devtools-for-tabs"
39+
40+
var (
41+
bt BrowserType
42+
lopts = &common.LaunchOptions{}
43+
)
44+
45+
flags := bt.flags(lopts)
46+
require.Contains(t, flags, devToolsFlag)
47+
48+
_, ok := flags[devToolsFlag].(bool)
49+
require.Truef(t, ok, "%q should be a bool", devToolsFlag)
50+
51+
lopts.Devtools = false
52+
assert.Falsef(t,
53+
flags[devToolsFlag].(bool),
54+
"%q should also be false if launch options Devtools is false", devToolsFlag,
55+
)
56+
57+
flags = bt.flags(&common.LaunchOptions{
58+
Devtools: true,
59+
})
60+
assert.Truef(t,
61+
flags[devToolsFlag].(bool),
62+
"%q should be true when launch options Devtools is true", devToolsFlag,
63+
)
64+
})
65+
66+
t.Run("headless", func(t *testing.T) {
67+
t.Parallel()
68+
69+
const headlessFlag = "headless"
70+
71+
var (
72+
bt BrowserType
73+
lopts = &common.LaunchOptions{}
74+
)
75+
76+
flags := bt.flags(lopts)
77+
require.Contains(t, flags, headlessFlag)
78+
79+
_, ok := flags[headlessFlag].(bool)
80+
require.Truef(t, ok, "%q should be a bool", headlessFlag)
81+
82+
lopts.Headless = false
83+
assert.Falsef(t,
84+
flags[headlessFlag].(bool),
85+
"%q should also be false if launch options Headless is false", headlessFlag,
86+
)
87+
88+
flags = bt.flags(&common.LaunchOptions{
89+
Headless: true,
90+
})
91+
assert.Truef(t,
92+
flags[headlessFlag].(bool),
93+
"%q should be true when launch options Headless is true", headlessFlag,
94+
)
95+
})
96+
97+
t.Run("headless/enabled", func(t *testing.T) {
98+
t.Parallel()
99+
100+
var (
101+
before, after int
102+
bt BrowserType
103+
lopts = &common.LaunchOptions{}
104+
)
105+
106+
lopts.Headless = false
107+
before = len(bt.flags(lopts))
108+
109+
lopts.Headless = true
110+
after = len(bt.flags(lopts))
111+
112+
assert.NotEqual(t, before, after, "enabling headless mode did not add flags")
113+
})
114+
115+
t.Run("darwin", func(t *testing.T) {
116+
t.Parallel()
117+
if runtime.GOOS != "darwin" {
118+
t.Skip()
119+
}
120+
121+
const zoomFlag = "enable-use-zoom-for-dsf"
122+
123+
var bt BrowserType
124+
f := bt.flags(&common.LaunchOptions{})
125+
126+
assert.Containsf(t, f, zoomFlag, "%q should exist for OSX (darwin)", zoomFlag)
127+
128+
flag, ok := f[zoomFlag].(bool)
129+
require.Truef(t, ok, "%q should be a bool", zoomFlag)
130+
assert.False(t, flag, zoomFlag)
131+
})
132+
}

0 commit comments

Comments
 (0)