Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.TestUtils.VerifyViewportAsync
ElementHandleScreenshotTests.cs
Source:ElementHandleScreenshotTests.cs
...109 <div class=""to-screenshot""></div>");110 var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");111 byte[] screenshot = await elementHandle.ScreenshotAsync();112 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-larger-than-viewport.png", screenshot));113 await TestUtils.VerifyViewportAsync(Page, 500, 500);114 }115 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should scroll element into view")]116 public async Task ShouldScrollElementIntoView()117 {118 await Page.SetViewportSizeAsync(500, 500);119 await Page.SetContentAsync(@"120 <div style=""height: 14px"">oooo</div>121 <style>div.above {122 border: 2px solid blue;123 background: red;124 height: 1500px;125 }126 div.to-screenshot {127 border: 2px solid blue;128 background: green;129 width: 50px;130 height: 50px;131 }132 </style>133 <div class=""above""></div>134 <div class=""to-screenshot""></div>");135 var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");136 byte[] screenshot = await elementHandle.ScreenshotAsync();137 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot));138 }139 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should scroll 15000px into view")]140 public async Task ShouldScroll15000pxIntoView()141 {142 await Page.SetViewportSizeAsync(500, 500);143 await Page.SetContentAsync(@"144 <div style=""height: 14px"">oooo</div>145 <style>div.above {146 border: 2px solid blue;147 background: red;148 height: 15000px;149 }150 div.to-screenshot {151 border: 2px solid blue;152 background: green;153 width: 50px;154 height: 50px;155 }156 </style>157 <div class=""above""></div>158 <div class=""to-screenshot""></div>");159 var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");160 byte[] screenshot = await elementHandle.ScreenshotAsync();161 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot));162 }163 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should work with a rotated element")]164 public async Task ShouldWorkWithARotatedElement()165 {166 await Page.SetViewportSizeAsync(500, 500);167 await Page.SetContentAsync(@"168 <div style='position: absolute;169 top: 100px;170 left: 100px;171 width: 100px;172 height: 100px;173 background: green;174 transform: rotateZ(200deg); '> </div>175 ");176 var elementHandle = await Page.QuerySelectorAsync("div");177 byte[] screenshot = await elementHandle.ScreenshotAsync();178 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-rotate.png", screenshot));179 }180 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should fail to screenshot a detached element")]181 public async Task ShouldFailToScreenshotADetachedElement()182 {183 await Page.SetContentAsync("<h1>remove this</h1>");184 var elementHandle = await Page.QuerySelectorAsync("h1");185 await Page.EvaluateAsync("element => element.remove()", elementHandle);186 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => elementHandle.ScreenshotAsync());187 StringAssert.Contains("Element is not attached to the DOM", exception.Message);188 }189 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should timeout waiting for visible")]190 public async Task ShouldTimeoutWaitingForVisible()191 {192 await Page.SetContentAsync(@"<div style='width: 50px; height: 0'></div>");193 var elementHandle = await Page.QuerySelectorAsync("div");194 var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => elementHandle.ScreenshotAsync(new() { Timeout = 3000 }));195 StringAssert.Contains("Timeout 3000ms exceeded", exception.Message);196 StringAssert.Contains("element is not visible", exception.Message);197 }198 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should wait for visible")]199 public async Task ShouldWaitForVisible()200 {201 await Page.SetViewportSizeAsync(500, 500);202 await Page.GotoAsync(Server.Prefix + "/grid.html");203 await Page.EvaluateAsync("() => window.scrollBy(50, 100)");204 var elementHandle = await Page.QuerySelectorAsync(".box:nth-of-type(3)");205 await elementHandle.EvaluateAsync("e => e.style.visibility = 'hidden'");206 var task = elementHandle.ScreenshotAsync();207 for (int i = 0; i < 10; i++)208 {209 await Page.EvaluateAsync("() => new Promise(f => requestAnimationFrame(f))");210 }211 Assert.False(task.IsCompleted);212 await elementHandle.EvaluateAsync("e => e.style.visibility = 'visible'");213 byte[] screenshot = await task;214 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-bounding-box.png", screenshot));215 }216 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should work for an element with fractional dimensions")]217 public async Task ShouldWorkForAnElementWithFractionalDimensions()218 {219 await Page.SetContentAsync("<div style=\"width:48.51px;height:19.8px;border:1px solid black;\"></div>");220 var elementHandle = await Page.QuerySelectorAsync("div");221 byte[] screenshot = await elementHandle.ScreenshotAsync();222 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot));223 }224 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should work with a mobile viewport")]225 [Skip(SkipAttribute.Targets.Firefox)]226 public async Task ShouldWorkWithAMobileViewport()227 {228 await using var context = await Browser.NewContextAsync(new()229 {230 ViewportSize = new()231 {232 Width = 320,233 Height = 480,234 },235 IsMobile = true,236 });237 var page = await context.NewPageAsync();238 await page.GotoAsync(Server.Prefix + "/grid.html");239 await page.EvaluateAsync("() => window.scrollBy(50, 100)");240 var elementHandle = await page.QuerySelectorAsync(".box:nth-of-type(3)");241 byte[] screenshot = await elementHandle.ScreenshotAsync();242 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-mobile.png", screenshot));243 }244 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should work with device scale factor")]245 [Skip(SkipAttribute.Targets.Firefox)]246 public async Task ShouldWorkWithDeviceScaleFactor()247 {248 await using var context = await Browser.NewContextAsync(new()249 {250 ViewportSize = new()251 {252 Width = 320,253 Height = 480,254 },255 DeviceScaleFactor = 2,256 });257 var page = await context.NewPageAsync();258 await page.GotoAsync(Server.Prefix + "/grid.html");259 await page.EvaluateAsync("() => window.scrollBy(50, 100)");260 var elementHandle = await page.QuerySelectorAsync(".box:nth-of-type(3)");261 byte[] screenshot = await elementHandle.ScreenshotAsync();262 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-mobile-dsf.png", screenshot));263 }264 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should work for an element with an offset")]265 public async Task ShouldWorkForAnElementWithAnOffset()266 {267 await Page.SetContentAsync("<div style=\"position:absolute; top: 10.3px; left: 20.4px;width:50.3px;height:20.2px;border:1px solid black;\"></div>");268 var elementHandle = await Page.QuerySelectorAsync("div");269 byte[] screenshot = await elementHandle.ScreenshotAsync();270 Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));271 }272 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should take screenshots when default viewport is null")]273 public async Task ShouldTakeScreenshotsWhenDefaultViewportIsNull()274 {275 await using var context = await Browser.NewContextAsync(new()276 {277 ViewportSize = ViewportSize.NoViewport278 });279 var page = await context.NewPageAsync();280 await page.SetContentAsync("<div style='height: 10000px; background: red'></div>");281 var windowSize = await page.EvaluateAsync<ViewportSize>("() => ({ width: window.innerWidth * window.devicePixelRatio, height: window.innerHeight * window.devicePixelRatio })");282 var sizeBefore = await page.EvaluateAsync<ViewportSize>("() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })");283 byte[] screenshot = await page.ScreenshotAsync();284 Assert.NotNull(screenshot);285 var decoded = Image.Load(screenshot);286 Assert.AreEqual(windowSize.Width, decoded.Width);287 Assert.AreEqual(windowSize.Height, decoded.Height);288 var sizeAfter = await page.EvaluateAsync<ViewportSize>("() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })");289 Assert.AreEqual(sizeBefore.Width, sizeAfter.Width);290 Assert.AreEqual(sizeBefore.Height, sizeAfter.Height);291 }292 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should take fullPage screenshots when default viewport is null")]293 public async Task ShouldTakeFullPageScreenshotsWhenDefaultViewportIsNull()294 {295 await using var context = await Browser.NewContextAsync(new()296 {297 ViewportSize = ViewportSize.NoViewport298 });299 var page = await context.NewPageAsync();300 await page.GotoAsync(Server.Prefix + "/grid.html");301 var sizeBefore = await page.EvaluateAsync<ViewportSize>("() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })");302 byte[] screenshot = await page.ScreenshotAsync(new() { FullPage = true });303 Assert.NotNull(screenshot);304 var sizeAfter = await page.EvaluateAsync<ViewportSize>("() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })");305 Assert.AreEqual(sizeBefore.Width, sizeAfter.Width);306 Assert.AreEqual(sizeBefore.Height, sizeAfter.Height);307 }308 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should restore default viewport after fullPage screenshot")]309 public async Task ShouldRestoreDefaultViewportAfterFullPageScreenshot()310 {311 await using var context = await Browser.NewContextAsync(new()312 {313 ViewportSize = new() { Width = 456, Height = 789 },314 });315 var page = await context.NewPageAsync();316 await TestUtils.VerifyViewportAsync(page, 456, 789);317 byte[] screenshot = await page.ScreenshotAsync(new() { FullPage = true });318 Assert.NotNull(screenshot);319 await TestUtils.VerifyViewportAsync(page, 456, 789);320 }321 [PlaywrightTest("elementhandle-screenshot.spec.ts", "should take element screenshot when default viewport is null and restore back")]322 public async Task ShouldTakeElementScreenshotWhenDefaultViewportIsNullAndRestoreBack()323 {324 await using var context = await Browser.NewContextAsync(new()325 {326 ViewportSize = ViewportSize.NoViewport,327 });328 var page = await context.NewPageAsync();329 await page.SetContentAsync(@"330 <div style=""height: 14px"">oooo</div>331 <style>332 div.to-screenshot {333 border: 1px solid blue;...
BrowserContextBasicTests.cs
Source:BrowserContextBasicTests.cs
...109 Height = 789110 }111 });112 var page = await context.NewPageAsync();113 await TestUtils.VerifyViewportAsync(page, 456, 789);114 }115 [PlaywrightTest("browsercontext-basic.spec.ts", "should make a copy of default viewport")]116 public async Task ShouldMakeACopyOfDefaultViewport()117 {118 var viewport = new ViewportSize119 {120 Width = 456,121 Height = 789122 };123 await using var context = await Browser.NewContextAsync(new() { ViewportSize = viewport });124 viewport.Width = 567;125 var page = await context.NewPageAsync();126 await TestUtils.VerifyViewportAsync(page, 456, 789);127 }128 [PlaywrightTest("browsercontext-basic.spec.ts", "should respect deviceScaleFactor")]129 public async Task ShouldRespectDeviceScaleFactor()130 {131 await using var context = await Browser.NewContextAsync(new()132 {133 DeviceScaleFactor = 3134 });135 var page = await context.NewPageAsync();136 Assert.AreEqual(3, await page.EvaluateAsync<int>("window.devicePixelRatio"));137 }138 [PlaywrightTest("browsercontext-basic.spec.ts", "should not allow deviceScaleFactor with null viewport")]139 public async Task ShouldNotAllowDeviceScaleFactorWithViewportDisabled()140 {...
DefaultBrowserContext1Tests.cs
Source:DefaultBrowserContext1Tests.cs
...156 Width = 456,157 Height = 789158 }159 });160 await TestUtils.VerifyViewportAsync(page, 456, 789);161 page = await context.NewPageAsync();162 await TestUtils.VerifyViewportAsync(page, 456, 789);163 await context.DisposeAsync();164 tmp.Dispose();165 }166 [PlaywrightTest("defaultbrowsercontext-1.spec.ts", "should support deviceScaleFactor option")]167 public async Task ShouldSupportDeviceScaleFactorOption()168 {169 var (tmp, context, page) = await LaunchPersistentAsync(new()170 {171 DeviceScaleFactor = 3172 });173 Assert.AreEqual(3, await page.EvaluateAsync<int>("window.devicePixelRatio"));174 await context.DisposeAsync();175 tmp.Dispose();176 }...
BrowserContextViewportTests.cs
Source:BrowserContextViewportTests.cs
...29 public class BrowserContextViewportTests : PageTestEx30 {31 [PlaywrightTest("browsercontext-viewport.spec.ts", "should get the proper default viewport size")]32 public Task ShouldGetTheProperDefaultViewPortSize()33 => TestUtils.VerifyViewportAsync(Page, 1280, 720);34 [PlaywrightTest("browsercontext-viewport.spec.ts", "should set the proper viewport size")]35 public async Task ShouldSetTheProperViewportSize()36 {37 await TestUtils.VerifyViewportAsync(Page, 1280, 720);38 await Page.SetViewportSizeAsync(123, 456);39 await TestUtils.VerifyViewportAsync(Page, 123, 456);40 }41 [PlaywrightTest("browsercontext-viewport.spec.ts", "should emulate device width")]42 public async Task ShouldEmulateDeviceWidth()43 {44 await TestUtils.VerifyViewportAsync(Page, 1280, 720);45 await Page.SetViewportSizeAsync(200, 200);46 Assert.AreEqual(200, await Page.EvaluateAsync<int>("window.innerWidth"));47 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-width: 100px)').matches"));48 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-width: 300px)').matches"));49 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-width: 100px)').matches"));50 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-width: 300px)').matches"));51 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-width: 500px)').matches"));52 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-width: 200px)').matches"));53 await Page.SetViewportSizeAsync(500, 500);54 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-width: 400px)').matches"));55 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-width: 600px)').matches"));56 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-width: 400px)').matches"));57 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-width: 600px)').matches"));58 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-width: 200px)').matches"));59 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-width: 500px)').matches"));60 }61 [PlaywrightTest("browsercontext-viewport.spec.ts", "should emulate device height")]62 public async Task ShouldEmulateDeviceHeight()63 {64 await TestUtils.VerifyViewportAsync(Page, 1280, 720);65 await Page.SetViewportSizeAsync(200, 200);66 Assert.AreEqual(200, await Page.EvaluateAsync<int>("window.innerWidth"));67 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-height: 100px)').matches"));68 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-height: 300px)').matches"));69 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-height: 100px)').matches"));70 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-height: 300px)').matches"));71 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-height: 500px)').matches"));72 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-height: 200px)').matches"));73 await Page.SetViewportSizeAsync(500, 500);74 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-height: 400px)').matches"));75 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(min-device-height: 600px)').matches"));76 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-height: 400px)').matches"));77 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(max-device-height: 600px)').matches"));78 Assert.False(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-height: 200px)').matches"));79 Assert.True(await Page.EvaluateAsync<bool?>("() => matchMedia('(device-height: 500px)').matches"));80 }81 [PlaywrightTest("browsercontext-viewport.spec.ts", "should not have touch by default")]82 public async Task ShouldNotHaveTouchByDefault()83 {84 await Page.GotoAsync(Server.Prefix + "/mobile.html");85 Assert.False(await Page.EvaluateAsync<bool>("'ontouchstart' in window"));86 await Page.GotoAsync(Server.Prefix + "/detect-touch.html");87 Assert.AreEqual("NO", await Page.EvaluateAsync<string>("document.body.textContent.trim()"));88 }89 [PlaywrightTest("browsercontext-viewport.spec.ts", "should support touch with null viewport")]90 public async Task ShouldSupportTouchWithNullViewport()91 {92 await using var context = await Browser.NewContextAsync(new() { ViewportSize = null, HasTouch = true });93 var page = await context.NewPageAsync();94 await page.GotoAsync(Server.Prefix + "/mobile.html");95 Assert.True(await page.EvaluateAsync<bool>("'ontouchstart' in window"));96 }97 [PlaywrightTest("browsercontext-viewport.spec.ts", "should respect screensize")]98 [Skip(SkipAttribute.Targets.Firefox)]99 public async Task ShouldSupportScreenSize()100 {101 await using var context = await Browser.NewContextAsync(new()102 {103 ScreenSize = new ScreenSize()104 {105 Width = 750,106 Height = 1334,107 },108 ViewportSize = new ViewportSize()109 {110 Width = 375,111 Height = 667112 }113 });114 var page = await context.NewPageAsync();115 Assert.True(await page.EvaluateAsync<bool?>("() => matchMedia('(device-height: 1334px)').matches"));116 Assert.True(await page.EvaluateAsync<bool?>("() => matchMedia('(device-width: 750px)').matches"));117 await TestUtils.VerifyViewportAsync(page, 375, 667);118 }119 [PlaywrightTest("browsercontext-viewport.spec.ts", "should ignore screensize when viewport is null")]120 [Skip(SkipAttribute.Targets.Firefox)]121 public async Task ShouldIgnoreScreensizeWhenViewportIsNull()122 {123 await using var context = await Browser.NewContextAsync(new()124 {125 ScreenSize = new ScreenSize()126 {127 Width = 750,128 Height = 1334,129 },130 ViewportSize = ViewportSize.NoViewport131 });...
TestUtils.cs
Source:TestUtils.cs
...124 {125 }126 }127 internal static string GetAsset(string path) => Path.Combine(FindParentDirectory("Playwright.Tests.TestServer"), "assets", path);128 internal static async Task VerifyViewportAsync(IPage page, int width, int height)129 {130 Assert.AreEqual(width, (int)page.ViewportSize.Width);131 Assert.AreEqual(height, (int)page.ViewportSize.Height);132 Assert.AreEqual(width, await page.EvaluateAsync<int>("window.innerWidth"));133 Assert.AreEqual(height, await page.EvaluateAsync<int>("window.innerHeight"));134 }135 internal static async Task RegisterEngineAsync(IPlaywright playwright, string name, string script, bool? contentScript = null)136 {137 try138 {139 await playwright.Selectors.RegisterAsync(name, new() { Script = script, ContentScript = contentScript });140 }141 catch (PlaywrightException ex) when (ex.Message.Contains("has been already registered"))142 {...
VerifyViewportAsync
Using AI Code Generation
1await TestUtils.VerifyViewportAsync(page, 1280, 720);2await TestUtils.VerifyViewportAsync(page, 1280, 720);3await TestUtils.VerifyViewportAsync(page, 1280, 720);4await TestUtils.VerifyViewportAsync(page, 1280, 720);5await TestUtils.VerifyViewportAsync(page, 1280, 720);6await TestUtils.VerifyViewportAsync(page, 1280, 720);7await TestUtils.VerifyViewportAsync(page, 1280, 720);8await TestUtils.VerifyViewportAsync(page, 1280, 720);9await TestUtils.VerifyViewportAsync(page, 1280, 720);10await TestUtils.VerifyViewportAsync(page, 1280, 720);11await TestUtils.VerifyViewportAsync(page, 1280, 720);12await TestUtils.VerifyViewportAsync(page, 1280, 720);13await TestUtils.VerifyViewportAsync(page, 1280, 720);
VerifyViewportAsync
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Tests;3using System.Threading.Tasks;4using Xunit;5using Xunit.Abstractions;6{7 {8 public VerifyViewportAsyncTests(ITestOutputHelper output) : base(output)9 {10 }11 [PlaywrightTest("2.cs", "should work")]12 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]13 public async Task ShouldWork()14 {15 await Page.SetViewportSizeAsync(500, 500);16 await TestUtils.VerifyViewportAsync(Page, 500, 500);17 await Page.SetViewportSizeAsync(700, 500);18 await TestUtils.VerifyViewportAsync(Page, 700, 500);19 }20 }21}
VerifyViewportAsync
Using AI Code Generation
1using Microsoft.Playwright.Tests;2using System;3using System.Threading.Tasks;4using Xunit;5{6 {7 public async Task VerifyViewportAsyncTest()8 {9 var browser = await BrowserType.LaunchAsync();10 var context = await browser.NewContextAsync();11 var page = await context.NewPageAsync();12 await TestUtils.VerifyViewportAsync(page, 1280, 720);13 await browser.CloseAsync();14 }15 }16}17using Microsoft.Playwright.Tests;18using System;19using System.Threading.Tasks;20using Xunit;21{22 {23 public async Task VerifyViewportAsyncTest()24 {25 var browser = await BrowserType.LaunchAsync();26 var context = await browser.NewContextAsync();27 var page = await context.NewPageAsync();28 await TestUtils.VerifyViewportAsync(page, 1280, 720);29 await browser.CloseAsync();30 }31 }32}33using Microsoft.Playwright.Tests;34using System;35using System.Threading.Tasks;36using Xunit;37{38 {39 public async Task VerifyViewportAsyncTest()40 {41 var browser = await BrowserType.LaunchAsync();42 var context = await browser.NewContextAsync();43 var page = await context.NewPageAsync();44 await TestUtils.VerifyViewportAsync(page, 1280, 720);45 await browser.CloseAsync();46 }47 }48}49using Microsoft.Playwright.Tests;50using System;51using System.Threading.Tasks;52using Xunit;53{54 {55 public async Task VerifyViewportAsyncTest()56 {57 var browser = await BrowserType.LaunchAsync();
VerifyViewportAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.Tests;5using NUnit.Framework;6using PlaywrightSharp.Tests.BaseTests;7{8 {9 public async Task Test1()10 {11 var page = await Browser.NewPageAsync();12 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");13 var elementHandle = await page.QuerySelectorAsync("#box6");14 await elementHandle.ScrollIntoViewIfNeededAsync();15 await TestUtils.VerifyViewportAsync(page, 500, 500);16 }17 }18}19 at Microsoft.Playwright.Tests.TestUtils.VerifyViewportAsync(IPage page, Int32 width, Int32 height) in C:\Users\user\source\repos\PlaywrightSharp\src\PlaywrightSharp.Tests\Utils\TestUtils.cs:line 3520 at PlaywrightSharp.Tests.PlaywrightTests.Test1() in C:\Users\user\source\repos\PlaywrightSharp\src\PlaywrightSharp.Tests\PlaywrightTests.cs:line 1821var viewportSize = await page.EvaluateAsync<string>("() => { return { width: window.innerWidth, height: window.innerHeight } }");22Assert.AreEqual(500, viewportSize.width);23Assert.AreEqual(500, viewportSize.height);24 at PlaywrightSharp.Tests.PlaywrightTests.Test1() in C:\Users\user\source\repos\PlaywrightSharp\src\PlaywrightSharp.Tests\PlaywrightTests.cs:line 1925var viewportSize = await page.EvaluateAsync<string>("() => { return { width: window.innerWidth, height: window.innerHeight } }");26Assert.AreEqual(500, viewportSize.width);27Assert.AreEqual(500, viewportSize.height);
VerifyViewportAsync
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Tests;3using System;4using System.Collections.Generic;5using System.Text;6using System.Threading.Tasks;7using Xunit;8using Xunit.Abstractions;9{10{11public PageEmulateTests(ITestOutputHelper output) : base(output)12{13}14[PlaywrightTest("page-emulate.spec.ts", "Page.emulate", "should work")]15[Fact(Timeout = PlaywrightTestConstants.DefaultTestTimeout)]16public async Task ShouldWork()17{18await Page.EmulateViewportAsync(500, 500);19await Page.GotoAsync(Server.Prefix + "/grid.html");20await TestUtils.VerifyViewportAsync(Page, 500, 500);21}22}23}24public static async Task VerifyViewportAsync(IPage page, int width, int height)25{26var size = await page.EvaluateAsync<ViewportSize>(@"() => {27return {28};29}");30Assert.Equal(width, size.Width);31Assert.Equal(height, size.Height);32}33public Task EmulateMediaAsync(string mediaType = null);34using Microsoft.Playwright;35using Microsoft.Playwright.Tests;36using System;37using System.Collections.Generic;38using System.Text;39using System.Threading.Tasks;40using Xunit;41using Xunit.Abstractions;42{43{44public PageEmulateTests(ITestOutputHelper output) : base(output)45{46}47[PlaywrightTest("page-emulate.spec.ts", "Page.emulate", "should work
VerifyViewportAsync
Using AI Code Generation
1public async Task VerifyViewportAsync(string pageName, string url, string selector, string screenshotName, string[] selectorsToHide)2{3 using var playwright = await Playwright.CreateAsync();4 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions5 {6 });7 var context = await browser.NewContextAsync(new BrowserNewContextOptions8 {9 ViewportSize = new ViewportSize() { Width = 1920, Height = 1080 }10 });11 var page = await context.NewPageAsync();12 await page.GotoAsync(url);13 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);14 foreach (var selectorToHide in selectorsToHide)15 {16 await page.QuerySelectorAsync(selectorToHide).EvaluateAsync("element => element.style.display = 'none'");17 }18 var screenshot = await page.ScreenshotAsync(new PageScreenshotOptions19 {20 });21 await TestUtils.VerifyViewportAsync(page, selector, screenshotName);22}23public async Task VerifyViewportAsync(string pageName, string url, string selector, string screenshotName, string[] selectorsToHide)24{25 using var playwright = await Playwright.CreateAsync();26 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions27 {28 });29 var context = await browser.NewContextAsync(new BrowserNewContextOptions30 {31 ViewportSize = new ViewportSize() { Width = 1920, Height = 1080 }32 });33 var page = await context.NewPageAsync();34 await page.GotoAsync(url);35 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);36 foreach (var selectorToHide in selectorsToHide)37 {38 await page.QuerySelectorAsync(selectorToHide).EvaluateAsync("element => element.style.display = 'none'");39 }40 var screenshot = await page.ScreenshotAsync(new PageScreenshotOptions41 {
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!