Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

2.8 KB
Raw Blame
using CloakBrowser;
using Microsoft.Playwright;
using Xunit;

namespace CloakBrowser.Tests;

/// <summary>
/// Window-geometry / no_viewport behavior - port of upstream 50bf14b tests
/// (Python tests/test_launch_context.py + test_persistent_context.py, JS
/// js/tests/launch.test.ts + puppeteer.test.ts).
///
/// Headed launches must NOT layer an emulated viewport on top of the real
/// browser window: CDP viewport emulation forces outerWidth &lt; innerWidth, a
/// physically impossible window (= bot tell). Headless also defaults to no
/// viewport: the binary reports coherent dimensions itself.
/// </summary>
public class ViewportTests
{
    // ViewportSize uses reference equality, so compare by the sentinel value that
    // Playwright's ViewportSize.NoViewport carries ({ Width = -1, Height = -1 }).
    private static bool IsNoViewport(ViewportSize? vp) =>
        vp != null && vp.Width == -1 && vp.Height == -1;

    [Fact]
    public void Viewport_option_is_removed_with_a_compile_error()
    {
        // Setting Viewport must fail the caller's build and point at --window-size.
        var attr = (ObsoleteAttribute?)Attribute.GetCustomAttribute(
            typeof(LaunchContextOptions).GetProperty("Viewport")!, typeof(ObsoleteAttribute));
        Assert.NotNull(attr);
        Assert.True(attr!.IsError);
        Assert.Contains("--window-size", attr.Message);
    }

    // -----------------------------------------------------------------------
    // ViewportDefaults.ApplyNoViewport - the shared default applied on EVERY
    // page/context creation path (the raw handle AND the humanize wrapper),
    // mirroring Python's _default_no_viewport which patches the browser so the
    // default holds regardless of which path creates the page.
    // -----------------------------------------------------------------------

    [Fact]
    public void Default_page_unset_becomes_no_viewport()
    {
        var o = ViewportDefaults.ApplyNoViewport((BrowserNewPageOptions?)null);
        Assert.True(IsNoViewport(o.ViewportSize));
    }

    [Fact]
    public void Default_context_unset_becomes_no_viewport()
    {
        var o = ViewportDefaults.ApplyNoViewport((BrowserNewContextOptions?)null);
        Assert.True(IsNoViewport(o.ViewportSize));
    }

    [Fact]
    public void Default_explicit_viewport_honored()
    {
        var input = new BrowserNewPageOptions { ViewportSize = new ViewportSize { Width = 1024, Height = 768 } };
        var o = ViewportDefaults.ApplyNoViewport(input);
        Assert.Equal(1024, o.ViewportSize!.Width);
        Assert.Equal(768, o.ViewportSize.Height);
    }

    [Fact]
    public void Default_explicit_no_viewport_honored()
    {
        var input = new BrowserNewContextOptions { ViewportSize = ViewportSize.NoViewport };
        var o = ViewportDefaults.ApplyNoViewport(input);
        Assert.True(IsNoViewport(o.ViewportSize));
    }
}