From 39c61c51ff268e82773f84ee41763d90bf866f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Fri, 4 Sep 2020 22:00:54 -0300 Subject: [PATCH] Options refactor (#806) * Options refactor related to #641 * dotnet format * ops --- .../BrowserContextBypassCSPTests.cs | 8 +- .../BrowserContextJavaScriptEnabledTests.cs | 4 +- .../LaunchPersistentContextTests.cs | 2 +- .../DownloadsPathLaunchPersistentTests.cs | 2 +- .../Emulation/BrowserContextLocaleTests.cs | 5 +- .../Page/DownloadTests.cs | 2 +- src/PlaywrightSharp.Tests/TestConstants.cs | 3 +- src/PlaywrightSharp/Browser.cs | 154 ++++++- src/PlaywrightSharp/BrowserContextOptions.cs | 266 +++++------ src/PlaywrightSharp/BrowserType.cs | 218 ++++++++- src/PlaywrightSharp/IBrowser.cs | 192 +++++++- src/PlaywrightSharp/IBrowserContext.cs | 2 +- src/PlaywrightSharp/IBrowserType.cs | 220 ++++++++- src/PlaywrightSharp/LaunchOptions.cs | 274 +++++------- .../LaunchPersistentOptions.cs | 421 ++++++------------ src/PlaywrightSharp/PlaywrightSharp.csproj | 5 + .../Transport/Channels/BrowserChannel.cs | 2 +- .../Transport/Channels/BrowserTypeChannel.cs | 8 +- src/PlaywrightSharp/ViewportSize.cs | 5 + 19 files changed, 1147 insertions(+), 646 deletions(-) diff --git a/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextBypassCSPTests.cs b/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextBypassCSPTests.cs index 26c71e94de..1f91ef5389 100644 --- a/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextBypassCSPTests.cs +++ b/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextBypassCSPTests.cs @@ -31,7 +31,7 @@ public async Task ShouldBypassCSPMetatag() Assert.Null(await page.EvaluateAsync("window.__injected")); } // By-pass CSP and try one more time. - await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true })) + await using (var context = await Browser.NewContextAsync(bypassCSP: true)) { var page = await context.NewPageAsync(); await page.GoToAsync(TestConstants.ServerUrl + "/csp.html"); @@ -58,7 +58,7 @@ public async Task ShouldBypassCSPHeader() } // By-pass CSP and try one more time. - await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true })) + await using (var context = await Browser.NewContextAsync(bypassCSP: true)) { var page = await context.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); @@ -73,7 +73,7 @@ public async Task ShouldBypassCSPHeader() [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)] public async Task ShouldBypassAfterCrossProcessNavigation() { - await using var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true }); + await using var context = await Browser.NewContextAsync(bypassCSP: true); var page = await context.NewPageAsync(); await page.GoToAsync(TestConstants.ServerUrl + "/csp.html"); await page.AddScriptTagAsync(content: "window.__injected = 42;"); @@ -102,7 +102,7 @@ public async Task ShouldBypassCSPInIframesAsWell() } // By-pass CSP and try one more time. - await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true })) + await using (var context = await Browser.NewContextAsync(bypassCSP: true)) { var page = await context.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); diff --git a/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextJavaScriptEnabledTests.cs b/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextJavaScriptEnabledTests.cs index 94ee3e934e..b14534cf47 100644 --- a/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextJavaScriptEnabledTests.cs +++ b/src/PlaywrightSharp.Tests/BrowserContext/BrowserContextJavaScriptEnabledTests.cs @@ -23,7 +23,7 @@ public BrowserContextJavaScriptEnabledTests(ITestOutputHelper output) : base(out [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)] public async Task ShouldWork() { - await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { JavaScriptEnabled = false })) + await using (var context = await Browser.NewContextAsync(javaScriptEnabled: false)) { var page = await context.NewPageAsync(); await page.GoToAsync("data:text/html, "); @@ -49,7 +49,7 @@ public async Task ShouldWork() [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)] public async Task ShouldBeAbleToNavigateAfterDisablingJavascript() { - await using var context = await Browser.NewContextAsync(new BrowserContextOptions { JavaScriptEnabled = false }); + await using var context = await Browser.NewContextAsync(javaScriptEnabled: false); var page = await context.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); } diff --git a/src/PlaywrightSharp.Tests/BrowserContext/LaunchPersistentContextTests.cs b/src/PlaywrightSharp.Tests/BrowserContext/LaunchPersistentContextTests.cs index 4e8e9832db..dccbc67dff 100644 --- a/src/PlaywrightSharp.Tests/BrowserContext/LaunchPersistentContextTests.cs +++ b/src/PlaywrightSharp.Tests/BrowserContext/LaunchPersistentContextTests.cs @@ -648,7 +648,7 @@ public async Task ShouldFireCloseEventForAPersistentContext() var tmp = new TempDirectory(); var context = await BrowserType.LaunchPersistenContextAsync( tmp.Path, - (LaunchPersistentOptions)TestConstants.GetDefaultBrowserOptions() + (options ?? new BrowserContextOptions())); + TestConstants.GetDefaultBrowserOptions().ToPersistentOptions() + (options ?? new BrowserContextOptions())); var page = context.Pages.First(); return (tmp, context, page); diff --git a/src/PlaywrightSharp.Tests/DownloadsPath/DownloadsPathLaunchPersistentTests.cs b/src/PlaywrightSharp.Tests/DownloadsPath/DownloadsPathLaunchPersistentTests.cs index f5f8d82e0b..23a9d74a41 100644 --- a/src/PlaywrightSharp.Tests/DownloadsPath/DownloadsPathLaunchPersistentTests.cs +++ b/src/PlaywrightSharp.Tests/DownloadsPath/DownloadsPathLaunchPersistentTests.cs @@ -75,7 +75,7 @@ public async Task InitializeAsync() _downloadsPath = new TempDirectory(); _userDataDir = new TempDirectory(); - var options = (LaunchPersistentOptions)TestConstants.GetDefaultBrowserOptions(); + var options = TestConstants.GetDefaultBrowserOptions().ToPersistentOptions(); options.DownloadsPath = _downloadsPath.Path; options.AcceptDownloads = true; _context = await Playwright[TestConstants.Product].LaunchPersistenContextAsync(_userDataDir.Path, options); diff --git a/src/PlaywrightSharp.Tests/Emulation/BrowserContextLocaleTests.cs b/src/PlaywrightSharp.Tests/Emulation/BrowserContextLocaleTests.cs index 62e3d6bf98..c23f90940d 100644 --- a/src/PlaywrightSharp.Tests/Emulation/BrowserContextLocaleTests.cs +++ b/src/PlaywrightSharp.Tests/Emulation/BrowserContextLocaleTests.cs @@ -25,10 +25,7 @@ public BrowserContextLocaleTests(ITestOutputHelper output) : base(output) [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)] public async Task ShouldAffectAcceptLanguageHeader() { - await using var context = await Browser.NewContextAsync(new BrowserContextOptions - { - Locale = "fr-CH" - }); + await using var context = await Browser.NewContextAsync(locale: "fr-CH"); string acceptLanguage = string.Empty; var page = await context.NewPageAsync(); var requestTask = Server.WaitForRequest("/empty.html", c => acceptLanguage = c.Headers["accept-language"]); diff --git a/src/PlaywrightSharp.Tests/Page/DownloadTests.cs b/src/PlaywrightSharp.Tests/Page/DownloadTests.cs index 1b8eadf16e..39c1470936 100644 --- a/src/PlaywrightSharp.Tests/Page/DownloadTests.cs +++ b/src/PlaywrightSharp.Tests/Page/DownloadTests.cs @@ -61,7 +61,7 @@ await TaskUtils.WhenAll( [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)] public async Task ShouldReportDownloadsWithAcceptDownloadsTrue() { - var page = await Browser.NewPageAsync(new BrowserContextOptions { AcceptDownloads = true }); + var page = await Browser.NewPageAsync(acceptDownloads: true); await page.SetContentAsync($"download"); var downloadTask = page.WaitForEvent(PageEvent.Download); diff --git a/src/PlaywrightSharp.Tests/TestConstants.cs b/src/PlaywrightSharp.Tests/TestConstants.cs index c0122c1c15..29959b92a8 100644 --- a/src/PlaywrightSharp.Tests/TestConstants.cs +++ b/src/PlaywrightSharp.Tests/TestConstants.cs @@ -13,7 +13,7 @@ internal static class TestConstants public const string FirefoxProduct = "FIREFOX"; public static string Product => string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PRODUCT")) ? - WebkitProduct : + ChromiumProduct : Environment.GetEnvironmentVariable("PRODUCT"); public const string TestFixtureBrowserCollectionName = "PlaywrightSharpBrowserLoaderFixture collection"; @@ -34,7 +34,6 @@ internal static LaunchOptions GetDefaultBrowserOptions() SlowMo = Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO")), Headless = Convert.ToBoolean(Environment.GetEnvironmentVariable("HEADLESS") ?? "true"), Timeout = 0, - LogProcess = true, }; public static LaunchOptions GetHeadfulOptions() diff --git a/src/PlaywrightSharp/Browser.cs b/src/PlaywrightSharp/Browser.cs index cc7c71e578..3ee85a1674 100644 --- a/src/PlaywrightSharp/Browser.cs +++ b/src/PlaywrightSharp/Browser.cs @@ -77,9 +77,83 @@ public async Task CloseAsync() } /// - public async Task NewContextAsync(BrowserContextOptions options = null) + public Task NewContextAsync( + ViewportSize viewport, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => NewContextAsync(new BrowserContextOptions + { + Viewport = viewport, + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public Task NewContextAsync( + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => NewContextAsync(new BrowserContextOptions + { + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public async Task NewContextAsync(BrowserContextOptions options) { - var context = (await _channel.NewContextAsync(options).ConfigureAwait(false)).Object; + var context = (await _channel.NewContextAsync(options ?? new BrowserContextOptions()).ConfigureAwait(false)).Object; BrowserContextsList.Add(context); context.Browser = this; return context; @@ -89,7 +163,81 @@ public async Task NewContextAsync(BrowserContextOptions options public async Task NewBrowserCDPSessionAsync() => (await _channel.NewBrowserCDPSessionAsync().ConfigureAwait(false)).Object; /// - public async Task NewPageAsync(BrowserContextOptions options = null) + public Task NewPageAsync( + ViewportSize viewport, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => NewPageAsync(new BrowserContextOptions + { + Viewport = viewport, + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public Task NewPageAsync( + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => NewPageAsync(new BrowserContextOptions + { + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public async Task NewPageAsync(BrowserContextOptions options) { var context = await NewContextAsync(options).ConfigureAwait(false) as BrowserContext; var page = await context.NewPageAsync().ConfigureAwait(false) as Page; diff --git a/src/PlaywrightSharp/BrowserContextOptions.cs b/src/PlaywrightSharp/BrowserContextOptions.cs index 11a6f16a53..8a6108ad35 100644 --- a/src/PlaywrightSharp/BrowserContextOptions.cs +++ b/src/PlaywrightSharp/BrowserContextOptions.cs @@ -4,232 +4,94 @@ namespace PlaywrightSharp { /// - /// options. + /// . /// public class BrowserContextOptions { /// /// Sets a consistent viewport for each page. Defaults to an 800x600 viewport. null disables the default viewport. /// - public ViewportSize Viewport - { - get - { - Values.TryGetValue("viewport", out object result); - return result as ViewportSize; - } - set => Values["viewport"] = value; - } + public ViewportSize Viewport { get; set; } = ViewportSize.None; /// /// Specific user agent to use in this context. /// - public string UserAgent - { - get - { - Values.TryGetValue("userAgent", out object result); - return result as string; - } - set => Values["userAgent"] = value; - } + public string UserAgent { get; set; } /// /// Toggles bypassing page's Content-Security-Policy. /// - public bool? BypassCSP - { - get - { - Values.TryGetValue("bypassCSP", out object result); - return result as bool?; - } - set => Values["bypassCSP"] = value; - } + public bool? BypassCSP { get; set; } /// /// Whether or not to enable or disable JavaScript in the context. Defaults to true. /// - public bool? JavaScriptEnabled - { - get - { - Values.TryGetValue("javaScriptEnabled", out object result); - return result as bool?; - } - set => Values["javaScriptEnabled"] = value; - } + public bool? JavaScriptEnabled { get; set; } /// /// Whether to ignore HTTPS errors during navigation. Defaults to false. /// - public bool? IgnoreHTTPSErrors - { - get - { - Values.TryGetValue("ignoreHTTPSErrors", out object result); - return result as bool?; - } - set => Values["ignoreHTTPSErrors"] = value; - } + public bool? IgnoreHTTPSErrors { get; set; } /// /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. /// - public string TimezoneId - { - get - { - Values.TryGetValue("timezoneId", out object result); - return result as string; - } - set => Values["timezoneId"] = value; - } + public string TimezoneId { get; set; } /// /// Changes the Geolocation of the context. /// - public Geolocation Geolocation - { - get - { - Values.TryGetValue("geolocation", out object result); - return result as Geolocation; - } - set => Values["geolocation"] = value; - } + public Geolocation Geolocation { get; set; } /// /// A from origin keys to permissions values. See for more details. /// - public ContextPermission[] Permissions - { - get - { - Values.TryGetValue("permissions", out object result); - return result as ContextPermission[]; - } - set => Values["permissions"] = value; - } + public ContextPermission[] Permissions { get; set; } /// /// Gets or sets whether the meta viewport tag is taken into account. /// - public bool? IsMobile - { - get - { - Values.TryGetValue("isMobile", out object result); - return result as bool?; - } - set => Values["isMobile"] = value; - } + public bool? IsMobile { get; set; } /// /// Whether to emulate network being offline. Defaults to `false`. /// - public bool? Offline - { - get - { - Values.TryGetValue("offline", out object result); - return result as bool?; - } - set => Values["offline"] = value; - } + public bool? Offline { get; set; } /// /// Gets or sets the device scale factor. /// - public decimal? DeviceScaleFactor - { - get - { - Values.TryGetValue("deviceScaleFactor", out object result); - return result as decimal?; - } - set => Values["deviceScaleFactor"] = value; - } + public decimal? DeviceScaleFactor { get; set; } /// /// Credentials for HTTP authentication. /// - public Credentials HttpCredentials - { - get - { - Values.TryGetValue("httpCredentials", out object result); - return result as Credentials; - } - set => Values["httpCredentials"] = value; - } + public Credentials HttpCredentials { get; set; } /// /// Specifies if viewport supports touch events. Defaults to false. /// - public bool? HasTouch - { - get - { - Values.TryGetValue("hasTouch", out object result); - return result as bool?; - } - set => Values["hasTouch"] = value; - } + public bool? HasTouch { get; set; } /// /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. /// - public bool? AcceptDownloads - { - get - { - Values.TryGetValue("acceptDownloads", out object result); - return result as bool?; - } - set => Values["acceptDownloads"] = value; - } + public bool? AcceptDownloads { get; set; } /// /// Emulates 'prefers-colors-scheme' media feature. /// - public ColorScheme? ColorScheme - { - get - { - Values.TryGetValue("colorScheme", out object result); - return result as ColorScheme?; - } - set => Values["colorScheme"] = value; - } + public ColorScheme? ColorScheme { get; set; } /// /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. /// - public string Locale - { - get - { - Values.TryGetValue("locale", out object result); - return result as string; - } - set => Values["locale"] = value; - } + public string Locale { get; set; } /// /// An object containing additional HTTP headers to be sent with every request. /// - public Dictionary ExtraHttpHeaders - { - get - { - Values.TryGetValue("extraHTTPHeaders", out object result); - return result as Dictionary; - } - set => Values["extraHTTPHeaders"] = value; - } - - internal Dictionary Values { get; } = new Dictionary(); + public Dictionary ExtraHttpHeaders { get; set; } /// /// Clones the . @@ -243,6 +105,96 @@ public BrowserContextOptions Clone() return copy; } - internal Dictionary ToChannelDictionary() => Values; + internal Dictionary ToChannelDictionary() + { + var args = new Dictionary(); + + if (Viewport == null || !Viewport.Equals(ViewportSize.None)) + { + args["viewport"] = Viewport; + } + + if (!string.IsNullOrEmpty(UserAgent)) + { + args["userAgent"] = UserAgent; + } + + if (BypassCSP != null) + { + args["bypassCSP"] = BypassCSP; + } + + if (JavaScriptEnabled != null) + { + args["javaScriptEnabled"] = JavaScriptEnabled; + } + + if (IgnoreHTTPSErrors != null) + { + args["ignoreHTTPSErrors"] = IgnoreHTTPSErrors; + } + + if (!string.IsNullOrEmpty(TimezoneId)) + { + args["timezoneId"] = TimezoneId; + } + + if (Geolocation != null) + { + args["geolocation"] = Geolocation; + } + + if (Permissions != null) + { + args["permissions"] = Permissions; + } + + if (IsMobile != null) + { + args["isMobile"] = IsMobile; + } + + if (Offline != null) + { + args["offline"] = Offline; + } + + if (DeviceScaleFactor != null) + { + args["deviceScaleFactor"] = DeviceScaleFactor; + } + + if (HttpCredentials != null) + { + args["httpCredentials"] = HttpCredentials; + } + + if (HasTouch != null) + { + args["hasTouch"] = HasTouch; + } + + if (AcceptDownloads != null) + { + args["acceptDownloads"] = AcceptDownloads; + } + + if (ColorScheme != null) + { + args["colorScheme"] = ColorScheme; + } + + if (Locale != null) + { + args["locale"] = Locale; + } + + if (ExtraHttpHeaders != null) + { + args["extraHTTPHeaders"] = ExtraHttpHeaders; + } + + return args; + } } } diff --git a/src/PlaywrightSharp/BrowserType.cs b/src/PlaywrightSharp/BrowserType.cs index 9df3f9933e..9e458c1945 100644 --- a/src/PlaywrightSharp/BrowserType.cs +++ b/src/PlaywrightSharp/BrowserType.cs @@ -51,17 +51,231 @@ internal BrowserType(ConnectionScope scope, string guid, BrowserTypeInitializer /// public string Name => _initializer.Name; + /// + public Task LaunchAsync( + bool? headless = null, + string[] args = null, + string userDataDir = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null) + => LaunchAsync(new LaunchOptions + { + Headless = headless, + Args = args, + UserDataDir = userDataDir, + Devtools = devtools, + ExecutablePath = executablePath, + DownloadsPath = downloadsPath, + IgnoreHTTPSErrors = ignoreHTTPSErrors, + Timeout = timeout, + DumpIO = dumpIO, + SlowMo = slowMo, + IgnoreDefaultArgs = ignoreHTTPSErrors, + IgnoredDefaultArgs = ignoredDefaultArgs, + Env = env, + FirefoxUserPrefs = firefoxUserPrefs, + Proxy = proxy, + }); + /// public async Task LaunchAsync(LaunchOptions options = null) - => (await _channel.LaunchAsync(options).ConfigureAwait(false)).Object; + => (await _channel.LaunchAsync(options ?? new LaunchOptions()).ConfigureAwait(false)).Object; /// public async Task LaunchServerAsync(LaunchOptions options = null) => (await _channel.LaunchServerAsync(options).ConfigureAwait(false)).Object; + /// + public Task LaunchServerAsync( + bool? headless = null, + string[] args = null, + string userDataDir = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null) + => LaunchServerAsync(new LaunchOptions + { + Headless = headless, + Args = args, + UserDataDir = userDataDir, + Devtools = devtools, + ExecutablePath = executablePath, + DownloadsPath = downloadsPath, + IgnoreHTTPSErrors = ignoreHTTPSErrors, + Timeout = timeout, + DumpIO = dumpIO, + SlowMo = slowMo, + IgnoreDefaultArgs = ignoreHTTPSErrors, + IgnoredDefaultArgs = ignoredDefaultArgs, + Env = env, + FirefoxUserPrefs = firefoxUserPrefs, + Proxy = proxy, + }); + + /// + public Task LaunchPersistenContextAsync( + string userDataDir, + ViewportSize viewport, + bool? headless = null, + string[] args = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => LaunchPersistenContextAsync( + userAgent, + new LaunchPersistentOptions + { + Headless = headless, + Args = args, + UserDataDir = userDataDir, + Devtools = devtools, + ExecutablePath = executablePath, + DownloadsPath = downloadsPath, + IgnoreHTTPSErrors = ignoreHTTPSErrors, + Timeout = timeout, + DumpIO = dumpIO, + SlowMo = slowMo, + IgnoreDefaultArgs = ignoreHTTPSErrors, + IgnoredDefaultArgs = ignoredDefaultArgs, + Env = env, + FirefoxUserPrefs = firefoxUserPrefs, + Proxy = proxy, + Viewport = viewport, + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public Task LaunchPersistenContextAsync( + string userDataDir, + bool? headless = null, + string[] args = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null) + => LaunchPersistenContextAsync( + userAgent, + new LaunchPersistentOptions + { + Headless = headless, + Args = args, + UserDataDir = userDataDir, + Devtools = devtools, + ExecutablePath = executablePath, + DownloadsPath = downloadsPath, + IgnoreHTTPSErrors = ignoreHTTPSErrors, + Timeout = timeout, + DumpIO = dumpIO, + SlowMo = slowMo, + IgnoreDefaultArgs = ignoreHTTPSErrors, + IgnoredDefaultArgs = ignoredDefaultArgs, + Env = env, + FirefoxUserPrefs = firefoxUserPrefs, + Proxy = proxy, + UserAgent = userAgent, + BypassCSP = bypassCSP, + JavaScriptEnabled = javaScriptEnabled, + TimezoneId = timezoneId, + Geolocation = geolocation, + Permissions = permissions, + IsMobile = isMobile, + Offline = offline, + DeviceScaleFactor = deviceScaleFactor, + HttpCredentials = httpCredentials, + HasTouch = hasTouch, + AcceptDownloads = acceptDownloads, + ColorScheme = colorScheme, + Locale = locale, + ExtraHttpHeaders = extraHttpHeaders, + }); + + /// + public Task LaunchPersistenContextAsync(string userDataDir, LaunchOptions options) + => LaunchPersistenContextAsync(userDataDir, options?.ToPersistentOptions() ?? new LaunchPersistentOptions()); + /// public async Task LaunchPersistenContextAsync(string userDataDir, LaunchPersistentOptions options) - => (await _channel.LaunchPersistenContextAsync(userDataDir, options).ConfigureAwait(false)).Object; + => (await _channel.LaunchPersistenContextAsync(userDataDir, options ?? new LaunchPersistentOptions()).ConfigureAwait(false)).Object; /// public async Task ConnectAsync(string wsEndpoint, int? timeout = null, int? slowMo = null) diff --git a/src/PlaywrightSharp/IBrowser.cs b/src/PlaywrightSharp/IBrowser.cs index dc78de74a8..15ce59b4bd 100644 --- a/src/PlaywrightSharp/IBrowser.cs +++ b/src/PlaywrightSharp/IBrowser.cs @@ -58,6 +58,100 @@ public interface IBrowser : IAsyncDisposable /// A that completes when the browser is closed. Task CloseAsync(); + /// + /// Creates a new browser context. It won't share cookies/cache with other browser contexts. + /// + /// Sets a consistent viewport for each page. Defaults to an 800x600 viewport. null disables the default viewport. + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// . + /// + /// + /// + /// + /// A that completes when a new is created. + Task NewContextAsync( + ViewportSize viewport, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); + + /// + /// Creates a new browser context. It won't share cookies/cache with other browser contexts. + /// + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// . + /// + /// + /// + /// + /// A that completes when a new is created. + Task NewContextAsync( + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); + /// /// Creates a new browser context. It won't share cookies/cache with other browser contexts. /// @@ -73,14 +167,108 @@ public interface IBrowser : IAsyncDisposable /// /// /// A that completes when a new is created. - Task NewContextAsync(BrowserContextOptions options = null); + Task NewContextAsync(BrowserContextOptions options); + + /// + /// Creates a new page in a new browser context. Closing this page will close the context as well. + /// + /// Sets a consistent viewport for each page. Defaults to an 800x600 viewport. null disables the default viewport. + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// . + /// + /// + /// + /// + /// A that completes when a new is created. + Task NewPageAsync( + ViewportSize viewport, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); + + /// + /// Creates a new page in a new browser context. Closing this page will close the context as well. + /// + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// . + /// + /// + /// + /// + /// A that completes when a new is created. + Task NewPageAsync( + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); /// /// Creates a new page in a new browser context. Closing this page will close the context as well. /// /// Context options. /// A that completes when a new is created. - Task NewPageAsync(BrowserContextOptions options = null); + Task NewPageAsync(BrowserContextOptions options); /// /// Creates a new browser session. diff --git a/src/PlaywrightSharp/IBrowserContext.cs b/src/PlaywrightSharp/IBrowserContext.cs index d3839406b6..221a21bda0 100644 --- a/src/PlaywrightSharp/IBrowserContext.cs +++ b/src/PlaywrightSharp/IBrowserContext.cs @@ -8,7 +8,7 @@ namespace PlaywrightSharp /// /// BrowserContexts provide a way to operate multiple independent browser sessions. /// If a opens another page, e.g.with a window.open call, the popup will belong to the parent page's browser context. - /// PlaywrightSharp allows creation of "incognito" browser contexts with method. "Incognito" browser contexts don't write any browsing data to disk. + /// PlaywrightSharp allows creation of "incognito" browser contexts with method. "Incognito" browser contexts don't write any browsing data to disk. /// /// /// diff --git a/src/PlaywrightSharp/IBrowserType.cs b/src/PlaywrightSharp/IBrowserType.cs index e92a1e8764..89de7b8507 100644 --- a/src/PlaywrightSharp/IBrowserType.cs +++ b/src/PlaywrightSharp/IBrowserType.cs @@ -24,7 +24,44 @@ public interface IBrowserType /// /// Launch options. /// A that completes when the browser is launched, yielding the browser. - Task LaunchAsync(LaunchOptions options = null); + Task LaunchAsync(LaunchOptions options); + + /// + /// Launches a new browser. + /// + /// Whether to run browser in headless mode. Defaults to true unless the devtools option is true. + /// Additional arguments to pass to the browser instance. + /// Path to a User Data Directory. + /// Whether to auto-open DevTools panel for each tab. If this option is true, the headless option will be set false. + /// Path to a browser executable to run instead of the bundled one. + /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. + /// Whether to ignore HTTPS errors during navigation. Defaults to false. + /// Maximum time in milliseconds to wait for the browser instance to start. + /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. + /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. + /// If true, Playwright does not pass its own configurations args and only uses the ones from args. + /// Dangerous option; use with care. Defaults to false. + /// if is set to false this list will be used to filter default arguments. + /// Specify environment variables that will be visible to browser. Defaults to Environment variables. + /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. + /// Network proxy settings. + /// A that completes when the browser is launched, yielding the browser. + Task LaunchAsync( + bool? headless = null, + string[] args = null, + string userDataDir = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null); /// /// Launches browser server that client can connect to. @@ -33,6 +70,187 @@ public interface IBrowserType /// A that completes when the browser is launched, yielding the browser server. Task LaunchServerAsync(LaunchOptions options); + /// + /// Launches browser server that client can connect to. + /// + /// Whether to run browser in headless mode. Defaults to true unless the devtools option is true. + /// Additional arguments to pass to the browser instance. + /// Path to a User Data Directory. + /// Whether to auto-open DevTools panel for each tab. If this option is true, the headless option will be set false. + /// Path to a browser executable to run instead of the bundled one. + /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. + /// Whether to ignore HTTPS errors during navigation. Defaults to false. + /// Maximum time in milliseconds to wait for the browser instance to start. + /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. + /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. + /// If true, Playwright does not pass its own configurations args and only uses the ones from args. + /// Dangerous option; use with care. Defaults to false. + /// if is set to false this list will be used to filter default arguments. + /// Specify environment variables that will be visible to browser. Defaults to Environment variables. + /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. + /// Network proxy settings. + /// A that completes when the browser is launched, yielding the browser server. + Task LaunchServerAsync( + bool? headless = null, + string[] args = null, + string userDataDir = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null); + + /// + /// Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing this context will automatically close the browser. + /// + /// Path to a User Data Directory, which stores browser session data like cookies and local storage. + /// Launch options. + /// A that completes when the browser is launched, yielding the browser server. + Task LaunchPersistenContextAsync(string userDataDir, LaunchOptions options); + + /// + /// Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing this context will automatically close the browser. + /// + /// Path to a User Data Directory, which stores browser session data like cookies and local storage. + /// Sets a consistent viewport for each page. Defaults to an 800x600 viewport. null disables the default viewport. + /// Whether to run browser in headless mode. Defaults to true unless the devtools option is true. + /// Additional arguments to pass to the browser instance. + /// Whether to auto-open DevTools panel for each tab. If this option is true, the headless option will be set false. + /// Path to a browser executable to run instead of the bundled one. + /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. + /// Whether to ignore HTTPS errors during navigation. Defaults to false. + /// Maximum time in milliseconds to wait for the browser instance to start. + /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. + /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. + /// If true, Playwright does not pass its own configurations args and only uses the ones from args. + /// Dangerous option; use with care. Defaults to false. + /// if is set to false this list will be used to filter default arguments. + /// Specify environment variables that will be visible to browser. Defaults to Environment variables. + /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. + /// Network proxy settings. + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// A that completes when the browser is launched, yielding the browser server. + Task LaunchPersistenContextAsync( + string userDataDir, + ViewportSize viewport, + bool? headless = null, + string[] args = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); + + /// + /// Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing this context will automatically close the browser. + /// + /// Path to a User Data Directory, which stores browser session data like cookies and local storage. + /// Whether to run browser in headless mode. Defaults to true unless the devtools option is true. + /// Additional arguments to pass to the browser instance. + /// Whether to auto-open DevTools panel for each tab. If this option is true, the headless option will be set false. + /// Path to a browser executable to run instead of the bundled one. + /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. + /// Whether to ignore HTTPS errors during navigation. Defaults to false. + /// Maximum time in milliseconds to wait for the browser instance to start. + /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. + /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. + /// If true, Playwright does not pass its own configurations args and only uses the ones from args. + /// Dangerous option; use with care. Defaults to false. + /// if is set to false this list will be used to filter default arguments. + /// Specify environment variables that will be visible to browser. Defaults to Environment variables. + /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. + /// Network proxy settings. + /// Specific user agent to use in this context. + /// Toggles bypassing page's Content-Security-Policy. + /// Whether or not to enable or disable JavaScript in the context. Defaults to true. + /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. + /// Changes the Geolocation of the context. + /// A from origin keys to permissions values. See for more details. + /// Gets or sets whether the meta viewport tag is taken into account. + /// Whether to emulate network being offline. Defaults to `false`. + /// Gets or sets the device scale factor. + /// Credentials for HTTP authentication. + /// Specifies if viewport supports touch events. Defaults to false. + /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. + /// Emulates 'prefers-colors-scheme' media feature. + /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. + /// An object containing additional HTTP headers to be sent with every request. + /// A that completes when the browser is launched, yielding the browser server. + Task LaunchPersistenContextAsync( + string userDataDir, + bool? headless = null, + string[] args = null, + bool? devtools = null, + string executablePath = null, + string downloadsPath = null, + bool? ignoreHTTPSErrors = null, + int? timeout = null, + bool? dumpIO = null, + int? slowMo = null, + bool? ignoreDefaultArgs = null, + string[] ignoredDefaultArgs = null, + IDictionary env = null, + IDictionary firefoxUserPrefs = null, + ProxySettings proxy = null, + string userAgent = null, + bool? bypassCSP = null, + bool? javaScriptEnabled = null, + string timezoneId = null, + Geolocation geolocation = null, + ContextPermission[] permissions = null, + bool? isMobile = null, + bool? offline = null, + decimal? deviceScaleFactor = null, + Credentials httpCredentials = null, + bool? hasTouch = null, + bool? acceptDownloads = null, + ColorScheme? colorScheme = null, + string locale = null, + Dictionary extraHttpHeaders = null); + /// /// Launches browser that uses persistent storage located at userDataDir and returns the only context. Closing this context will automatically close the browser. /// diff --git a/src/PlaywrightSharp/LaunchOptions.cs b/src/PlaywrightSharp/LaunchOptions.cs index 95999d91f8..d54f08b505 100644 --- a/src/PlaywrightSharp/LaunchOptions.cs +++ b/src/PlaywrightSharp/LaunchOptions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace PlaywrightSharp @@ -10,240 +11,183 @@ public class LaunchOptions /// /// Whether to run browser in headless mode. Defaults to true unless the devtools option is true. /// - public bool? Headless - { - get - { - Values.TryGetValue("headless", out object result); - return result as bool?; - } - set => Values["headless"] = value; - } + public bool? Headless { get; set; } /// /// Additional arguments to pass to the browser instance. /// - public string[] Args - { - get - { - Values.TryGetValue("args", out object result); - return result as string[]; - } - set => Values["args"] = value; - } + public string[] Args { get; set; } /// /// Path to a User Data Directory. /// - public string UserDataDir - { - get - { - Values.TryGetValue("userDataDir", out object result); - return result as string; - } - set => Values["userDataDir"] = value; - } + public string UserDataDir { get; set; } /// /// Whether to auto-open DevTools panel for each tab. If this option is true, the headless option will be set false. /// - public bool? Devtools - { - get - { - Values.TryGetValue("devtools", out object result); - return result as bool?; - } - set => Values["devtools"] = value; - } + public bool? Devtools { get; set; } /// /// Path to a browser executable to run instead of the bundled one. /// - public string ExecutablePath - { - get - { - Values.TryGetValue("executablePath", out object result); - return result as string; - } - set => Values["executablePath"] = value; - } + public string ExecutablePath { get; set; } /// /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. /// - public string DownloadsPath - { - get - { - Values.TryGetValue("downloadsPath", out object result); - return result as string; - } - set => Values["downloadsPath"] = value; - } + public string DownloadsPath { get; set; } /// /// Whether to ignore HTTPS errors during navigation. Defaults to false. /// - public bool? IgnoreHTTPSErrors - { - get - { - Values.TryGetValue("ignoreHTTPSErrors", out object result); - return result as bool?; - } - set => Values["ignoreHTTPSErrors"] = value; - } + public bool? IgnoreHTTPSErrors { get; set; } /// /// Maximum time in milliseconds to wait for the browser instance to start. /// - public int? Timeout - { - get - { - Values.TryGetValue("timeout", out object result); - return result as int?; - } - set => Values["timeout"] = value; - } + public int? Timeout { get; set; } /// /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. /// - public bool? DumpIO - { - get - { - Values.TryGetValue("dumpIO", out object result); - return result as bool?; - } - set => Values["dumpIO"] = value; - } + public bool? DumpIO { get; set; } /// /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. /// - public int? SlowMo - { - get - { - Values.TryGetValue("slowMo", out object result); - return result as int?; - } - set => Values["slowmMo"] = value; - } - - /// - /// Logs process counts after launching the browser and after exiting. - /// - public bool? LogProcess - { - get - { - Values.TryGetValue("logProgress", out object result); - return result as bool?; - } - set => Values["logProgress"] = value; - } + public int? SlowMo { get; set; } /// /// If true, Playwright does not pass its own configurations args and only uses the ones from args. /// Dangerous option; use with care. Defaults to false. /// - public bool? IgnoreDefaultArgs - { - get - { - Values.TryGetValue("ignoreDefaultAR", out object result); - return result as bool?; - } - set => Values["headless"] = value; - } + public bool? IgnoreDefaultArgs { get; set; } /// /// if is set to false this list will be used to filter default arguments. /// - public string[] IgnoredDefaultArgs - { - get - { - Values.TryGetValue("ignoredDefaultArgs", out object result); - return result as string[]; - } - set => Values["ignoredDefaultArgs"] = value; - } + public string[] IgnoredDefaultArgs { get; set; } /// /// Specify environment variables that will be visible to browser. Defaults to Environment variables. /// - public IDictionary Env - { - get - { - Values.TryGetValue("env", out object result); - return result as Dictionary; - } - set => Values["env"] = value; - } + public IDictionary Env { get; set; } /// /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. /// - public IDictionary FirefoxUserPrefs - { - get - { - Values.TryGetValue("firefoxUserPrefs", out object result); - return result as Dictionary; - } - set => Values["firefoxUserPrefs"] = value; - } + public IDictionary FirefoxUserPrefs { get; set; } /// /// Network proxy settings. /// - public ProxySettings Proxy - { - get - { - Values.TryGetValue("proxy", out object result); - return result as ProxySettings; - } - set => Values["proxy"] = value; - } - - internal Dictionary Values { get; } = new Dictionary(); + public ProxySettings Proxy { get; set; } /// /// Converts the to . /// - /// Option to convert. - public static implicit operator LaunchPersistentOptions(LaunchOptions options) + /// The object converted to . + public LaunchPersistentOptions ToPersistentOptions() + => new LaunchPersistentOptions + { + Headless = Headless, + Args = Args, + UserDataDir = UserDataDir, + Devtools = Devtools, + ExecutablePath = ExecutablePath, + DownloadsPath = DownloadsPath, + IgnoreHTTPSErrors = IgnoreHTTPSErrors, + Timeout = Timeout, + DumpIO = DumpIO, + SlowMo = SlowMo, + IgnoreDefaultArgs = IgnoreDefaultArgs, + IgnoredDefaultArgs = IgnoredDefaultArgs, + Env = Env, + FirefoxUserPrefs = FirefoxUserPrefs, + Proxy = Proxy, + }; + + internal virtual Dictionary ToChannelDictionary() { - if (options == null) + var args = new Dictionary(); + + if (Headless != null) { - return null; + args["headless"] = Headless; } - var result = new LaunchPersistentOptions(); - foreach (var kv in options.Values) + if (Args != null) { - result.Values[kv.Key] = kv.Value; + args["args"] = Args; } - return result; - } + if (!string.IsNullOrEmpty(UserDataDir)) + { + args["userDataDir"] = UserDataDir; + } - /// - /// Converts the to . - /// - /// A with the same information as the . - public LaunchPersistentOptions ToLaunchPersistentOptions() => this; + if (Devtools != null) + { + args["devTools"] = Devtools; + } + + if (ExecutablePath != null) + { + args["executablePath"] = ExecutablePath; + } + + if (DownloadsPath != null) + { + args["downloadsPath"] = DownloadsPath; + } - internal Dictionary ToChannelDictionary() => Values; + if (IgnoreHTTPSErrors != null) + { + args["ignoreHTTPSErrors"] = IgnoreHTTPSErrors; + } + + if (Timeout != null) + { + args["timeout"] = Timeout; + } + + if (DumpIO != null) + { + args["dumpIO"] = DumpIO; + } + + if (SlowMo != null) + { + args["slowMo"] = SlowMo; + } + + if (IgnoreDefaultArgs != null) + { + args["ignoreDefaultArgs"] = IgnoreDefaultArgs; + } + + if (IgnoredDefaultArgs != null) + { + args["ignoredDefaultArgs"] = IgnoredDefaultArgs; + } + + if (Env != null) + { + args["env"] = Env; + } + + if (FirefoxUserPrefs != null) + { + args["firefoxUserPrefs"] = FirefoxUserPrefs; + } + + if (Proxy != null) + { + args["proxy"] = Proxy; + } + + return args; + } } } diff --git a/src/PlaywrightSharp/LaunchPersistentOptions.cs b/src/PlaywrightSharp/LaunchPersistentOptions.cs index aa0bb8ea49..459f9039e7 100644 --- a/src/PlaywrightSharp/LaunchPersistentOptions.cs +++ b/src/PlaywrightSharp/LaunchPersistentOptions.cs @@ -6,361 +6,87 @@ namespace PlaywrightSharp /// /// Options for . /// - public class LaunchPersistentOptions + public class LaunchPersistentOptions : LaunchOptions { - /// - /// Path to a browser executable to run instead of the bundled one. - /// - public string ExecutablePath - { - get - { - Values.TryGetValue("executablePath", out object result); - return result as string; - } - set => Values["executablePath"] = value; - } - - /// - /// If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. - /// - public string DownloadsPath - { - get - { - Values.TryGetValue("downloadsPath", out object result); - return result as string; - } - set => Values["downloadsPath"] = value; - } - - /// - /// Whether to ignore HTTPS errors during navigation. Defaults to false. - /// - public bool? IgnoreHTTPSErrors - { - get - { - Values.TryGetValue("ignoreHTTPSErrors", out object result); - return result as bool?; - } - set => Values["ignoreHTTPSErrors"] = value; - } - - /// - /// Maximum time in milliseconds to wait for the browser instance to start. - /// - public int? Timeout - { - get - { - Values.TryGetValue("timeout", out object result); - return result as int?; - } - set => Values["timeout"] = value; - } - - /// - /// Whether to pipe browser process stdout and stderr into process.stdout and process.stderr. Defaults to false. - /// - public bool? DumpIO - { - get - { - Values.TryGetValue("dumpIO", out object result); - return result as bool?; - } - set => Values["dumpIO"] = value; - } - - /// - /// Slows down PlaywrightSharp operations by the specified amount of milliseconds. Useful so that you can see what is going on. - /// - public int? SlowMo - { - get - { - Values.TryGetValue("slowmo", out object result); - return result as int?; - } - set => Values["slowmo"] = value; - } - - /// - /// If true, Playwright does not pass its own configurations args and only uses the ones from args. - /// Dangerous option; use with care. Defaults to false. - /// - public bool? IgnoreDefaultArgs - { - get - { - Values.TryGetValue("ignoreDefaultAR", out object result); - return result as bool?; - } - set => Values["headless"] = value; - } - - /// - /// if is set to false this list will be used to filter default arguments. - /// - public string[] IgnoredDefaultArgs - { - get - { - Values.TryGetValue("ignoredDefaultArgs", out object result); - return result as string[]; - } - set => Values["ignoredDefaultArgs"] = value; - } - - /// - /// Specify environment variables that will be visible to browser. Defaults to Environment variables. - /// - public IDictionary Env - { - get - { - Values.TryGetValue("env", out object result); - return result as Dictionary; - } - set => Values["env"] = value; - } - - /// - /// Firefox user preferences. Learn more about the Firefox user preferences at about:config. - /// - public IDictionary FirefoxUserPrefs - { - get - { - Values.TryGetValue("firefoxUserPrefs", out object result); - return result as Dictionary; - } - set => Values["firefoxUserPrefs"] = value; - } - /// /// Sets a consistent viewport for each page. Defaults to an 800x600 viewport. null disables the default viewport. /// - public ViewportSize Viewport - { - get - { - Values.TryGetValue("viewport", out object result); - return result as ViewportSize; - } - set => Values["viewport"] = value; - } + public ViewportSize Viewport { get; set; } /// /// Specific user agent to use in this context. /// - public string UserAgent - { - get - { - Values.TryGetValue("userAgent", out object result); - return result as string; - } - set => Values["userAgent"] = value; - } + public string UserAgent { get; set; } /// /// Toggles bypassing page's Content-Security-Policy. /// - public bool? BypassCSP - { - get - { - Values.TryGetValue("bypassCSP", out object result); - return result as bool?; - } - set => Values["bypassCSP"] = value; - } + public bool? BypassCSP { get; set; } /// /// Whether or not to enable or disable JavaScript in the context. Defaults to true. /// - public bool? JavaScriptEnabled - { - get - { - Values.TryGetValue("javaScriptEnabled", out object result); - return result as bool?; - } - set => Values["javaScriptEnabled"] = value; - } + public bool? JavaScriptEnabled { get; set; } /// /// Changes the timezone of the context. See ICU’s metaZones.txt for a list of supported timezone IDs. /// - public string TimezoneId - { - get - { - Values.TryGetValue("timezoneId", out object result); - return result as string; - } - set => Values["timezoneId"] = value; - } + public string TimezoneId { get; set; } /// /// Changes the Geolocation of the context. /// - public Geolocation Geolocation - { - get - { - Values.TryGetValue("geolocation", out object result); - return result as Geolocation; - } - set => Values["geolocation"] = value; - } + public Geolocation Geolocation { get; set; } /// /// A from origin keys to permissions values. See for more details. /// - public ContextPermission[] Permissions - { - get - { - Values.TryGetValue("permissions", out object result); - return result as ContextPermission[]; - } - set => Values["permissions"] = value; - } + public ContextPermission[] Permissions { get; set; } /// /// Gets or sets whether the meta viewport tag is taken into account. /// - public bool? IsMobile - { - get - { - Values.TryGetValue("isMobile", out object result); - return result as bool?; - } - set => Values["isMobile"] = value; - } + public bool? IsMobile { get; set; } /// /// Whether to emulate network being offline. Defaults to `false`. /// - public bool? Offline - { - get - { - Values.TryGetValue("offline", out object result); - return result as bool?; - } - set => Values["offline"] = value; - } + public bool? Offline { get; set; } /// /// Gets or sets the device scale factor. /// - public double? DeviceScaleFactor - { - get - { - Values.TryGetValue("deviceScaleFactor", out object result); - return result as double?; - } - set => Values["deviceScaleFactor"] = value; - } + public decimal? DeviceScaleFactor { get; set; } /// /// Credentials for HTTP authentication. /// - public Credentials HttpCredentials - { - get - { - Values.TryGetValue("httpCredentials", out object result); - return result as Credentials; - } - set => Values["httpCredentials"] = value; - } + public Credentials HttpCredentials { get; set; } /// /// Specifies if viewport supports touch events. Defaults to false. /// - public bool? HasTouch - { - get - { - Values.TryGetValue("hasTouch", out object result); - return result as bool?; - } - set => Values["hasTouch"] = value; - } + public bool? HasTouch { get; set; } /// /// Whether to automatically download all the attachments. Defaults to false where all the downloads are canceled. /// - public bool? AcceptDownloads - { - get - { - Values.TryGetValue("acceptDownloads", out object result); - return result as bool?; - } - set => Values["acceptDownloads"] = value; - } + public bool? AcceptDownloads { get; set; } /// /// Emulates 'prefers-colors-scheme' media feature. /// - public ColorScheme? ColorScheme - { - get - { - Values.TryGetValue("colorScheme", out object result); - return result as ColorScheme?; - } - set => Values["acceptDownloads"] = value; - } + public ColorScheme? ColorScheme { get; set; } /// /// Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. /// - public string Locale - { - get - { - Values.TryGetValue("locale", out object result); - return result as string; - } - set => Values["locale"] = value; - } + public string Locale { get; set; } /// /// An object containing additional HTTP headers to be sent with every request. /// - public Dictionary ExtraHttpHeaders - { - get - { - Values.TryGetValue("extraHTTPHeaders", out object result); - return result as Dictionary; - } - set => Values["extraHTTPHeaders"] = value; - } - - /// - /// Network proxy settings. - /// - public ProxySettings Proxy - { - get - { - Values.TryGetValue("proxy", out object result); - return result as ProxySettings; - } - set => Values["proxy"] = value; - } - - internal Dictionary Values { get; } = new Dictionary(); + public Dictionary ExtraHttpHeaders { get; set; } /// /// Adds all the values set int into . @@ -375,10 +101,23 @@ public ProxySettings Proxy return null; } - foreach (var kv in right.Values) - { - left.Values[kv.Key] = kv.Value; - } + left.Viewport = right.Viewport ?? left.Viewport; + left.UserAgent = right.UserAgent ?? left.UserAgent; + left.BypassCSP = right.BypassCSP ?? left.BypassCSP; + left.JavaScriptEnabled = right.JavaScriptEnabled ?? left.JavaScriptEnabled; + left.IgnoreHTTPSErrors = right.IgnoreHTTPSErrors ?? left.IgnoreHTTPSErrors; + left.TimezoneId = right.TimezoneId ?? left.TimezoneId; + left.Geolocation = right.Geolocation ?? left.Geolocation; + left.Permissions = right.Permissions ?? left.Permissions; + left.IsMobile = right.IsMobile ?? left.IsMobile; + left.Offline = right.Offline ?? left.Offline; + left.DeviceScaleFactor = right.DeviceScaleFactor ?? left.DeviceScaleFactor; + left.HttpCredentials = right.HttpCredentials ?? left.HttpCredentials; + left.HasTouch = right.HasTouch ?? left.HasTouch; + left.AcceptDownloads = right.AcceptDownloads ?? left.AcceptDownloads; + left.ColorScheme = right.ColorScheme ?? left.ColorScheme; + left.Locale = right.Locale ?? left.Locale; + left.ExtraHttpHeaders = right.ExtraHttpHeaders ?? left.ExtraHttpHeaders; return left; } @@ -403,6 +142,96 @@ public BrowserContextOptions Clone() return copy; } - internal Dictionary ToChannelDictionary() => Values; + internal override Dictionary ToChannelDictionary() + { + var args = base.ToChannelDictionary(); + + if (Viewport == null || !Viewport.Equals(ViewportSize.None)) + { + args["viewport"] = Viewport; + } + + if (!string.IsNullOrEmpty(UserAgent)) + { + args["userAgent"] = UserAgent; + } + + if (BypassCSP != null) + { + args["bypassCSP"] = BypassCSP; + } + + if (JavaScriptEnabled != null) + { + args["javaScriptEnabled"] = JavaScriptEnabled; + } + + if (IgnoreHTTPSErrors != null) + { + args["ignoreHTTPSErrors"] = IgnoreHTTPSErrors; + } + + if (!string.IsNullOrEmpty(TimezoneId)) + { + args["timezoneId"] = TimezoneId; + } + + if (Geolocation != null) + { + args["geolocation"] = Geolocation; + } + + if (Permissions != null) + { + args["permissions"] = Permissions; + } + + if (IsMobile != null) + { + args["isMobile"] = IsMobile; + } + + if (Offline != null) + { + args["offline"] = Offline; + } + + if (DeviceScaleFactor != null) + { + args["deviceScaleFactor"] = DeviceScaleFactor; + } + + if (HttpCredentials != null) + { + args["httpCredentials"] = HttpCredentials; + } + + if (HasTouch != null) + { + args["hasTouch"] = HasTouch; + } + + if (AcceptDownloads != null) + { + args["acceptDownloads"] = AcceptDownloads; + } + + if (ColorScheme != null) + { + args["colorScheme"] = ColorScheme; + } + + if (Locale != null) + { + args["locale"] = Locale; + } + + if (ExtraHttpHeaders != null) + { + args["extraHTTPHeaders"] = ExtraHttpHeaders; + } + + return args; + } } } diff --git a/src/PlaywrightSharp/PlaywrightSharp.csproj b/src/PlaywrightSharp/PlaywrightSharp.csproj index 847fd75463..42633366b7 100644 --- a/src/PlaywrightSharp/PlaywrightSharp.csproj +++ b/src/PlaywrightSharp/PlaywrightSharp.csproj @@ -11,6 +11,10 @@ netstandard2.0;netstandard2.1 8.0 + embedded + true + https://github.com/hardkoded/playwright-sharp + true bin\$(Configuration)\netstandard2.0\PlaywrightSharp.xml true false @@ -45,6 +49,7 @@ + diff --git a/src/PlaywrightSharp/Transport/Channels/BrowserChannel.cs b/src/PlaywrightSharp/Transport/Channels/BrowserChannel.cs index 665fc98dd9..7e9c6c2bd5 100644 --- a/src/PlaywrightSharp/Transport/Channels/BrowserChannel.cs +++ b/src/PlaywrightSharp/Transport/Channels/BrowserChannel.cs @@ -28,7 +28,7 @@ internal Task NewContextAsync(BrowserContextOptions optio => Scope.SendMessageToServer( Guid, "newContext", - (options ?? new BrowserContextOptions()).ToChannelDictionary()); + options.ToChannelDictionary()); internal Task CloseAsync() => Scope.SendMessageToServer(Guid, "close", null); diff --git a/src/PlaywrightSharp/Transport/Channels/BrowserTypeChannel.cs b/src/PlaywrightSharp/Transport/Channels/BrowserTypeChannel.cs index 88309119de..d62829137b 100644 --- a/src/PlaywrightSharp/Transport/Channels/BrowserTypeChannel.cs +++ b/src/PlaywrightSharp/Transport/Channels/BrowserTypeChannel.cs @@ -11,10 +11,12 @@ public BrowserTypeChannel(string guid, ConnectionScope scope, BrowserType owner) } public Task LaunchAsync(LaunchOptions options) - => Scope.SendMessageToServer( + { + return Scope.SendMessageToServer( Guid, "launch", - (options ?? new LaunchOptions()).ToChannelDictionary()); + options.ToChannelDictionary()); + } public Task LaunchServerAsync(LaunchOptions options) => Scope.SendMessageToServer( @@ -35,7 +37,7 @@ internal Task ConnectAsync(string wsEndpoint, int? timeout = nul internal Task LaunchPersistenContextAsync(string userDataDir, LaunchPersistentOptions options) { - var args = (options ?? new LaunchPersistentOptions()).ToChannelDictionary(); + var args = options.ToChannelDictionary(); args["userDataDir"] = userDataDir; return Scope.SendMessageToServer(Guid, "launchPersistentContext", args); diff --git a/src/PlaywrightSharp/ViewportSize.cs b/src/PlaywrightSharp/ViewportSize.cs index 1773b69550..6cac83508b 100644 --- a/src/PlaywrightSharp/ViewportSize.cs +++ b/src/PlaywrightSharp/ViewportSize.cs @@ -7,6 +7,11 @@ namespace PlaywrightSharp /// public class ViewportSize : IEquatable { + /// + /// ViewportSize used to determine if the a Viewport was set or not. + /// + public static ViewportSize None => new ViewportSize { Height = -1, Width = -1 }; + /// /// Viewport width. ///