How to use ExposeBindingAsync method of Microsoft.Playwright.Core.BrowserContext class

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

Page.cs

Source:Page.cs Github

copy

Full Screen

...533 public async Task<IResponse> GoForwardAsync(PageGoForwardOptions options = default)534 => (await _channel.GoForwardAsync(options?.Timeout, options?.WaitUntil).ConfigureAwait(false))?.Object;535 public async Task<IResponse> ReloadAsync(PageReloadOptions options = default)536 => (await _channel.ReloadAsync(options?.Timeout, options?.WaitUntil).ConfigureAwait(false))?.Object;537 public Task ExposeBindingAsync(string name, Action callback, PageExposeBindingOptions options = default)538 => InnerExposeBindingAsync(name, (Delegate)callback, options?.Handle ?? false);539 public Task ExposeBindingAsync(string name, Action<BindingSource> callback)540 => InnerExposeBindingAsync(name, (Delegate)callback);541 public Task ExposeBindingAsync<T>(string name, Action<BindingSource, T> callback)542 => InnerExposeBindingAsync(name, (Delegate)callback);543 public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, TResult> callback)544 => InnerExposeBindingAsync(name, (Delegate)callback);545 public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, IJSHandle, TResult> callback)546 => InnerExposeBindingAsync(name, (Delegate)callback, true);547 public Task ExposeBindingAsync<T, TResult>(string name, Func<BindingSource, T, TResult> callback)548 => InnerExposeBindingAsync(name, (Delegate)callback);549 public Task ExposeBindingAsync<T1, T2, TResult>(string name, Func<BindingSource, T1, T2, TResult> callback)550 => InnerExposeBindingAsync(name, (Delegate)callback);551 public Task ExposeBindingAsync<T1, T2, T3, TResult>(string name, Func<BindingSource, T1, T2, T3, TResult> callback)552 => InnerExposeBindingAsync(name, (Delegate)callback);553 public Task ExposeBindingAsync<T1, T2, T3, T4, TResult>(string name, Func<BindingSource, T1, T2, T3, T4, TResult> callback)554 => InnerExposeBindingAsync(name, (Delegate)callback);555 public Task ExposeFunctionAsync(string name, Action callback)556 => ExposeBindingAsync(name, (BindingSource _) => callback());557 public Task ExposeFunctionAsync<T>(string name, Action<T> callback)558 => ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));559 public Task ExposeFunctionAsync<TResult>(string name, Func<TResult> callback)560 => ExposeBindingAsync(name, (BindingSource _) => callback());561 public Task ExposeFunctionAsync<T, TResult>(string name, Func<T, TResult> callback)562 => ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));563 public Task ExposeFunctionAsync<T1, T2, TResult>(string name, Func<T1, T2, TResult> callback)564 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2) => callback(t1, t2));565 public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> callback)566 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3) => callback(t1, t2, t3));567 public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> callback)568 => ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3, T4 t4) => callback(t1, t2, t3, t4));569 public async Task<byte[]> PdfAsync(PagePdfOptions options = default)570 {571 if (!Context.IsChromium)572 {573 throw new NotSupportedException("This browser doesn't support this action.");574 }575 byte[] result = await _channel.PdfAsync(576 scale: options?.Scale,577 displayHeaderFooter: options?.DisplayHeaderFooter,578 headerTemplate: options?.HeaderTemplate,579 footerTemplate: options?.FooterTemplate,580 printBackground: options?.PrintBackground,581 landscape: options?.Landscape,582 pageRanges: options?.PageRanges,583 format: options?.Format,584 width: options?.Width,585 height: options?.Height,586 margin: options?.Margin,587 preferCSSPageSize: options?.PreferCSSPageSize).ConfigureAwait(false);588 if (!string.IsNullOrEmpty(options?.Path))589 {590 Directory.CreateDirectory(new FileInfo(options.Path).Directory.FullName);591 File.WriteAllBytes(options.Path, result);592 }593 return result;594 }595 public Task AddInitScriptAsync(string script, string scriptPath)596 => _channel.AddInitScriptAsync(ScriptsHelper.EvaluationScript(script, scriptPath));597 public Task RouteAsync(string url, Action<IRoute> handler, PageRouteOptions options = null)598 => RouteAsync(new Regex(Context.CombineUrlWithBase(url).GlobToRegex()), null, handler, options);599 public Task RouteAsync(Regex url, Action<IRoute> handler, PageRouteOptions options = null)600 => RouteAsync(url, null, handler, options);601 public Task RouteAsync(Func<string, bool> url, Action<IRoute> handler, PageRouteOptions options = null)602 => RouteAsync(null, url, handler, options);603 public Task UnrouteAsync(string urlString, Action<IRoute> handler)604 => UnrouteAsync(new Regex(Context.CombineUrlWithBase(urlString).GlobToRegex()), null, handler);605 public Task UnrouteAsync(Regex urlString, Action<IRoute> handler)606 => UnrouteAsync(urlString, null, handler);607 public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler)608 => UnrouteAsync(null, urlFunc, handler);609 public Task WaitForLoadStateAsync(LoadState? state = default, PageWaitForLoadStateOptions options = default)610 => MainFrame.WaitForLoadStateAsync(state, new() { Timeout = options?.Timeout });611 public Task SetViewportSizeAsync(int width, int height)612 {613 ViewportSize = new() { Width = width, Height = height };614 return _channel.SetViewportSizeAsync(ViewportSize);615 }616 public Task SetCheckedAsync(string selector, bool checkedState, PageSetCheckedOptions options = null)617 => checkedState ?618 MainFrame.CheckAsync(selector, new()619 {620 Position = options?.Position,621 Force = options?.Force,622 NoWaitAfter = options?.NoWaitAfter,623 Strict = options?.Strict,624 Timeout = options?.Timeout,625 Trial = options?.Trial,626 })627 : MainFrame.UncheckAsync(selector, new()628 {629 Position = options?.Position,630 Force = options?.Force,631 NoWaitAfter = options?.NoWaitAfter,632 Timeout = options?.Timeout,633 Trial = options?.Trial,634 Strict = options?.Strict,635 });636 public Task CheckAsync(string selector, PageCheckOptions options = default)637 => MainFrame.CheckAsync(selector, new()638 {639 Position = options?.Position,640 Force = options?.Force,641 NoWaitAfter = options?.NoWaitAfter,642 Strict = options?.Strict,643 Timeout = options?.Timeout,644 Trial = options?.Trial,645 });646 public Task UncheckAsync(string selector, PageUncheckOptions options = default)647 => MainFrame.UncheckAsync(selector, new()648 {649 Position = options?.Position,650 Force = options?.Force,651 NoWaitAfter = options?.NoWaitAfter,652 Timeout = options?.Timeout,653 Trial = options?.Trial,654 Strict = options?.Strict,655 });656 public Task DispatchEventAsync(string selector, string type, object eventInit = default, PageDispatchEventOptions options = default)657 => MainFrame.DispatchEventAsync(selector, type, eventInit, new() { Timeout = options?.Timeout, Strict = options?.Strict });658 public Task<string> GetAttributeAsync(string selector, string name, PageGetAttributeOptions options = default)659 => MainFrame.GetAttributeAsync(selector, name, new()660 {661 Timeout = options?.Timeout,662 Strict = options?.Strict,663 });664 public Task<string> InnerHTMLAsync(string selector, PageInnerHTMLOptions options = default)665 => MainFrame.InnerHTMLAsync(selector, new()666 {667 Timeout = options?.Timeout,668 Strict = options?.Strict,669 });670 public Task<string> InnerTextAsync(string selector, PageInnerTextOptions options = default)671 => MainFrame.InnerTextAsync(selector, new()672 {673 Timeout = options?.Timeout,674 Strict = options?.Strict,675 });676 public Task<string> TextContentAsync(string selector, PageTextContentOptions options = default)677 => MainFrame.TextContentAsync(selector, new()678 {679 Timeout = options?.Timeout,680 Strict = options?.Strict,681 });682 public Task TapAsync(string selector, PageTapOptions options = default)683 => MainFrame.TapAsync(684 selector,685 new()686 {687 Modifiers = options?.Modifiers,688 Position = options?.Position,689 Force = options?.Force,690 NoWaitAfter = options?.NoWaitAfter,691 Timeout = options?.Timeout,692 Trial = options?.Trial,693 Strict = options?.Strict,694 });695 public Task<bool> IsCheckedAsync(string selector, PageIsCheckedOptions options = default)696 => MainFrame.IsCheckedAsync(selector, new()697 {698 Timeout = options?.Timeout,699 Strict = options?.Strict,700 });701 public Task<bool> IsDisabledAsync(string selector, PageIsDisabledOptions options = default)702 => MainFrame.IsDisabledAsync(selector, new()703 {704 Timeout = options?.Timeout,705 Strict = options?.Strict,706 });707 public Task<bool> IsEditableAsync(string selector, PageIsEditableOptions options = default)708 => MainFrame.IsEditableAsync(selector, new()709 {710 Timeout = options?.Timeout,711 Strict = options?.Strict,712 });713 public Task<bool> IsEnabledAsync(string selector, PageIsEnabledOptions options = default)714 => MainFrame.IsEnabledAsync(selector, new()715 {716 Timeout = options?.Timeout,717 Strict = options?.Strict,718 });719#pragma warning disable CS0612 // Type or member is obsolete720 public Task<bool> IsHiddenAsync(string selector, PageIsHiddenOptions options = default)721 => MainFrame.IsHiddenAsync(selector, new()722 {723 Timeout = options?.Timeout,724 Strict = options?.Strict,725 });726 public Task<bool> IsVisibleAsync(string selector, PageIsVisibleOptions options = default)727 => MainFrame.IsVisibleAsync(selector, new()728 {729 Timeout = options?.Timeout,730 Strict = options?.Strict,731 });732#pragma warning restore CS0612 // Type or member is obsolete733 public Task PauseAsync() => Context.Channel.PauseAsync();734 public void SetDefaultNavigationTimeout(float timeout) => DefaultNavigationTimeout = timeout;735 public void SetDefaultTimeout(float timeout) => DefaultTimeout = timeout;736 public Task<string> InputValueAsync(string selector, PageInputValueOptions options = null)737 => MainFrame.InputValueAsync(selector, new()738 {739 Timeout = options?.Timeout,740 Strict = options?.Strict,741 });742 public Task DragAndDropAsync(string source, string target, PageDragAndDropOptions options = null)743 => MainFrame.DragAndDropAsync(source, target, new()744 {745 Force = options?.Force,746 NoWaitAfter = options?.NoWaitAfter,747 Timeout = options?.Timeout,748 Trial = options?.Trial,749 Strict = options?.Strict,750 });751 internal void NotifyPopup(Page page) => Popup?.Invoke(this, page);752 internal void OnFrameNavigated(Frame frame)753 => FrameNavigated?.Invoke(this, frame);754 internal void FireRequest(IRequest request) => Request?.Invoke(this, request);755 internal void FireRequestFailed(IRequest request) => RequestFailed?.Invoke(this, request);756 internal void FireRequestFinished(IRequest request) => RequestFinished?.Invoke(this, request);757 internal void FireResponse(IResponse response) => Response?.Invoke(this, response);758 private Task RouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Action<IRoute> handler, PageRouteOptions options)759 => RouteAsync(new()760 {761 Regex = urlRegex,762 Function = urlFunc,763 Handler = handler,764 Times = options?.Times,765 });766 private Task RouteAsync(RouteSetting setting)767 {768 _routes.Insert(0, setting);769 if (_routes.Count == 1)770 {771 return _channel.SetNetworkInterceptionEnabledAsync(true);772 }773 return Task.CompletedTask;774 }775 private Task UnrouteAsync(Regex urlRegex, Func<string, bool> urlFunc, Action<IRoute> handler = null)776 => UnrouteAsync(new()777 {778 Function = urlFunc,779 Regex = urlRegex,780 Handler = handler,781 });782 private Task UnrouteAsync(RouteSetting setting)783 {784 var newRoutesList = new List<RouteSetting>();785 newRoutesList.AddRange(_routes.Where(r =>786 (setting.Regex != null && !(r.Regex == setting.Regex || (r.Regex.ToString() == setting.Regex.ToString() && r.Regex.Options == setting.Regex.Options))) ||787 (setting.Function != null && r.Function != setting.Function) ||788 (setting.Handler != null && r.Handler != setting.Handler)));789 _routes = newRoutesList;790 if (_routes.Count == 0)791 {792 return DisableInterceptionAsync();793 }794 return Task.CompletedTask;795 }796 internal void OnClose()797 {798 IsClosed = true;799 Context?.PagesList.Remove(this);800 RejectPendingOperations(false);801 Close?.Invoke(this, this);802 }803 private void Channel_Crashed(object sender, EventArgs e)804 {805 RejectPendingOperations(true);806 Crash?.Invoke(this, this);807 }808 private void Channel_BindingCall(object sender, BindingCallEventArgs e)809 {810 if (Bindings.TryGetValue(e.BindingCall.Name, out var binding))811 {812 _ = e.BindingCall.CallAsync(binding);813 }814 }815 private void OnRoute(Route route, IRequest request)816 {817 foreach (var routeHandler in _routes.ToList())818 {819 if ((routeHandler.Regex?.IsMatch(request.Url) == true) ||820 (routeHandler.Function?.Invoke(request.Url) == true))821 {822 try823 {824 routeHandler.Handle(route);825 }826 finally827 {828 if (!routeHandler.IsActive())829 {830 _routes.Remove(routeHandler);831 if (_routes.Count == 0)832 {833 DisableInterceptionAsync().ConfigureAwait(false);834 }835 }836 }837 return;838 }839 }840 Context.OnRoute(route, request);841 }842 internal async Task DisableInterceptionAsync()843 {844 await Channel.SetNetworkInterceptionEnabledAsync(false).ConfigureAwait(false);845 }846 private void Channel_FrameDetached(object sender, IFrame args)847 {848 var frame = (Frame)args;849 _frames.Remove(frame);850 frame.IsDetached = true;851 frame.ParentFrame?.ChildFramesList?.Remove(frame);852 FrameDetached?.Invoke(this, args);853 }854 private void Channel_FrameAttached(object sender, IFrame args)855 {856 var frame = (Frame)args;857 frame.Page = this;858 _frames.Add(frame);859 frame.ParentFrame?.ChildFramesList?.Add(frame);860 FrameAttached?.Invoke(this, args);861 }862 private void RejectPendingOperations(bool isCrash)863 {864 foreach (var (_, waitTcs) in _waitForCancellationTcs.Where(e => e.PageEvent != (isCrash ? PageEvent.Crash : PageEvent.Close)))865 {866 waitTcs.TrySetException(new PlaywrightException(isCrash ? "Page crashed" : "Page closed"));867 }868 _waitForCancellationTcs.Clear();869 }870 private Task InnerExposeBindingAsync(string name, Delegate callback, bool handle = false)871 {872 if (Bindings.ContainsKey(name))873 {874 throw new PlaywrightException($"Function \"{name}\" has been already registered");875 }876 Bindings.Add(name, callback);877 return _channel.ExposeBindingAsync(name, handle);878 }879 private Video ForceVideo() => _video ??= new(this, _channel.Connection);880 private FrameSetInputFilesOptions Map(PageSetInputFilesOptions options)881 {882 if (options == null)883 {884 return null;885 }886 return new()887 {888 NoWaitAfter = options.NoWaitAfter,889 Timeout = options.Timeout,890 Strict = options.Strict,891 };...

Full Screen

Full Screen

BrowserContext.cs

Source:BrowserContext.cs Github

copy

Full Screen

...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 {362 _ = e.BindingCall.CallAsync(binding);363 }364 }365 private void Channel_Route(object sender, RouteEventArgs e) => OnRoute(e.Route, e.Request);366 private Task ExposeBindingAsync(string name, Delegate callback, bool handle = false)367 {368 foreach (var page in PagesList)369 {370 if (page.Bindings.ContainsKey(name))371 {372 throw new PlaywrightException($"Function \"{name}\" has been already registered in one of the pages");373 }374 }375 if (_bindings.ContainsKey(name))376 {377 throw new PlaywrightException($"Function \"{name}\" has been already registered");378 }379 _bindings.Add(name, callback);380 return Channel.ExposeBindingAsync(name, handle);381 }382 }383}...

Full Screen

Full Screen

BrowserContextChannel.cs

Source:BrowserContextChannel.cs Github

copy

Full Screen

...117 new Dictionary<string, object>118 {119 ["timeout"] = timeout,120 });121 internal Task ExposeBindingAsync(string name, bool needsHandle)122 => Connection.SendMessageToServerAsync<PageChannel>(123 Guid,124 "exposeBinding",125 new Dictionary<string, object>126 {127 ["name"] = name,128 ["needsHandle"] = needsHandle,129 });130 internal Task AddInitScriptAsync(string script)131 => Connection.SendMessageToServerAsync<PageChannel>(132 Guid,133 "addInitScript",134 new Dictionary<string, object>135 {...

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 await using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync();10 var page = await browser.NewPageAsync();11 await page.ExposeBindingAsync("myBinding", (BindingSource source, object[] args) =>12 {13 return Task.FromResult("Hello from myBinding!");14 });15 var result = await page.EvaluateAsync<string>("async () => { ret

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync();10 var context = await browser.NewContextAsync();11 await context.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>12 {13 return Task.FromResult("Hello from Playwright");14 });15 var page = await context.NewPageAsync();16 var result = await page.EvaluateAsync<string>("() => myBinding()");17 Console.WriteLine(result);18 }19 }20}21using Microsoft.Playwright;22using System;23using System.Threading.Tasks;24{25 {26 static async Task Main(string[] args)27 {28 using var playwright = await Playwright.CreateAsync();29 await using var browser = await playwright.Chromium.LaunchAsync();30 var context = await browser.NewContextAsync();31 var page = await context.NewPageAsync();32 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>33 {34 return Task.FromResult("Hello from Playwright");35 });36 var result = await page.EvaluateAsync<string>("() => myBinding()");37 Console.WriteLine(result);38 }39 }40}41using Microsoft.Playwright;42using System;43using System.Threading.Tasks;44{45 {46 static async Task Main(string[] args)47 {48 using var playwright = await Playwright.CreateAsync();49 await using var browser = await playwright.Chromium.LaunchAsync();50 var context = await browser.NewContextAsync();51 var page = await context.NewPageAsync();52 var frame = page.MainFrame;53 await frame.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>54 {55 return Task.FromResult("Hello from Playwright");56 });

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1var context = await browser.NewContextAsync(new BrowserNewContextOptions2{3});4var page = await context.NewPageAsync();5await page.ClickAsync("text=Download");6var download = await context.WaitForEventAsync<Download>(BrowserContextEvent.Download);7var content = await download.PathAsync();8Console.WriteLine(await File.ReadAllTextAsync(content));9await download.DeleteAsync();10await context.CloseAsync();11await browser.CloseAsync();12var page = await browser.NewPageAsync();13await page.ExposeBindingAsync("download", async (BindingSource _, string url) =>14{15 var download = await page.Context.WaitForEventAsync<Download>(BrowserContextEvent.Download);16 await download.PathAsync();17 await download.DeleteAsync();18});19await page.ClickAsync("text=Download");20await browser.CloseAsync();21var page = await browser.NewPageAsync();22var frame = page.MainFrame;23await frame.ExposeBindingAsync("download", async (BindingSource _, string url) =>24{25 var download = await frame.Context.WaitForEventAsync<Download>(BrowserContextEvent.Download);26 await download.PathAsync();27 await download.DeleteAsync();28});29await page.ClickAsync("text=Download");30await browser.CloseAsync();31var page = await browser.NewPageAsync();32await page.ExposeBindingAsync("download", async (BindingSource _, string url) =>33{34 var download = await page.Context.WaitForEventAsync<Download>(BrowserContextEvent.Download);35 await download.PathAsync();36 await download.DeleteAsync();37});38await page.ClickAsync("text=Download");39await browser.CloseAsync();40var page = await browser.NewPageAsync();

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var context = await browser.NewContextAsync(new BrowserNewContextOptions13 {14 });15 var page = await context.NewPageAsync();16 await context.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>17 {18 Console.WriteLine("myBinding was called with: " + arg);19 });20 await page.EvaluateAsync(@"() => {21 window.myBinding('hello from page');22 }");23 }24 }25}

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 await using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var context = await browser.NewContextAsync(new BrowserNewContextOptions13 {14 ExposeBinding = new Func<string, BindingSource, Task>(async (name, source) =>15 {16 if (source == BindingSource.Page)17 {18 await context.ExposeBindingAsync(name, (BindingCallback)((

Full Screen

Full Screen

ExposeBindingAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>14 {15 Console.WriteLine("myBinding called with " + arg);16 });17 await page.EvaluateAsync("() => window.myBinding('test')");18 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>19 {20 Console.WriteLine("myBinding called with " + arg);21 });22 await page.EvaluateAsync("() => window.myBinding('test')");23 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>24 {25 Console.WriteLine("myBinding called with " + arg);26 });27 await page.EvaluateAsync("() => window.myBinding('test')");28 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>29 {30 Console.WriteLine("myBinding called with " + arg);31 });32 await page.EvaluateAsync("() => window.myBinding('test')");33 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>34 {35 Console.WriteLine("myBinding called with " + arg);36 });37 await page.EvaluateAsync("() => window.myBinding('test')");38 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>39 {40 Console.WriteLine("myBinding called with " + arg);41 });42 await page.EvaluateAsync("() => window.myBinding('test')");43 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>44 {45 Console.WriteLine("myBinding called with " + arg);46 });47 await page.EvaluateAsync("() => window.myBinding('test')");48 await page.ExposeBindingAsync("myBinding", (BindingSource source, object arg) =>49 {

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