Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.GeolocationTests.ShouldUseContextOptions
GeolocationTests.cs
Source:GeolocationTests.cs
...102 Assert.AreEqual(options.Geolocation.Latitude, geolocation.Latitude);103 Assert.AreEqual(options.Geolocation.Longitude, geolocation.Longitude);104 }105 [PlaywrightTest("geolocation.spec.ts", "should use context options")]106 public async Task ShouldUseContextOptions()107 {108 var options = new BrowserNewContextOptions()109 {110 Geolocation = new()111 {112 Latitude = 10,113 Longitude = 10114 },115 Permissions = new[] { "geolocation" },116 };117 await using var context = await Browser.NewContextAsync(options);118 var page = await context.NewPageAsync();119 await page.GotoAsync(Server.EmptyPage);120 var geolocation = await page.EvaluateAsync<Geolocation>(@"() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {121 resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});122 }))");123 Assert.AreEqual(options.Geolocation.Latitude, geolocation.Latitude);124 Assert.AreEqual(options.Geolocation.Longitude, geolocation.Longitude);125 }126 [PlaywrightTest("geolocation.spec.ts", "watchPosition should be notified")]127 public async Task WatchPositionShouldBeNotified()128 {129 await Context.GrantPermissionsAsync(new[] { "geolocation" });130 await Page.GotoAsync(Server.EmptyPage);131 var messages = new List<string>();132 Page.Console += (_, e) => messages.Add(e.Text);133 await Context.SetGeolocationAsync(new()134 {135 Longitude = 0,136 Latitude = 0137 });138 await Page.EvaluateAsync<Geolocation>(@"() => {139 navigator.geolocation.watchPosition(pos => {140 const coords = pos.coords;141 console.log(`lat=${coords.latitude} lng=${coords.longitude}`);142 }, err => {});143 }");144 await Page.RunAndWaitForConsoleMessageAsync(async () =>145 {146 await Context.SetGeolocationAsync(new() { Latitude = 0, Longitude = 10 });147 }, new()148 {149 Predicate = e => e.Text.Contains("lat=0 lng=10")150 });151 await TaskUtils.WhenAll(152 Page.WaitForConsoleMessageAsync(new()153 {154 Predicate = e => e.Text.Contains("lat=20 lng=30")155 }),156 Context.SetGeolocationAsync(new() { Latitude = 20, Longitude = 30 }));157 await TaskUtils.WhenAll(158 Page.WaitForConsoleMessageAsync(new()159 {160 Predicate = e => e.Text.Contains("lat=40 lng=50")161 }),162 Context.SetGeolocationAsync(new() { Latitude = 40, Longitude = 50 }));163 string allMessages = string.Join("|", messages);164 StringAssert.Contains("lat=0 lng=10", allMessages);165 StringAssert.Contains("lat=20 lng=30", allMessages);166 StringAssert.Contains("lat=40 lng=50", allMessages);167 }168 [PlaywrightTest("geolocation.spec.ts", "should use context options for popup")]169 public async Task ShouldUseContextOptionsForPopup()170 {171 await Context.GrantPermissionsAsync(new[] { "geolocation" });172 await Context.SetGeolocationAsync(new()173 {174 Longitude = 10,175 Latitude = 10,176 });177 var popupTask = Page.WaitForPopupAsync();178 await TaskUtils.WhenAll(179 popupTask,180 Page.EvaluateAsync("url => window._popup = window.open(url)", Server.Prefix + "/geolocation.html"));181 await popupTask.Result.WaitForLoadStateAsync();182 var geolocation = await popupTask.Result.EvaluateAsync<Geolocation>("() => window.geolocationPromise");183 Assert.AreEqual(10, geolocation.Longitude);...
ShouldUseContextOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Text;4{5 {6 [PlaywrightTest("geolocation.spec.ts", "should use context options")]7 public void ShouldUseContextOptions()8 {9 }10 }11}
ShouldUseContextOptions
Using AI Code Generation
1{2 using System;3 using System.Collections.Generic;4 using System.Linq;5 using System.Text;6 using System.Threading.Tasks;7 using Microsoft.Playwright;8 using Xunit;9 using Xunit.Abstractions;10 {11 internal GeolocationTests(ITestOutputHelper output) : base(output)12 {13 }14 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]15 public async Task ShouldWork()16 {17 await Page.SetGeolocationAsync(new Geolocation18 {19 });20 Assert.Equal(new Geolocation21 {22 }, await Page.EvaluateAsync<Geolocation>("() => window.navigator.geolocation.getCurrentPosition(success => {\n window.result = success.coords;\n })"));23 }24 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]25 public async Task ShouldThrowWhenInvalidLongitudeIsPassed()26 {27 var exception = await Assert.ThrowsAsync<PlaywrightSharpException>(() => Page.SetGeolocationAsync(new Geolocation28 {29 }));30 Assert.Contains("Invalid longitude \"200\": precondition -180 <= LONGITUDE <= 180 failed.", exception.Message);31 }32 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]33 public async Task ShouldThrowWhenInvalidLatitudeIsPassed()34 {35 var exception = await Assert.ThrowsAsync<PlaywrightSharpException>(() => Page.SetGeolocationAsync(new Geolocation36 {37 }));38 Assert.Contains("Invalid latitude \"100\": precondition -90 <= LATITUDE <= 90 failed.", exception.Message);39 }40 }41}
ShouldUseContextOptions
Using AI Code Generation
1using Microsoft.Playwright.Tests;2{3 {4 [PlaywrightTest("geolocation.spec.ts", "should use context options")]5 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]6 public async Task ShouldUseContextOptions()7 {8 await using var context = await Browser.NewContextAsync(new BrowserNewContextOptions9 {10 {11 },12 Permissions = new[] { "geolocation" },13 });14 var page = await context.NewPageAsync();15 await page.GotoAsync(Server.EmptyPage);16 var geolocation = await page.EvaluateAsync<Geolocation>("() => window.navigator.geolocation");17 Assert.Equal(10, geolocation.Longitude);18 Assert.Equal(10, geolocation.Latitude);19 Assert.Equal(0, geolocation.Accuracy);20 }21 }22}
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!!