Ejemplo n.º 1
0
        /// <summary>
        /// Creates an identity event from the request object.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequestBase" /> object.</param>
        /// <param name="requestId">The request ID.</param>
        public static EventItem CreateEvent(this HttpRequestBase request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            BrowserBrand browserBrand   = BrowserBrand.Unknown;
            short        browserVersion = 0;
            MobileDevice mobileDevice   = MobileDevice.Unknown;

            if (request.Browser != null)
            {
                s_browsers.TryGetValue(request.Browser.Browser, out browserBrand);
                browserVersion = (short)request.Browser.MajorVersion;
                s_devices.TryGetValue(request.Browser.MobileDeviceModel, out mobileDevice);
            }

            return(new EventItem
            {
                AnonymId = GetAnonymId(request),
                BrowserBrand = browserBrand,
                BrowserVersion = browserVersion,
                MobileDevice = mobileDevice,
                ClientId = request.UserHostAddress,
                ReferrerUrl = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : null
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Instantiates selected browser.
        /// </summary>
        /// <param name="browserBrand">Browser brand to instantiate.</param>
        /// <param name="startPageUrl">Start page URL.</param
        /// <param name="implicitWait">Selenium driver implicit wait duration, in seconds.</param>
        /// <param name="killOthers">Optional. If true then other browser instances are closed.</param>
        /// <param name="driverDirectory">Optional driver directory. If not spcefied then environment PATH is used.</param>
        /// <param name="options">Specific driver options. Must match the browser brand options type.</param>
        /// <returns>Driver in context</returns>
        public static Context CreateContext(BrowserBrand browserBrand, Uri startPageUrl, int implicitWait = 10, bool killOthers = false, string driverDirectory = null, DriverOptions options = null)
        {
            if (startPageUrl == null)
            {
                throw new ArgumentException($"{nameof(CreateContext)}: NULL argument: {nameof(startPageUrl)}");
            }

            RemoteWebDriver driver = null;

            try
            {
                switch (browserBrand)
                {
                case BrowserBrand.Chrome:
                {
                    if (killOthers)
                    {
                        KillBrowserProcesses("chrome");
                    }

                    if (options == null)
                    {
                        options = new ChromeOptions
                        {
                            PageLoadStrategy           = PageLoadStrategy.Normal,
                            AcceptInsecureCertificates = true
                        };
                    }

                    driver = driverDirectory != null ? new ChromeDriver(driverDirectory, (ChromeOptions)options) : new ChromeDriver((ChromeOptions)options);
                }
                break;

                case BrowserBrand.Edge:
                {
                    if (killOthers)
                    {
                        KillBrowserProcesses("MicrosoftEdge", "MicrosoftWebDriver");
                    }

                    if (options == null)
                    {
                        options = new EdgeOptions
                        {
                            PageLoadStrategy = PageLoadStrategy.Normal
                        };
                    }
                    driver = driverDirectory != null ? new EdgeDriver(driverDirectory, (EdgeOptions)options) : new EdgeDriver((EdgeOptions)options);
                }
                break;


                case BrowserBrand.FireFox:
                {
                    if (killOthers)
                    {
                        KillBrowserProcesses("firefox");
                    }

                    if (options == null)
                    {
                        options = new FirefoxOptions
                        {
                            PageLoadStrategy           = PageLoadStrategy.Normal,
                            AcceptInsecureCertificates = true,
                            UseLegacyImplementation    = false
                        };
                    }
                    driver = driverDirectory != null ? new FirefoxDriver(driverDirectory, (FirefoxOptions)options) : new FirefoxDriver((FirefoxOptions)options);
                }
                break;


                case BrowserBrand.IE:
                {
                    if (killOthers)
                    {
                        KillBrowserProcesses("iexplore");
                    }

                    if (options == null)
                    {
                        options = new InternetExplorerOptions
                        {
                            PageLoadStrategy        = PageLoadStrategy.Normal,
                            EnableNativeEvents      = true,
                            UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
                            EnablePersistentHover   = true,
                            IgnoreZoomLevel         = false,
                            IntroduceInstabilityByIgnoringProtectedModeSettings = true,
                            RequireWindowFocus = false,
                        };
                    }
                    driver = driverDirectory != null ? new InternetExplorerDriver(driverDirectory, (InternetExplorerOptions)options) : new InternetExplorerDriver((InternetExplorerOptions)options);
                }
                break;
                }

                driver.Url = startPageUrl.ToString();

                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(implicitWait);

                return(new Context(driver, null, null));
            }
            catch (Exception x)
            {
                return(new Context(null, null, null, x));
            }
        }