fix: set default viewport for NewBrowser and align User-Agent with engine
NewBrowser previously had no viewport (strong headless signal) and used a Firefox User-Agent unconditionally, even for Chromium instances (detectable mismatch). Add per-engine UA constants (DefaultFirefoxUserAgent, DefaultChromiumUserAgent) and auto-select the matching UA in initBrowser when the caller hasn't set one explicitly. Keep DefaultUserAgent as a backward-compatible alias. Add 1920x1080 default viewport to NewBrowser (most common desktop resolution). NewInteractiveBrowser keeps its existing 1280x720 default but also gains engine-aware UA selection. Closes #70 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -374,3 +374,68 @@ func TestStealthFirefoxScripts_NoChromiumMarkers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- User-Agent constants ---
|
||||
|
||||
func TestDefaultUserAgent_BackwardCompat(t *testing.T) {
|
||||
if DefaultUserAgent != DefaultFirefoxUserAgent {
|
||||
t.Fatal("DefaultUserAgent must equal DefaultFirefoxUserAgent for backward compatibility")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultFirefoxUserAgent_Content(t *testing.T) {
|
||||
if !strings.Contains(DefaultFirefoxUserAgent, "Firefox") {
|
||||
t.Fatal("DefaultFirefoxUserAgent must contain 'Firefox'")
|
||||
}
|
||||
if strings.Contains(DefaultFirefoxUserAgent, "Chrome") {
|
||||
t.Fatal("DefaultFirefoxUserAgent must not contain 'Chrome'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultChromiumUserAgent_Content(t *testing.T) {
|
||||
if !strings.Contains(DefaultChromiumUserAgent, "Chrome") {
|
||||
t.Fatal("DefaultChromiumUserAgent must contain 'Chrome'")
|
||||
}
|
||||
if strings.Contains(DefaultChromiumUserAgent, "Firefox") {
|
||||
t.Fatal("DefaultChromiumUserAgent must not contain 'Firefox'")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Viewport and UA defaults via mergeOptions ---
|
||||
|
||||
func TestMergeOptions_DefaultViewport(t *testing.T) {
|
||||
base := BrowserOptions{
|
||||
Dimensions: Size{Width: 1920, Height: 1080},
|
||||
}
|
||||
got := mergeOptions(base, nil)
|
||||
if got.Dimensions.Width != 1920 || got.Dimensions.Height != 1080 {
|
||||
t.Fatalf("expected default viewport 1920x1080, got %dx%d", got.Dimensions.Width, got.Dimensions.Height)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_ViewportOverride(t *testing.T) {
|
||||
base := BrowserOptions{
|
||||
Dimensions: Size{Width: 1920, Height: 1080},
|
||||
}
|
||||
got := mergeOptions(base, []BrowserOptions{{Dimensions: Size{Width: 1280, Height: 720}}})
|
||||
if got.Dimensions.Width != 1280 || got.Dimensions.Height != 720 {
|
||||
t.Fatalf("expected overridden viewport 1280x720, got %dx%d", got.Dimensions.Width, got.Dimensions.Height)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_EmptyUANotOverridden(t *testing.T) {
|
||||
base := BrowserOptions{}
|
||||
got := mergeOptions(base, []BrowserOptions{{Browser: BrowserChromium}})
|
||||
if got.UserAgent != "" {
|
||||
t.Fatalf("expected empty UserAgent after merge with no explicit UA, got %q", got.UserAgent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_ExplicitUAPreserved(t *testing.T) {
|
||||
base := BrowserOptions{}
|
||||
customUA := "MyCustomAgent/1.0"
|
||||
got := mergeOptions(base, []BrowserOptions{{UserAgent: customUA}})
|
||||
if got.UserAgent != customUA {
|
||||
t.Fatalf("expected explicit UA %q preserved, got %q", customUA, got.UserAgent)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user