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

Commit 3cb501b

Browse files
committed
Updates chrome flag while launching a browser
Playwright update: https://github.com/microsoft/playwright/blob/43b7e6c26db717f6711e37bf44e9623421fd5df9/packages/playwright-core/src/server/chromium/chromium.ts#L288-L315 You should read the source code for finding other flags that I used. Fixes #147
1 parent 3aca99a commit 3cb501b

File tree

2 files changed

+147
-41
lines changed

2 files changed

+147
-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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
)
29+
30+
func TestBrowserTypeFlags(t *testing.T) {
31+
t.Parallel()
32+
33+
t.Run("devtools", func(t *testing.T) {
34+
t.Parallel()
35+
36+
const devToolsFlag = "auto-open-devtools-for-tabs"
37+
38+
var bt BrowserType
39+
40+
flags := bt.flags(&common.LaunchOptions{
41+
Devtools: false,
42+
})
43+
flagi, ok := flags[devToolsFlag]
44+
45+
if !ok {
46+
t.Fatalf("%q is missing", devToolsFlag)
47+
}
48+
flag, ok := flagi.(bool)
49+
if !ok {
50+
t.Fatalf("%q should be a bool", devToolsFlag)
51+
}
52+
if flag {
53+
t.Fatalf("%q should be false when launch options Devtools is false, got %t", devToolsFlag, flag)
54+
}
55+
56+
flags = bt.flags(&common.LaunchOptions{
57+
Devtools: true,
58+
})
59+
flag = flags[devToolsFlag].(bool)
60+
if !flag {
61+
t.Fatalf("%q should be true when launch options Devtools is true, got %t", devToolsFlag, flag)
62+
}
63+
})
64+
65+
t.Run("headless", func(t *testing.T) {
66+
t.Parallel()
67+
68+
var (
69+
before, after int
70+
bt BrowserType
71+
lopts = &common.LaunchOptions{}
72+
)
73+
74+
lopts.Headless = false
75+
before = len(bt.flags(lopts))
76+
77+
lopts.Headless = true
78+
after = len(bt.flags(lopts))
79+
80+
if before == after {
81+
t.Errorf("enabling headless mode did not add flags")
82+
}
83+
})
84+
85+
t.Run("darwin", func(t *testing.T) {
86+
t.Parallel()
87+
if runtime.GOOS != "darwin" {
88+
t.Skip()
89+
}
90+
91+
var bt BrowserType
92+
f := bt.flags(&common.LaunchOptions{})
93+
94+
if _, ok := f["enable-use-zoom-for-dsf"]; !ok {
95+
t.Errorf("darwin should enable 'enable-use-zoom-for-dsf'")
96+
}
97+
})
98+
}

0 commit comments

Comments
 (0)