microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

Viewport auto resize to browser size #1722

Closed cirrusone closed 3 years ago

cirrusone commented 3 years ago

The ViewportSize seems to be fixed and won't resize if a user resizes the browser window. Is it possible to allow the ViewportSize to auto resize when user changes browser size? I've seen a few posts on this library and others where setting the Viewport = null worked but it's not working here.

I would expect when not using headless that the viewport can be auto changed by the user. I understand viewport not changing when in headless mode but sometimes we need to test animations and fluid layouts when size changes.

The following examples all have a fixed viewport which does not auto resize (see image).

        public static async Task Main()
        {
            // Package Manager Console:
            // dotnet tool install --global Microsoft.Playwright.CLI
            // playwright install

            using var playwright = await Playwright.CreateAsync();
            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });

            //// default - no context
            //var page = await browser.NewPageAsync();

            // context ViewportSize = null
            await using var context = await browser.NewContextAsync(new BrowserNewContextOptions { ViewportSize = null });
            var page = await context.NewPageAsync();

            // context fixed  ViewportSize = new ViewportSize() { Width = 1920, Height = 1080 }
            //await using var context = await browser.NewContextAsync(new BrowserNewContextOptions { ViewportSize = new ViewportSize() { Width = 1920, Height = 1080 } });
            //var page = await context.NewPageAsync();

            await page.GotoAsync("https://playwright.dev/dotnet");
            Console.ReadLine();
        }

Untitled

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Playwright" Version="1.14.1" />
  </ItemGroup>

</Project>
avodovnik commented 3 years ago
/// Emulates consistent viewport for each page. Defaults to an 1280x720 viewport. Use
/// <c>ViewportSize.NoViewport</c> to disable the default viewport.

As per the docs, you need to pass in ViewportSize.NoViewport if you want that particular behaviour.

cirrusone commented 3 years ago

Thank you, I missed that in the docs. I did look do a search on the docs which returned Page.ViewportSize https://playwright.dev/dotnet/docs/api/class-page#page-viewport-size