Example #1
0
        internal Browser(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewport,
            LauncherBase launcher,
            Func <TargetInfo, bool> targetFilter)
        {
            Connection          = connection;
            IgnoreHTTPSErrors   = ignoreHTTPSErrors;
            DefaultViewport     = defaultViewport;
            TargetsMap          = new ConcurrentDictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();
            DefaultContext      = new BrowserContext(Connection, this, null);
            _contexts           = contextIds.ToDictionary(
                contextId => contextId,
                contextId => new BrowserContext(Connection, this, contextId));

            Connection.Disconnected    += Connection_Disconnected;
            Connection.MessageReceived += Connect_MessageReceived;

            Launcher = launcher;
            _logger  = Connection.LoggerFactory.CreateLogger <Browser>();
            _targetFilterCallback = targetFilter ?? ((TargetInfo _) => true);
        }
Example #2
0
        private async Task SetViewport(ViewPortOptions viewport)
        {
            var needsReload = await _emulationManager.EmulateViewport(_client, viewport);

            _viewport = viewport;
            if (needsReload)
            {
                await Reload();
            }
        }
Example #3
0
        internal static async Task <Browser> CreateAsync(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewPort,
            ChromiumProcess chromiumProcess)
        {
            var browser = new Browser(connection, contextIds, ignoreHTTPSErrors, defaultViewPort, chromiumProcess);
            await connection.SendAsync("Target.setDiscoverTargets", new TargetSetDiscoverTargetsRequest
            {
                Discover = true
            }).ConfigureAwait(false);

            return(browser);
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Browser"/> class.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="contextIds">The context ids></param>
        /// <param name="ignoreHTTPSErrors">The option to ignoreHTTPSErrors</param>
        /// <param name="defaultViewport">Default viewport</param>
        /// <param name="chromiumProcess">The Chromium process</param>
        public Browser(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewport,
            ChromiumProcess chromiumProcess)
        {
            SetConnectionAsync(connection, false).Wait();
            IgnoreHTTPSErrors   = ignoreHTTPSErrors;
            DefaultViewport     = defaultViewport;
            TargetsMap          = new ConcurrentDictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();
            DefaultContext      = new BrowserContext(Connection, this, null);
            _contexts           = contextIds.ToDictionary(keySelector: contextId => contextId,
                                                          elementSelector: contextId => new BrowserContext(Connection, this, contextId));

            _chromiumProcess = chromiumProcess;
        }
Example #5
0
        internal static async Task <Browser> CreateAsync(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewPort,
            LauncherBase launcher,
            Func <TargetInfo, bool> targetFilter,
            Action <Browser> initAction = null)
        {
            var browser = new Browser(connection, contextIds, ignoreHTTPSErrors, defaultViewPort, launcher, targetFilter);

            initAction?.Invoke(browser);

            await connection.SendAsync("Target.setDiscoverTargets", new TargetSetDiscoverTargetsRequest
            {
                Discover = true
            }).ConfigureAwait(false);

            return(browser);
        }
Example #6
0
        internal async Task <bool> EmulateViewport(ViewPortOptions viewport)
        {
            var mobile            = viewport.IsMobile;
            var width             = viewport.Width;
            var height            = viewport.Height;
            var deviceScaleFactor = viewport.DeviceScaleFactor;
            var screenOrientation = viewport.IsLandscape ?
                                    new ScreenOrientation
            {
                Angle = 90,
                Type  = ScreenOrientationType.LandscapePrimary
            } :
            new ScreenOrientation
            {
                Angle = 0,
                Type  = ScreenOrientationType.PortraitPrimary
            };
            var hasTouch = viewport.HasTouch;

            await Task.WhenAll(new Task[] {
                _client.SendAsync("Emulation.setDeviceMetricsOverride", new EmulationSetDeviceMetricsOverrideRequest
                {
                    Mobile            = mobile,
                    Width             = width,
                    Height            = height,
                    DeviceScaleFactor = deviceScaleFactor,
                    ScreenOrientation = screenOrientation
                }),
                _client.SendAsync("Emulation.setTouchEmulationEnabled", new EmulationSetTouchEmulationEnabledRequest
                {
                    Enabled       = hasTouch,
                    Configuration = viewport.IsMobile ? "mobile" : "desktop"
                })
            }).ConfigureAwait(false);

            var reloadNeeded = _emulatingMobile != mobile || _hasTouch != hasTouch;

            _emulatingMobile = mobile;
            _hasTouch        = hasTouch;
            return(reloadNeeded);
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Browser"/> class.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="contextIds">The context ids></param>
        /// <param name="ignoreHTTPSErrors">The option to ignoreHTTPSErrors</param>
        /// <param name="defaultViewport">Default viewport</param>
        /// <param name="launcher">The launcher</param>
        public Browser(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewport,
            LauncherBase launcher)
        {
            Connection          = connection;
            IgnoreHTTPSErrors   = ignoreHTTPSErrors;
            DefaultViewport     = defaultViewport;
            TargetsMap          = new ConcurrentDictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();
            DefaultContext      = new BrowserContext(Connection, this, null);
            _contexts           = contextIds.ToDictionary(
                contextId => contextId,
                contextId => new BrowserContext(Connection, this, contextId));

            Connection.Disconnected    += Connection_Disconnected;
            Connection.MessageReceived += Connect_MessageReceived;

            Launcher = launcher;
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Browser"/> class.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="contextIds">The context ids></param>
        /// <param name="ignoreHTTPSErrors">The option to ignoreHTTPSErrors</param>
        /// <param name="defaultViewport">Default viewport</param>
        /// <param name="chromiumProcess">The Chromium process</param>
        public Browser(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewport,
            ChromiumProcess chromiumProcess)
        {
            Connection          = connection;
            IgnoreHTTPSErrors   = ignoreHTTPSErrors;
            DefaultViewport     = defaultViewport;
            TargetsMap          = new Dictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();
            DefaultContext      = new BrowserContext(Connection, this, null);
            _contexts           = contextIds.ToDictionary(keySelector: contextId => contextId,
                                                          elementSelector: contextId => new BrowserContext(Connection, this, contextId));

            Connection.Closed          += (object sender, EventArgs e) => Disconnected?.Invoke(this, new EventArgs());
            Connection.MessageReceived += Connect_MessageReceived;

            _chromiumProcess = chromiumProcess;
            _logger          = Connection.LoggerFactory.CreateLogger <Browser>();
        }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Browser"/> class.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="contextIds">The context ids></param>
        /// <param name="ignoreHTTPSErrors">The option to ignoreHTTPSErrors</param>
        /// <param name="defaultViewport">Default viewport</param>
        /// <param name="chromiumProcess">The Chromium process</param>
        public Browser(
            Connection connection,
            string[] contextIds,
            bool ignoreHTTPSErrors,
            ViewPortOptions defaultViewport,
            ChromiumProcess chromiumProcess)
        {
            Connection          = connection;
            IgnoreHTTPSErrors   = ignoreHTTPSErrors;
            DefaultViewport     = defaultViewport;
            TargetsMap          = new ConcurrentDictionary <string, Target>();
            ScreenshotTaskQueue = new TaskQueue();
            DefaultContext      = new BrowserContext(Connection, this, null);
            _contexts           = contextIds.ToDictionary(
                contextId => contextId,
                contextId => new BrowserContext(Connection, this, contextId));

            Connection.Disconnected    += Connection_Disconnected;
            Connection.MessageReceived += Connect_MessageReceived;

            ChromiumProcess = chromiumProcess;
            _logger         = Connection.LoggerFactory.CreateLogger <Browser>();
        }
        internal async Task <bool> EmulateViewport(ViewPortOptions viewport)
        {
            var mobile            = viewport.IsMobile;
            var width             = viewport.Width;
            var height            = viewport.Height;
            var deviceScaleFactor = viewport.DeviceScaleFactor;
            ScreenOrientation screenOrientation = viewport.IsLandscape ?
                                                  new ScreenOrientation
            {
                Angle = 90,
                Type  = ScreenOrientationType.LandscapePrimary
            } :
            new ScreenOrientation
            {
                Angle = 0,
                Type  = ScreenOrientationType.PortraitPrimary
            };

            await Task.WhenAll(new Task[] {
                _client.SendAsync("Emulation.setDeviceMetricsOverride", new
                {
                    mobile,
                    width,
                    height,
                    deviceScaleFactor,
                    screenOrientation
                }),
                _client.SendAsync("Emulation.setTouchEmulationEnabled", new
                {
                    enabled       = viewport.HasTouch,
                    configuration = viewport.IsMobile ? "mobile" : "desktop"
                })
            });

            var reloadNeeded = false;

            if (viewport.HasTouch && string.IsNullOrEmpty(_injectedTouchScriptId))
            {
                //TODO: It's not clear what to do here

                /*
                 * var source = $"({ injectedTouchEventsFunction})()";
                 * this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
                 * reloadNeeded = true;
                 */
            }
            else if (!viewport.HasTouch && !string.IsNullOrEmpty(_injectedTouchScriptId))
            {
                await _client.SendAsync("Page.removeScriptToEvaluateOnNewDocument", new
                {
                    identifier = _injectedTouchScriptId
                });

                _injectedTouchScriptId = null;
                reloadNeeded           = true;
            }

            if (_emulatingMobile != mobile)
            {
                reloadNeeded = true;
            }
            _emulatingMobile = mobile;
            return(reloadNeeded);
        }