Beispiel #1
0
        internal Task <BrowserContextChannel> NewContextAsync(
            bool?acceptDownloads    = null,
            bool?bypassCSP          = null,
            ColorScheme colorScheme = ColorScheme.Undefined,
            float?deviceScaleFactor = null,
            IEnumerable <KeyValuePair <string, string> > extraHTTPHeaders = null,
            Geolocation geolocation         = null,
            bool?hasTouch                   = null,
            HttpCredentials httpCredentials = null,
            bool?ignoreHTTPSErrors          = null,
            bool?isMobile                   = null,
            bool?javaScriptEnabled          = null,
            string locale                   = null,
            bool?offline = null,
            IEnumerable <string> permissions = null,
            Proxy proxy = null,
            bool?recordHarOmitContent       = null,
            string recordHarPath            = null,
            string recordVideoDir           = null,
            RecordVideoSize recordVideoSize = null,
            string storageState             = null,
            string storageStatePath         = null,
            string timezoneId         = null,
            string userAgent          = null,
            ViewportSize viewportSize = default)
        {
            var args = new Dictionary <string, object>();

            if (acceptDownloads.HasValue)
            {
                args.Add("acceptDownloads", acceptDownloads.Value);
            }

            if (bypassCSP.HasValue)
            {
                args.Add("bypassCSP", bypassCSP.Value);
            }

            if (colorScheme != ColorScheme.Undefined)
            {
                args.Add("colorScheme", colorScheme);
            }

            if (deviceScaleFactor.HasValue)
            {
                args.Add("deviceScaleFactor", deviceScaleFactor.Value);
            }

            if (extraHTTPHeaders != null)
            {
                args["extraHTTPHeaders"] = extraHTTPHeaders.Select(kv => new HeaderEntry {
                    Name = kv.Key, Value = kv.Value
                }).ToArray();
            }

            if (geolocation != null)
            {
                args.Add("geolocation", geolocation);
            }

            if (hasTouch.HasValue)
            {
                args.Add("hasTouch", hasTouch.Value);
            }

            if (httpCredentials != null)
            {
                args.Add("httpCredentials", httpCredentials);
            }

            if (ignoreHTTPSErrors.HasValue)
            {
                args.Add("ignoreHTTPSErrors", ignoreHTTPSErrors.Value);
            }

            if (isMobile.HasValue)
            {
                args.Add("isMobile", isMobile.Value);
            }

            if (javaScriptEnabled.HasValue)
            {
                args.Add("javaScriptEnabled", javaScriptEnabled.Value);
            }

            if (!string.IsNullOrEmpty(locale))
            {
                args.Add("locale", locale);
            }

            if (offline.HasValue)
            {
                args.Add("offline", offline.Value);
            }

            if (permissions != null)
            {
                args.Add("permissions", permissions);
            }

            if (proxy != null)
            {
                args.Add("proxy", proxy);
            }

            if (!string.IsNullOrEmpty(recordHarPath))
            {
                args.Add("recordHar", new
                {
                    Path        = recordHarPath,
                    OmitContent = recordHarOmitContent,
                });
            }

            if (!string.IsNullOrEmpty(recordVideoDir) &&
                recordVideoDir != null)
            {
                args.Add("recordVideo", new Dictionary <string, object>()
                {
                    { "dir", recordVideoDir },
                    { "size", recordVideoSize },
                });
            }

            if (!string.IsNullOrEmpty(storageState))
            {
                args.Add("storageState", storageState);
            }

            if (!string.IsNullOrEmpty(storageStatePath))
            {
                args.Add("storageStatePath", storageStatePath);
            }

            if (!string.IsNullOrEmpty(timezoneId))
            {
                args.Add("timezoneId", timezoneId);
            }

            if (!string.IsNullOrEmpty(userAgent))
            {
                args.Add("userAgent", userAgent);
            }

            if (ViewportSize.NoViewport.Equals(viewportSize))
            {
                args.Add("noDefaultViewport", true);
            }
            else if (viewportSize != null && !ViewportSize.Default.Equals(viewportSize))
            {
                args.Add("viewport", viewportSize);
            }

            args["sdkLanguage"] = "csharp";

            return(Connection.SendMessageToServerAsync <BrowserContextChannel>(
                       Guid,
                       "newContext",
                       args,
                       true));
        }
        internal Task <BrowserContextChannel> LaunchPersistentContextAsync(
            string userDataDir,
            bool?headless = default,
            Microsoft.Playwright.BrowserChannel channel = default,
            string executablePath     = default,
            IEnumerable <string> args = default,
            Proxy proxy          = default,
            string downloadsPath = default,
            bool?chromiumSandbox = default,
            bool?handleSIGINT    = default,
            bool?handleSIGTERM   = default,
            bool?handleSIGHUP    = default,
            float?timeout        = default,
            IEnumerable <KeyValuePair <string, string> > env = default,
            bool?devtools                    = default,
            float?slowMo                     = default,
            bool?acceptDownloads             = default,
            bool?ignoreHTTPSErrors           = default,
            bool?bypassCSP                   = default,
            ViewportSize viewportSize        = default,
            ScreenSize screenSize            = default,
            string userAgent                 = default,
            float?deviceScaleFactor          = default,
            bool?isMobile                    = default,
            bool?hasTouch                    = default,
            bool?javaScriptEnabled           = default,
            string timezoneId                = default,
            Geolocation geolocation          = default,
            string locale                    = default,
            IEnumerable <string> permissions = default,
            IEnumerable <KeyValuePair <string, string> > extraHTTPHeaders = default,
            bool?offline = default,
            HttpCredentials httpCredentials        = default,
            ColorScheme colorScheme                = default,
            string recordHarPath                   = default,
            bool?recordHarOmitContent              = default,
            string recordVideoDir                  = default,
            RecordVideoSize recordVideoSize        = default,
            IEnumerable <string> ignoreDefaultArgs = default,
            bool?ignoreAllDefaultArgs              = default)
        {
            var channelArgs = new Dictionary <string, object>();

            if (!string.IsNullOrEmpty(userDataDir))
            {
                channelArgs.Add("userDataDir", userDataDir);
            }

            if (headless.HasValue)
            {
                channelArgs.Add("headless", headless);
            }

            if (channel != Microsoft.Playwright.BrowserChannel.Undefined)
            {
                channelArgs.Add("channel", channel);
            }

            if (!string.IsNullOrEmpty(executablePath))
            {
                channelArgs.Add("executablePath", executablePath);
            }

            if (args?.Any() == true)
            {
                channelArgs.Add("args", args);
            }

            if (!string.IsNullOrEmpty(downloadsPath))
            {
                channelArgs.Add("downloadsPath", downloadsPath);
            }

            if (proxy != null)
            {
                channelArgs.Add("proxy", proxy);
            }

            if (chromiumSandbox.HasValue)
            {
                channelArgs.Add("chromiumSandbox", chromiumSandbox);
            }

            if (handleSIGINT.HasValue)
            {
                channelArgs.Add("handleSIGINT", handleSIGINT);
            }

            if (handleSIGTERM.HasValue)
            {
                channelArgs.Add("handleSIGTERM", handleSIGTERM);
            }

            if (handleSIGHUP.HasValue)
            {
                channelArgs.Add("handleSIGHUP", handleSIGHUP);
            }

            if (timeout.HasValue)
            {
                channelArgs.Add("timeout", timeout);
            }

            if (env?.Any() == true)
            {
                channelArgs.Add("env", env.Remap());
            }

            if (devtools.HasValue)
            {
                channelArgs.Add("devtools", devtools);
            }

            if (slowMo.HasValue)
            {
                channelArgs.Add("slowMo", slowMo);
            }

            if (acceptDownloads.HasValue)
            {
                channelArgs.Add("acceptDownloads", acceptDownloads);
            }

            if (ignoreHTTPSErrors.HasValue)
            {
                channelArgs.Add("ignoreHTTPSErrors", ignoreHTTPSErrors);
            }

            if (bypassCSP.HasValue)
            {
                channelArgs.Add("bypassCSP", bypassCSP);
            }

            if (ViewportSize.NoViewport.Equals(viewportSize))
            {
                channelArgs.Add("noDefaultViewport", true);
            }
            else if (viewportSize != null && !ViewportSize.Default.Equals(viewportSize))
            {
                channelArgs.Add("viewport", viewportSize);
            }

            if (screenSize != default)
            {
                channelArgs.Add("screensize", screenSize);
            }

            if (!string.IsNullOrEmpty(userAgent))
            {
                channelArgs.Add("userAgent", userAgent);
            }

            if (deviceScaleFactor.HasValue)
            {
                channelArgs.Add("deviceScaleFactor", deviceScaleFactor);
            }

            if (isMobile.HasValue)
            {
                channelArgs.Add("isMobile", isMobile);
            }

            if (hasTouch.HasValue)
            {
                channelArgs.Add("hasTouch", hasTouch);
            }

            if (javaScriptEnabled.HasValue)
            {
                channelArgs.Add("javaScriptEnabled", javaScriptEnabled);
            }

            if (!string.IsNullOrEmpty(timezoneId))
            {
                channelArgs.Add("timezoneId", timezoneId);
            }

            if (geolocation != default)
            {
                channelArgs.Add("geolocation", geolocation);
            }

            if (!string.IsNullOrEmpty(locale))
            {
                channelArgs.Add("locale", locale);
            }

            if (permissions != null && permissions.Any())
            {
                channelArgs.Add("permissions", permissions);
            }

            if (extraHTTPHeaders != null && extraHTTPHeaders.Any())
            {
                channelArgs.Add("extraHTTPHeaders", extraHTTPHeaders.Remap());
            }

            if (offline.HasValue)
            {
                channelArgs.Add("offline", offline);
            }

            if (httpCredentials != default)
            {
                channelArgs.Add("httpCredentials", httpCredentials);
            }

            if (colorScheme != ColorScheme.Undefined)
            {
                channelArgs.Add("colorScheme", colorScheme);
            }

            if (!string.IsNullOrEmpty(recordHarPath))
            {
                channelArgs.Add("recordHar", new
                {
                    Path        = recordHarPath,
                    OmitContent = recordHarOmitContent.GetValueOrDefault(false),
                });
            }

            if (!string.IsNullOrEmpty(recordVideoDir) &&
                recordVideoDir != null)
            {
                channelArgs.Add("recordVideo", new Dictionary <string, object>()
                {
                    { "dir", recordVideoDir },
                    { "size", recordVideoSize },
                });
            }

            if (ignoreDefaultArgs != null && ignoreDefaultArgs.Any())
            {
                channelArgs.Add("ignoreDefaultArgs", ignoreDefaultArgs);
            }

            if (ignoreAllDefaultArgs.HasValue)
            {
                channelArgs.Add("ignoreAllDefaultArgs", ignoreAllDefaultArgs);
            }

            channelArgs.Add("sdkLanguage", "csharp");

            return(Connection.SendMessageToServerAsync <BrowserContextChannel>(Guid, "launchPersistentContext", channelArgs, false));
        }