How to use BrowserContext class of Microsoft.Playwright.Core package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.BrowserContext

Crawler.cs

Source:Crawler.cs Github

copy

Full Screen

...198 }199200 private async Task<PageData?> AnalyzeUrlAsync(AnalyzeContext context)201 {202 var page = await context.BrowserContext.NewPageAsync();203 try204 {205 // perf: Enabling routing disables http cache206 await page.RouteAsync("**", async route =>207 {208 if (route.Request.ResourceType is "image" or "media" or "font")209 {210 await route.AbortAsync();211 }212 else213 {214 await route.ContinueAsync();215 }216 }); ...

Full Screen

Full Screen

BrowserContext.cs

Source:BrowserContext.cs Github

copy

Full Screen

...35using Microsoft.Playwright.Transport.Channels;36using Microsoft.Playwright.Transport.Protocol;37namespace Microsoft.Playwright.Core38{39 internal class BrowserContext : ChannelOwnerBase, IChannelOwner<BrowserContext>, IBrowserContext40 {41 private readonly TaskCompletionSource<bool> _closeTcs = new();42 private readonly Dictionary<string, Delegate> _bindings = new();43 private readonly BrowserContextInitializer _initializer;44 private readonly ITracing _tracing;45 private List<RouteSetting> _routes = new();46 private float? _defaultNavigationTimeout;47 private float? _defaultTimeout;48 internal BrowserContext(IChannelOwner parent, string guid, BrowserContextInitializer initializer) : base(parent, guid)49 {50 Channel = new(guid, parent.Connection, this);51 Channel.Close += (_, _) => OnClose();52 Channel.Page += Channel_OnPage;53 Channel.BindingCall += Channel_BindingCall;54 Channel.Route += Channel_Route;55 Channel.RequestFailed += (_, e) =>56 {57 e.Request.Failure = e.FailureText;58 e.Request.Timing.ResponseEnd = e.ResponseEndTiming;59 RequestFailed?.Invoke(this, e.Request);60 e.Page?.FireRequestFailed(e.Request);61 e.Response?.ReportFinished(e.FailureText);62 };63 Channel.Request += (_, e) =>64 {65 Request?.Invoke(this, e.Request);66 e.Page?.FireRequest(e.Request);67 };68 Channel.RequestFinished += (_, e) =>69 {70 e.Request.Timing.ResponseEnd = e.ResponseEndTiming;71 e.Request.Sizes = e.RequestSizes;72 RequestFinished?.Invoke(this, e.Request);73 e.Page?.FireRequestFinished(e.Request);74 e.Response?.ReportFinished();75 };76 Channel.Response += (_, e) =>77 {78 Response?.Invoke(this, e.Response);79 e.Page?.FireResponse(e.Response);80 };81 _tracing = initializer.Tracing;82 _initializer = initializer;83 Browser = parent as IBrowser;84 }85 public event EventHandler<IBrowserContext> Close;86 public event EventHandler<IPage> Page;87 public event EventHandler<IRequest> Request;88 public event EventHandler<IRequest> RequestFailed;89 public event EventHandler<IRequest> RequestFinished;90 public event EventHandler<IResponse> Response;91 public ITracing Tracing92 {93 get => _tracing;94 set => throw new NotSupportedException();95 }96 ChannelBase IChannelOwner.Channel => Channel;97 IChannel<BrowserContext> IChannelOwner<BrowserContext>.Channel => Channel;98 public IBrowser Browser { get; }99 public IReadOnlyList<IPage> Pages => PagesList;100 internal float DefaultNavigationTimeout101 {102 get => _defaultNavigationTimeout ?? PlaywrightImpl.DefaultTimeout;103 set104 {105 _defaultNavigationTimeout = value;106 Channel.SetDefaultNavigationTimeoutNoReplyAsync(value).IgnoreException();107 }108 }109 internal float DefaultTimeout110 {111 get => _defaultTimeout ?? PlaywrightImpl.DefaultTimeout;112 set113 {114 _defaultTimeout = value;115 Channel.SetDefaultTimeoutNoReplyAsync(value).IgnoreException();116 }117 }118 internal BrowserContextChannel Channel { get; }119 internal List<Page> PagesList { get; } = new();120 internal Page OwnerPage { get; set; }121 internal List<Worker> ServiceWorkersList { get; } = new();122 internal bool IsChromium => _initializer.IsChromium;123 internal BrowserNewContextOptions Options { get; set; }124 public Task AddCookiesAsync(IEnumerable<Cookie> cookies) => Channel.AddCookiesAsync(cookies);125 public Task AddInitScriptAsync(string script = null, string scriptPath = null)126 {127 if (string.IsNullOrEmpty(script))128 {129 script = ScriptsHelper.EvaluationScript(script, scriptPath);130 }131 return Channel.AddInitScriptAsync(script);132 }133 public Task ClearCookiesAsync() => Channel.ClearCookiesAsync();134 public Task ClearPermissionsAsync() => Channel.ClearPermissionsAsync();135 public async Task CloseAsync()136 {137 try138 {139 if (Options.RecordHarPath != null)140 {141 Artifact artifact = await Channel.HarExportAsync().ConfigureAwait(false);142 await artifact.SaveAsAsync(Options.RecordHarPath).ConfigureAwait(false);143 await artifact.DeleteAsync().ConfigureAwait(false);144 }145 await Channel.CloseAsync().ConfigureAwait(false);146 await _closeTcs.Task.ConfigureAwait(false);147 }148 catch (Exception e) when (DriverMessages.IsSafeCloseError(e))149 {150 // Swallow exception151 }152 }153 public Task<IReadOnlyList<BrowserContextCookiesResult>> CookiesAsync(IEnumerable<string> urls = null) => Channel.CookiesAsync(urls);154 public Task ExposeBindingAsync(string name, Action callback, BrowserContextExposeBindingOptions options = default)155 => ExposeBindingAsync(name, callback, handle: options?.Handle ?? false);156 public Task ExposeBindingAsync(string name, Action<BindingSource> callback)157 => ExposeBindingAsync(name, (Delegate)callback);158 public Task ExposeBindingAsync<T>(string name, Action<BindingSource, T> callback)159 => ExposeBindingAsync(name, (Delegate)callback);160 public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, TResult> callback)161 => ExposeBindingAsync(name, (Delegate)callback);162 public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, IJSHandle, TResult> callback)163 => ExposeBindingAsync(name, callback, true);164 public Task ExposeBindingAsync<T, TResult>(string name, Func<BindingSource, T, TResult> callback)165 => ExposeBindingAsync(name, (Delegate)callback);166 public Task ExposeBindingAsync<T1, T2, TResult>(string name, Func<BindingSource, T1, T2, TResult> callback)167 => ExposeBindingAsync(name, (Delegate)callback);168 public Task ExposeBindingAsync<T1, T2, T3, TResult>(string name, Func<BindingSource, T1, T2, T3, TResult> callback)169 => ExposeBindingAsync(name, (Delegate)callback);170 public Task ExposeBindingAsync<T1, T2, T3, T4, TResult>(string name, Func<BindingSource, T1, T2, T3, T4, TResult> callback)171 => ExposeBindingAsync(name, (Delegate)callback);172 public Task ExposeFunctionAsync(string name, Action callback)173 => ExposeBindingAsync(name, (BindingSource _) => callback());174 public Task ExposeFunctionAsync<T>(string name, Action<T> callback)175 => ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));176 public Task ExposeFunctionAsync<TResult>(string name, Func<TResult> callback)177 => ExposeBindingAsync(name, (BindingSource _) => callback());178 public Task ExposeFunctionAsync<T, TResult>(string name, Func<T, TResult> callback)179 => ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));180 public Task ExposeFunctionAsync<T1, T2, TResult>(string name, Func<T1, T2, TResult> callback)181 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2) => callback(t1, t2));182 public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> callback)183 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3) => callback(t1, t2, t3));184 public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> callback)185 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3, T4 t4) => callback(t1, t2, t3, t4));186 public Task GrantPermissionsAsync(IEnumerable<string> permissions, BrowserContextGrantPermissionsOptions options = default)187 => Channel.GrantPermissionsAsync(permissions, options?.Origin);188 public async Task<IPage> NewPageAsync()189 {190 if (OwnerPage != null)191 {192 throw new PlaywrightException("Please use Browser.NewContextAsync()");193 }194 return (await Channel.NewPageAsync().ConfigureAwait(false)).Object;195 }196 public Task RouteAsync(string url, Action<IRoute> handler, BrowserContextRouteOptions options = default)197 => RouteAsync(new Regex(CombineUrlWithBase(url).GlobToRegex()), null, handler, options);198 public Task RouteAsync(Regex url, Action<IRoute> handler, BrowserContextRouteOptions options = default)199 => RouteAsync(url, null, handler, options);200 public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, BrowserContextRouteOptions options = default)201 => RouteAsync(null, url, handler, options);202 public Task SetExtraHTTPHeadersAsync(IEnumerable<KeyValuePair<string, string>> headers)203 => Channel.SetExtraHTTPHeadersAsync(headers);204 public Task SetGeolocationAsync(Geolocation geolocation) => Channel.SetGeolocationAsync(geolocation);205 public Task SetOfflineAsync(bool offline) => Channel.SetOfflineAsync(offline);206 public async Task<string> StorageStateAsync(BrowserContextStorageStateOptions options = default)207 {208 string state = JsonSerializer.Serialize(209 await Channel.GetStorageStateAsync().ConfigureAwait(false),210 JsonExtensions.DefaultJsonSerializerOptions);211 if (!string.IsNullOrEmpty(options?.Path))212 {213 File.WriteAllText(options?.Path, state);214 }215 return state;216 }217 public Task UnrouteAsync(string urlString, Action<IRoute> handler = default)218 => UnrouteAsync(new Regex(CombineUrlWithBase(urlString).GlobToRegex()), null, handler);219 public Task UnrouteAsync(Regex urlRegex, Action<IRoute> handler = default)220 => UnrouteAsync(urlRegex, null, handler);221 public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler = default)222 => UnrouteAsync(null, urlFunc, handler);223 public async Task<T> InnerWaitForEventAsync<T>(PlaywrightEvent<T> playwrightEvent, Func<Task> action = default, Func<T, bool> predicate = default, float? timeout = default)224 {225 if (playwrightEvent == null)226 {227 throw new ArgumentException("Page event is required", nameof(playwrightEvent));228 }229 timeout ??= DefaultTimeout;230 using var waiter = new Waiter(this, $"context.WaitForEventAsync(\"{playwrightEvent.Name}\")");231 waiter.RejectOnTimeout(Convert.ToInt32(timeout), $"Timeout {timeout}ms exceeded while waiting for event \"{playwrightEvent.Name}\"");232 if (playwrightEvent.Name != BrowserContextEvent.Close.Name)233 {234 waiter.RejectOnEvent<IBrowserContext>(this, BrowserContextEvent.Close.Name, new("Context closed"));235 }236 var result = waiter.WaitForEventAsync(this, playwrightEvent.Name, predicate);237 if (action != null)238 {239 await WrapApiBoundaryAsync(() => Task.WhenAll(result, action())).ConfigureAwait(false);240 }241 return await result.ConfigureAwait(false);242 }243 public Task<IPage> WaitForPageAsync(BrowserContextWaitForPageOptions options = default)244 => InnerWaitForEventAsync(BrowserContextEvent.Page, null, options?.Predicate, options?.Timeout);245 public Task<IPage> RunAndWaitForPageAsync(Func<Task> action, BrowserContextRunAndWaitForPageOptions options = default)246 => InnerWaitForEventAsync(BrowserContextEvent.Page, action, options?.Predicate, options?.Timeout);247 public ValueTask DisposeAsync() => new ValueTask(CloseAsync());248 public void SetDefaultNavigationTimeout(float timeout) => DefaultNavigationTimeout = timeout;249 public void SetDefaultTimeout(float timeout) => DefaultTimeout = timeout;250 internal void OnRoute(Route route, IRequest request)251 {252 foreach (var routeHandler in _routes)253 {254 if (255 routeHandler.Regex?.IsMatch(request.Url) == true ||256 routeHandler.Function?.Invoke(request.Url) == true)257 {258 try259 {260 routeHandler.Handle(route);261 }262 finally263 {264 if (!routeHandler.IsActive())265 {266 _routes.Remove(routeHandler);267 if (_routes.Count == 0)268 {269 DisableInterceptionAsync().ConfigureAwait(false);270 }271 }272 }273 return;274 }275 }276 route.ContinueAsync().IgnoreException();277 }278 internal async Task DisableInterceptionAsync()279 {280 await Channel.SetNetworkInterceptionEnabledAsync(false).ConfigureAwait(false);281 }282 internal bool UrlMatches(string url, string glob)283 => new Regex(CombineUrlWithBase(glob).GlobToRegex()).Match(url).Success;284 internal string CombineUrlWithBase(string url)285 {286 var baseUrl = Options?.BaseURL;287 if (string.IsNullOrEmpty(baseUrl)288 || (url?.StartsWith("*", StringComparison.InvariantCultureIgnoreCase) ?? false)289 || !Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))290 {291 return url;292 }293 var mUri = new Uri(url, UriKind.RelativeOrAbsolute);294 if (!mUri.IsAbsoluteUri)295 {296 return new Uri(new Uri(baseUrl), mUri).ToString();297 }298 return url;299 }300 private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Action<IRoute> handler, BrowserContextRouteOptions options)301 => RouteAsync(new()302 {303 Regex = urlRegex,304 Function = urlFunc,305 Handler = handler,306 Times = options?.Times,307 });308 private Task RouteAsync(RouteSetting setting)309 {310 _routes.Insert(0, setting);311 if (_routes.Count == 1)312 {313 return Channel.SetNetworkInterceptionEnabledAsync(true);314 }315 return Task.CompletedTask;316 }317 private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Action<IRoute> handler = null)318 => UnrouteAsync(new()319 {320 Function = urlFunc,321 Regex = urlRegex,322 Handler = handler,323 });324 private Task UnrouteAsync(RouteSetting setting)325 {326 var newRoutesList = new List<RouteSetting>();327 newRoutesList.AddRange(_routes.Where(r =>328 (setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||329 (setting.Function != null && r.Function != setting.Function) ||330 (setting.Handler != null && r.Handler != setting.Handler)));331 _routes = newRoutesList;332 if (_routes.Count == 0)333 {334 return Channel.SetNetworkInterceptionEnabledAsync(false);335 }336 return Task.CompletedTask;337 }338 internal void OnClose()339 {340 if (Browser != null)341 {342 ((Browser)Browser).BrowserContextsList.Remove(this);343 }344 Close?.Invoke(this, this);345 _closeTcs.TrySetResult(true);346 }347 private void Channel_OnPage(object sender, BrowserContextPageEventArgs e)348 {349 var page = e.PageChannel.Object;350 page.Context = this;351 PagesList.Add(page);352 Page?.Invoke(this, page);353 if (page.Opener?.IsClosed == false)354 {355 page.Opener.NotifyPopup(page);356 }357 }358 private void Channel_BindingCall(object sender, BindingCallEventArgs e)359 {360 if (_bindings.TryGetValue(e.BindingCall.Name, out var binding))361 {...

Full Screen

Full Screen

BrowserContextBasicTests.cs

Source:BrowserContextBasicTests.cs Github

copy

Full Screen

...29using Microsoft.Playwright.NUnit;30using NUnit.Framework;31namespace Microsoft.Playwright.Tests32{33 public class BrowserContextBasicTests : BrowserTestEx34 {35 [PlaywrightTest("browsercontext-basic.spec.ts", "should create new context")]36 public async Task ShouldCreateNewContext()37 {38 await using var browser = await BrowserType.LaunchAsync();39 Assert.IsEmpty(browser.Contexts);40 await using var context = await browser.NewContextAsync();41 Assert.That(browser.Contexts, Has.Length.EqualTo(1));42 CollectionAssert.Contains(browser.Contexts, context);43 Assert.AreEqual(browser, context.Browser);44 await context.CloseAsync();45 Assert.IsEmpty(browser.Contexts);46 Assert.AreEqual(browser, context.Browser);47 }48 [PlaywrightTest("browsercontext-basic.spec.ts", "window.open should use parent tab context")]49 public async Task WindowOpenShouldUseParentTabContext()50 {51 await using var context = await Browser.NewContextAsync();52 var page = await context.NewPageAsync();53 await page.GotoAsync(Server.EmptyPage);54 var popupTargetCompletion = new TaskCompletionSource<IPage>();55 page.Popup += (_, e) => popupTargetCompletion.SetResult(e);56 var (popupTarget, _) = await TaskUtils.WhenAll(57 popupTargetCompletion.Task,58 page.EvaluateAsync("url => window.open(url)", Server.EmptyPage)59 );60 Assert.AreEqual(context, popupTarget.Context);61 await context.CloseAsync();62 }63 [PlaywrightTest("browsercontext-basic.spec.ts", "should isolate localStorage and cookies")]64 public async Task ShouldIsolateLocalStorageAndCookies()65 {66 // Create two incognito contexts.67 await using var browser = await BrowserType.LaunchAsync();68 var context1 = await browser.NewContextAsync();69 var context2 = await browser.NewContextAsync();70 Assert.IsEmpty(context1.Pages);71 Assert.IsEmpty(context2.Pages);72 // Create a page in first incognito context.73 var page1 = await context1.NewPageAsync();74 await page1.GotoAsync(Server.EmptyPage);75 await page1.EvaluateAsync(@"() => {76 localStorage.setItem('name', 'page1');77 document.cookie = 'name=page1';78 }");79 Assert.That(context1.Pages, Has.Count.EqualTo(1));80 Assert.IsEmpty(context2.Pages);81 // Create a page in second incognito context.82 var page2 = await context2.NewPageAsync();83 await page2.GotoAsync(Server.EmptyPage);84 await page2.EvaluateAsync(@"() => {85 localStorage.setItem('name', 'page2');86 document.cookie = 'name=page2';87 }");88 Assert.That(context1.Pages, Has.Count.EqualTo(1));89 Assert.AreEqual(page1, context1.Pages.FirstOrDefault());90 Assert.That(context2.Pages, Has.Count.EqualTo(1));91 Assert.AreEqual(page2, context2.Pages.FirstOrDefault());92 // Make sure pages don't share localstorage or cookies.93 Assert.AreEqual("page1", await page1.EvaluateAsync<string>("() => localStorage.getItem('name')"));94 Assert.AreEqual("name=page1", await page1.EvaluateAsync<string>("() => document.cookie"));95 Assert.AreEqual("page2", await page2.EvaluateAsync<string>("() => localStorage.getItem('name')"));96 Assert.AreEqual("name=page2", await page2.EvaluateAsync<string>("() => document.cookie"));97 // Cleanup contexts.98 await TaskUtils.WhenAll(context1.CloseAsync(), context2.CloseAsync());99 Assert.IsEmpty(browser.Contexts);100 }101 [PlaywrightTest("browsercontext-basic.spec.ts", "should propagate default viewport to the page")]102 public async Task ShouldPropagateDefaultViewportToThePage()103 {104 await using var context = await Browser.NewContextAsync(new()105 {106 ViewportSize = new()107 {108 Width = 456,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 {141 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Browser.NewContextAsync(new()142 {143 ViewportSize = ViewportSize.NoViewport,144 DeviceScaleFactor = 3,145 }));146 Assert.AreEqual("\"deviceScaleFactor\" option is not supported with null \"viewport\"", exception.Message);147 }148 [PlaywrightTest("browsercontext-basic.spec.ts", "should not allow isMobile with null viewport")]149 public async Task ShouldNotAllowIsMobileWithViewportDisabled()150 {151 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Browser.NewContextAsync(new()152 {153 ViewportSize = ViewportSize.NoViewport,154 IsMobile = true,155 }));156 Assert.AreEqual("\"isMobile\" option is not supported with null \"viewport\"", exception.Message);157 }158 [PlaywrightTest("browsercontext-basic.spec.ts", "close() should work for empty context")]159 public async Task CloseShouldWorkForEmptyContext()160 {161 var context = await Browser.NewContextAsync();162 await context.CloseAsync();163 }164 [PlaywrightTest("browsercontext-basic.spec.ts", "close() should abort waitForEvent")]165 public async Task CloseShouldAbortWaitForEvent()166 {167 var context = await Browser.NewContextAsync();168 var waitTask = context.WaitForPageAsync();169 await context.CloseAsync();170 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => waitTask);171 Assert.AreEqual("Context closed", exception.Message);172 }173 [PlaywrightTest("browsercontext-basic.spec.ts", "should not report frameless pages on error")]174 public async Task ShouldNotReportFramelessPagesOnError()175 {176 var context = await Browser.NewContextAsync();177 var page = await context.NewPageAsync();178 Server.SetRoute("/empty.html", context =>179 {180 context.Response.ContentType = "text/html";181 return context.Response.WriteAsync($"<a href=\"{Server.EmptyPage}\" target=\"_blank\">Click me</a>");182 });183 IPage popup = null;184 context.Page += (_, e) => popup = e;185 await page.GotoAsync(Server.EmptyPage);186 await page.ClickAsync("'Click me'");187 await context.CloseAsync();188 if (popup != null)189 {190 Assert.True(popup.IsClosed);191 Assert.NotNull(popup.MainFrame);192 }193 }194 [PlaywrightTest("browsercontext-basic.spec.ts", "close() should be callable twice")]195 public async Task CloseShouldBeCallableTwice()196 {197 var context = await Browser.NewContextAsync();198 await TaskUtils.WhenAll(context.CloseAsync(), context.CloseAsync());199 await context.CloseAsync();200 }201 [PlaywrightTest("browsercontext-basic.spec.ts", "should return all of the pages")]202 public async Task ShouldReturnAllOfThePages()203 {204 await using var context = await Browser.NewContextAsync();205 var page = await context.NewPageAsync();206 var second = await context.NewPageAsync();207 Assert.AreEqual(2, context.Pages.Count);208 CollectionAssert.Contains(context.Pages, page);209 CollectionAssert.Contains(context.Pages, second);210 }211 [PlaywrightTest("browsercontext-basic.spec.ts", "BrowserContext.pages()", "should close all belonging pages once closing context")]212 public async Task ShouldCloseAllBelongingPagesOnceClosingContext()213 {214 await using var context = await Browser.NewContextAsync();215 await context.NewPageAsync();216 Assert.That(context.Pages, Has.Count.EqualTo(1));217 await context.CloseAsync();218 Assert.IsEmpty(context.Pages);219 }220 [PlaywrightTest("browsercontext-basic.spec.ts", "should disable javascript")]221 public async Task ShouldDisableJavascript()222 {223 await using (var context = await Browser.NewContextAsync(new() { JavaScriptEnabled = false }))224 {225 var page = await context.NewPageAsync();...

Full Screen

Full Screen

BrowserType.cs

Source:BrowserType.cs Github

copy

Full Screen

...70 ignoreAllDefaultArgs: options.IgnoreAllDefaultArgs).ConfigureAwait(false)).Object;71 browser.LocalUtils = Playwright.Utils;72 return browser;73 }74 public async Task<IBrowserContext> LaunchPersistentContextAsync(string userDataDir, BrowserTypeLaunchPersistentContextOptions options = default)75 {76 options ??= new BrowserTypeLaunchPersistentContextOptions();77 var context = (await _channel.LaunchPersistentContextAsync(78 userDataDir,79 headless: options.Headless,80 channel: options.Channel,81 executablePath: options.ExecutablePath,82 args: options.Args,83 proxy: options.Proxy,84 downloadsPath: options.DownloadsPath,85 tracesDir: options.TracesDir,86 chromiumSandbox: options.ChromiumSandbox,87 handleSIGINT: options.HandleSIGINT,88 handleSIGTERM: options.HandleSIGTERM,89 handleSIGHUP: options.HandleSIGHUP,90 timeout: options.Timeout,91 env: options.Env,92 devtools: options.Devtools,93 slowMo: options.SlowMo,94 acceptDownloads: options.AcceptDownloads,95 ignoreHTTPSErrors: options.IgnoreHTTPSErrors,96 bypassCSP: options.BypassCSP,97 viewportSize: options.ViewportSize,98 screenSize: options.ScreenSize,99 userAgent: options.UserAgent,100 deviceScaleFactor: options.DeviceScaleFactor,101 isMobile: options.IsMobile,102 hasTouch: options.HasTouch,103 javaScriptEnabled: options.JavaScriptEnabled,104 timezoneId: options.TimezoneId,105 geolocation: options.Geolocation,106 locale: options.Locale,107 permissions: options.Permissions,108 extraHTTPHeaders: options.ExtraHTTPHeaders,109 offline: options.Offline,110 httpCredentials: options.HttpCredentials,111 colorScheme: options.ColorScheme,112 reducedMotion: options.ReducedMotion,113 recordHarPath: options.RecordHarPath,114 recordHarOmitContent: options.RecordHarOmitContent,115 recordVideo: Browser.GetVideoArgs(options.RecordVideoDir, options.RecordVideoSize),116 ignoreDefaultArgs: options.IgnoreDefaultArgs,117 ignoreAllDefaultArgs: options.IgnoreAllDefaultArgs,118 baseUrl: options.BaseURL,119 forcedColors: options.ForcedColors).ConfigureAwait(false)).Object;120 // TODO: unite with a single browser context options type which is derived from channels121 context.Options = new()122 {123 RecordVideoDir = options.RecordVideoDir,124 RecordVideoSize = options.RecordVideoSize,125 RecordHarPath = options.RecordHarPath,126 RecordHarOmitContent = options.RecordHarOmitContent,127 };128 ((Core.Tracing)context.Tracing).LocalUtils = Playwright.Utils;129 return context;130 }131 public async Task<IBrowser> ConnectAsync(string wsEndpoint, BrowserTypeConnectOptions options = null)132 {133 options ??= new BrowserTypeConnectOptions();134 var headers = new List<KeyValuePair<string, string>>(options.Headers ?? new Dictionary<string, string>())135 {136 new KeyValuePair<string, string>("x-playwright-browser", Name),137 }.ToDictionary(pair => pair.Key, pair => pair.Value);138 JsonPipe pipe = (await _channel.ConnectAsync(wsEndpoint: wsEndpoint, headers: headers, slowMo: options.SlowMo, timeout: options.Timeout).ConfigureAwait(false)).Object;139 void ClosePipe()140 {141 pipe.CloseAsync().IgnoreException();142 }143#pragma warning disable CA2000 // Dispose objects before losing scope144 var connection = new Connection();145#pragma warning restore CA2000146 connection.MarkAsRemote();147 connection.Close += (_, _) => ClosePipe();148 string closeError = null;149 Browser browser = null;150 void OnPipeClosed()151 {152 // Emulate all pages, contexts and the browser closing upon disconnect.153 foreach (BrowserContext context in browser?.BrowserContextsList.ToArray() ?? Array.Empty<BrowserContext>())154 {155 foreach (Page page in context.PagesList.ToArray())156 {157 page.OnClose();158 }159 context.OnClose();160 }161 browser?.DidClose();162 connection.DoClose(closeError != null ? closeError : DriverMessages.BrowserClosedExceptionMessage);163 }164 pipe.Closed += (_, _) => OnPipeClosed();165 connection.OnMessage = async (object message) =>166 {167 try168 {169 await pipe.SendAsync(message).ConfigureAwait(false);170 }171 catch (Exception e) when (DriverMessages.IsSafeCloseError(e))172 {173 // swallow exception174 }175 catch176 {177 OnPipeClosed();178 }179 };180 pipe.Message += (_, message) =>181 {182 try183 {184 connection.Dispatch(message);185 }186 catch (Exception ex)187 {188 closeError = ex.ToString();189 _channel.Connection.TraceMessage("pw:dotnet", $"Dispatching error: {ex.Message}\n{ex.StackTrace}");190 ClosePipe();191 }192 };193 async Task<IBrowser> CreateBrowserAsync()194 {195 var playwright = await connection.InitializePlaywrightAsync().ConfigureAwait(false);196 playwright.Connection = connection;197 if (playwright.PreLaunchedBrowser == null)198 {199 ClosePipe();200 throw new ArgumentException("Malformed endpoint. Did you use launchServer method?");201 }202 browser = playwright.PreLaunchedBrowser;203 browser.ShouldCloseConnectionOnClose = true;204 browser.Disconnected += (_, _) => ClosePipe();205 browser.LocalUtils = Playwright.Utils;206 return playwright.PreLaunchedBrowser;207 }208 var task = CreateBrowserAsync();209 var timeout = options?.Timeout != null ? (int)options.Timeout : 30_000;210 return await task.WithTimeout(timeout, _ => throw new TimeoutException($"BrowserType.ConnectAsync: Timeout {options.Timeout}ms exceeded")).ConfigureAwait(false);211 }212 public async Task<IBrowser> ConnectOverCDPAsync(string endpointURL, BrowserTypeConnectOverCDPOptions options = null)213 {214 if (Name != "chromium")215 {216 throw new ArgumentException("Connecting over CDP is only supported in Chromium.");217 }218 options ??= new BrowserTypeConnectOverCDPOptions();219 JsonElement result = await _channel.ConnectOverCDPAsync(endpointURL, headers: options.Headers, slowMo: options.SlowMo, timeout: options.Timeout).ConfigureAwait(false);220 Browser browser = result.GetProperty("browser").ToObject<Browser>(_channel.Connection.DefaultJsonSerializerOptions);221 if (result.TryGetProperty("defaultContext", out JsonElement defaultContextValue))222 {223 browser.BrowserContextsList.Add(defaultContextValue.ToObject<BrowserContext>(_channel.Connection.DefaultJsonSerializerOptions));224 }225 browser.LocalUtils = Playwright.Utils;226 return browser;227 }228 }229}...

Full Screen

Full Screen

BrowserContextStorageStateTests.cs

Source:BrowserContextStorageStateTests.cs Github

copy

Full Screen

1/*2 * MIT License3 *4 * Copyright (c) Microsoft Corporation.5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24using System.IO;25using System.Text.Json;26using System.Threading.Tasks;27using Microsoft.Playwright.Core;28using NUnit.Framework;29namespace Microsoft.Playwright.Tests30{31 public sealed class BrowsercontextStorageStateTests : PageTestEx32 {33 [PlaywrightTest("browsercontext-storage-state.spec.ts", "should capture local storage")]34 public async Task ShouldCaptureLocalStorage()35 {36 var page1 = await Context.NewPageAsync();37 await page1.RouteAsync("**/*", (route) =>38 {39 route.FulfillAsync(new() { Body = "<html></html>" });40 });41 await page1.GotoAsync("https://www.example.com");42 await page1.EvaluateAsync(@"() =>43 {44 localStorage['name1'] = 'value1';45 }");46 await page1.GotoAsync("https://www.domain.com");47 await page1.EvaluateAsync(@"() =>48 {49 localStorage['name2'] = 'value2';50 }");51 string storage = await Context.StorageStateAsync();52 // TODO: think about IVT-in the StorageState and serializing53 string expected = @"{""cookies"":[],""origins"":[{""origin"":""https://www.example.com"",""localStorage"":[{""name"":""name1"",""value"":""value1""}]},{""origin"":""https://www.domain.com"",""localStorage"":[{""name"":""name2"",""value"":""value2""}]}]}";54 Assert.AreEqual(expected, storage);55 }56 [PlaywrightTest("browsercontext-storage-state.spec.ts", "should set local storage")]57 public async Task ShouldSetLocalStorage()58 {59 var context = await Browser.NewContextAsync(new()60 {61 StorageState = "{\"cookies\":[],\"origins\":[{\"origin\":\"https://www.example.com\",\"localStorage\":[{\"name\":\"name1\",\"value\":\"value1\"}]}]}",62 });63 var page = await context.NewPageAsync();64 await page.RouteAsync("**/*", (route) =>65 {66 route.FulfillAsync(new() { Body = "<html></html>" });67 });68 await page.GotoAsync("https://www.example.com");69 var localStorage = await page.EvaluateAsync<string[]>("Object.keys(window.localStorage)");70 Assert.AreEqual(localStorage, new string[] { "name1" });71 var name1Value = await page.EvaluateAsync<string>("window.localStorage.getItem('name1')");72 Assert.AreEqual(name1Value, "value1");73 }74 [PlaywrightTest("browsercontext-storage-state.spec.ts", "should round-trip through the file")]75 public async Task ShouldRoundTripThroughTheFile()76 {77 var page1 = await Context.NewPageAsync();78 await page1.RouteAsync("**/*", (route) =>79 {80 route.FulfillAsync(new() { Body = "<html></html>" });81 });82 await page1.GotoAsync("https://www.example.com");83 await page1.EvaluateAsync(@"() =>84 {85 localStorage['name1'] = 'value1';86 document.cookie = 'username=John Doe';87 }");88 using var tempDir = new TempDirectory();89 string path = Path.Combine(tempDir.Path, "storage-state.json");90 string storage = await Context.StorageStateAsync(new() { Path = path });91 Assert.AreEqual(storage, File.ReadAllText(path));92 await using var context = await Browser.NewContextAsync(new() { StorageStatePath = path });93 var page2 = await context.NewPageAsync();94 await page2.RouteAsync("**/*", (route) =>95 {96 route.FulfillAsync(new() { Body = "<html></html>" });97 });98 await page2.GotoAsync("https://www.example.com");99 Assert.AreEqual("value1", await page2.EvaluateAsync<string>("localStorage['name1']"));100 Assert.AreEqual("username=John Doe", await page2.EvaluateAsync<string>("document.cookie"));101 }102 [PlaywrightTest("browsercontext-storage-state.spec.ts", "should capture cookies")]103 public async Task ShouldCaptureCookies()104 {105 Server.SetRoute("/setcookie.html", context =>106 {107 context.Response.Cookies.Append("a", "b");108 context.Response.Cookies.Append("empty", "");109 return Task.CompletedTask;110 });111 await Page.GotoAsync(Server.Prefix + "/setcookie.html");112 CollectionAssert.AreEqual(new[] { "a=b", "empty=" }, await Page.EvaluateAsync<string[]>(@"() =>113 {114 const cookies = document.cookie.split(';');115 return cookies.map(cookie => cookie.trim()).sort();116 }"));117 var storageState = await Context.StorageStateAsync();118 StringAssert.Contains(@"""name"":""a"",""value"":""b""", storageState);119 StringAssert.Contains(@"""name"":""empty"",""value"":""""", storageState);120 if (TestConstants.IsWebKit || TestConstants.IsFirefox)121 {122 StringAssert.Contains(@"""sameSite"":""None""", storageState);123 }124 else125 {126 StringAssert.Contains(@"""sameSite"":""Lax""", storageState);127 }128 StringAssert.DoesNotContain(@"""url"":null", storageState);129 await using var context2 = await Browser.NewContextAsync(new() { StorageState = storageState });130 var page2 = await context2.NewPageAsync();131 await page2.GotoAsync(Server.EmptyPage);132 CollectionAssert.AreEqual(new[] { "a=b", "empty=" }, await page2.EvaluateAsync<string[]>(@"() =>133 {134 const cookies = document.cookie.split(';');135 return cookies.map(cookie => cookie.trim()).sort();136 }"));137 }138 }139}...

Full Screen

Full Screen

Worker.cs

Source:Worker.cs Github

copy

Full Screen

...42 if (Page != null)43 {44 Page.WorkersList.Remove(this);45 }46 if (BrowserContext != null)47 {48 BrowserContext.ServiceWorkersList.Remove(this);49 }50 Close?.Invoke(this, this);51 };52 }53 public event EventHandler<IWorker> Close;54 public string Url => _initializer.Url;55 ChannelBase IChannelOwner.Channel => _channel;56 IChannel<Worker> IChannelOwner<Worker>.Channel => _channel;57 internal Page Page { get; set; }58 internal BrowserContext BrowserContext { get; set; }59 public async Task<T> EvaluateAsync<T>(string expression, object arg = null)60 => ScriptsHelper.ParseEvaluateResult<T>(await _channel.EvaluateExpressionAsync(61 expression,62 null,63 ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));64 public async Task<IJSHandle> EvaluateHandleAsync(string expression, object arg = null)65 => await _channel.EvaluateExpressionHandleAsync(66 expression,67 null,68 ScriptsHelper.SerializedArgument(arg))69 .ConfigureAwait(false);70 public async Task<IWorker> WaitForCloseAsync(Func<Task> action = default, float? timeout = default)71 {72 using var waiter = new Waiter(this, "worker.WaitForCloseAsync");...

Full Screen

Full Screen

PlaywrightDriverSetup.cs

Source:PlaywrightDriverSetup.cs Github

copy

Full Screen

...31 case BrowserType.Webkit:32 browser = await playwright.Webkit.LaunchAsync(browserTypeLaunchOptions);33 break;34 }35 IBrowserContext browserContext = await browser.NewContextAsync(new BrowserNewContextOptions36 {37 IgnoreHTTPSErrors = true,38 ViewportSize = ViewportSize.NoViewport39 });40 IPage page = await browserContext.NewPageAsync();41 _playwrightDriver = new PlaywrightDriver()42 {43 Playwright = playwright,44 Browser = browser,45 BrowserContext = browserContext,46 Page = page47 };48 }49 catch (Exception e)50 {51 Console.WriteLine(e);52 throw;53 }54 return _playwrightDriver;55 }56 }57}...

Full Screen

Full Screen

PlaywrightDriver.cs

Source:PlaywrightDriver.cs Github

copy

Full Screen

...4 public class PlaywrightDriver5 {6 public IPlaywright Playwright { get; set; }7 public IBrowser Browser { get; set; }8 public IBrowserContext BrowserContext { get; set; }9 public IPage Page { get; set; }10 }11}...

Full Screen

Full Screen

BrowserContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright.Core;2using System;3{4 {5 static void Main(string[] args)6 {7 Console.WriteLine("Hello World!");8 Playwright.InstallAsync().Wait();9 using (var playwright = Playwright.CreateAsync().Result)10 {11 using (var browser = playwright.Chromium.LaunchAsync().Result)12 {13 using (var context = browser.NewContextAsync().Result)14 {15 using (var page = context.NewPageAsync().Result)16 {17 Console.WriteLine(page.TitleAsync().Result);18 }19 }20 }21 }22 }23 }24}25using Microsoft.Playwright;26using System;27{28 {29 static void Main(string[] args)30 {31 Console.WriteLine("Hello World!");32 Playwright.InstallAsync().Wait();33 using (var playwright = Playwright.CreateAsync().Result)34 {35 using (var browser = playwright.Chromium.LaunchAsync().Result)36 {37 using (var context = browser.NewContextAsync().Result)38 {39 using (var page = context.NewPageAsync().Result)40 {41 Console.WriteLine(page.TitleAsync().Result);42 }43 }44 }45 }46 }47 }48}49using Microsoft.Playwright;50using System;51{52 {53 static void Main(string[] args)54 {55 Console.WriteLine("Hello World!");56 Playwright.InstallAsync().Wait();57 using (var playwright = Playwright.CreateAsync().Result)58 {59 using (var browser = playwright.Chromium.LaunchAsync().Result)60 {61 using (var context = browser.NewContextAsync().Result)62 {63 using (var page = context.NewPageAsync().Result)64 {65 Console.WriteLine(page.TitleAsync().Result);66 }67 }68 }69 }70 }71 }72}73using Microsoft.Playwright;

Full Screen

Full Screen

BrowserContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using Microsoft.Playwright.Helpers;3using Microsoft.Playwright.Input;4using Microsoft.Playwright.Network;5using Microsoft.Playwright.Playwright;6{7 {8 static async Task Main(string[] args)9 {10 using var playwright = await Playwright.CreateAsync();11 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });12 var context = await browser.NewContextAsync();13 var page = await context.NewPageAsync();14 }15 }16}17using Microsoft.Playwright;18using Microsoft.Playwright.Helpers;19using Microsoft.Playwright.Input;20using Microsoft.Playwright.Network;21using Microsoft.Playwright.Playwright;22{23 {24 static async Task Main(string[] args)25 {26 using var playwright = await Playwright.CreateAsync();27 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });28 var context = await browser.NewContextAsync();29 var page = await context.NewPageAsync();30 }31 }32}33using Microsoft.Playwright;34using Microsoft.Playwright.Helpers;35using Microsoft.Playwright.Input;

Full Screen

Full Screen

BrowserContext

Using AI Code Generation

copy

Full Screen

1var context = await browser.NewContextAsync(new Browser.NewContextOptions {2 RecordVideo = new Browser.NewContextOptions.VideoSize {3 Size = new Browser.NewContextOptions.VideoSize.SizeUnion {4 },5 },6 RecordHar = new Browser.NewContextOptions.RecordHar {7 },8 RecordTrace = new Browser.NewContextOptions.RecordTrace {9 },10 StorageState = new Browser.NewContextOptions.StorageState {11 },12 Viewport = new Browser.NewContextOptions.ViewportUnion {13 },14 Geolocation = new Browser.NewContextOptions.Geolocation {15 },16 Permissions = new[] {17 },18 ExtraHTTPHeaders = new Dictionary<string, string> {19 },20 HttpCredentials = new Browser.NewContextOptions.HttpCredentials {21 },22 UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0",23 Proxy = new Browser.NewContextOptions.Proxy {24 Bypass = new[] {

Full Screen

Full Screen

Playwright tutorial

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.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful