예제 #1
0
파일: Browser.cs 프로젝트: Thinksys/krypton
        /// <summary>
        /// Method to open new browser and initiate Driver with specified browser name.
        /// </summary>
        /// <param name="browserName">string : Name of the browser Ex."firefox"</param>
        /// <param name="deleteCookie">bool : This parameter indicate whether cookies should delete or not</param>
        /// <param name="url">string : Url of webpage to be open in new browser</param>
        /// <param name="isRemoteExecution">string : determine for remote execetion. value must be "true" or "false"</param>
        /// <param name="remoteUrl">string : Url of remote machine</param>
        /// <param name="browserDimension"> string: Dimension for Browser Window size.</param>
        /// <param name="profilePath">Path to the browser profile if available.</param>
        /// <param name="addonsPath"></param>
        /// >
        /// <returns></returns>
        public static Browser OpenBrowser(string browserName, bool deleteCookie, string url,
            string isRemoteExecution, string remoteUrl, string profilePath, string addonsPath,
            DataSet datasetRecoverPopup, DataSet datasetRecoverBrowser, DataSet datasetOR, string browserDimension)
        {
            try
            {
                try
                {
                    if (!string.IsNullOrEmpty(browserDimension))
                    {
                        browserDimension = browserDimension.Trim().Split('=')[1];
                        Width = int.Parse(browserDimension.Split(',')[0].Trim());
                        Height = int.Parse(browserDimension.Split(',')[1].Trim());
                        IsBrowserDimendion = true;
                    }
                    else
                        IsBrowserDimendion = false;
                }
                catch
                {
                    throw new KryptonException(Utility.GetCommonMsgVariable("KRYPTONERRCODE0054") + ":" + "Input string was not in a correct format.");
                }
                //If cookies need to be deleted, no use of using a profile created by webdriver in previous instances
                if (deleteCookie)
                {
                    Utility.SetVariable("FirefoxProfilePath", Utility.GetParameter("FirefoxProfilePath"));
                    FireFox.FirefoxProfilePath = Utility.GetParameter("FirefoxProfilePath");
                }
                if (Property.Parameterdic.ContainsKey(KryptonConstants.CHROME_PROFILE_PATH))
                {
                    if (!Path.IsPathRooted(Property.Parameterdic[KryptonConstants.CHROME_PROFILE_PATH]))
                    {
                        Property.Parameterdic[KryptonConstants.CHROME_PROFILE_PATH] = string.Concat(Property.IniPath, Property.Parameterdic[KryptonConstants.CHROME_PROFILE_PATH]);
                        Utility.SetVariable(KryptonConstants.CHROME_PROFILE_PATH, Property.Parameterdic[KryptonConstants.CHROME_PROFILE_PATH] = Path.GetFullPath(Property.Parameterdic[KryptonConstants.CHROME_PROFILE_PATH]));
                    }
                }

                //Determine which Driver will be initiated.
                string prevBrowser = _driverName;
                switch (browserName.ToLower())
                {
                    case KryptonConstants.BROWSER_FIREFOX:

                        global::Driver.Browsers.FireFox.FirefoxProfilePath = profilePath;
                        _driverName = KryptonConstants.FIREFOX_DRIVER;
                        break;
                    case KryptonConstants.BROWSER_IE:
                        _driverName = KryptonConstants.INTERNET_EXPLORER_DRIVER;
                        break;
                    case KryptonConstants.BROWSER_CHROME:
                        Chrome.ChromeProfilePath = Utility.GetVariable(KryptonConstants.CHROME_PROFILE_PATH);
                        _driverName = KryptonConstants.CHROME_DRIVER;
                        break;
                    case KryptonConstants.BROWSER_SAFARI:
                        _driverName = KryptonConstants.SAFARI_DRIVER;
                        break;
                }

                //Check url format.
                if (url.IndexOf(':') != 1 && !(url.Contains("http://") || url.Contains("https://"))) //Check for file protocol and http protocol :
                {
                    url = "http://" + url;
                }
                //If opening a file in Firefox.
                if (url.IndexOf(':').Equals(1) && url.IndexOf("xml", StringComparison.OrdinalIgnoreCase) >= 0 && !(url.Contains("http://") || url.Contains("https://")) && browserName.IndexOf("firefox", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    url = "View-source:" + url;
                }
                //Determine whether execution is remote or not.
                IsRemoteExecution = isRemoteExecution;
                //Get Remote machine ip or name for remote execution.
                _remoteUrl = remoteUrl;
                //Variable to determine whether to use firefox addons or not
                _addonsPath = addonsPath;
                //Condition to check Whether to initiate new driver instance or just open new browser.
                int existingBrowserCount = 0;
                try
                {
                    if (_driverName.ToLower().Equals(prevBrowser.ToLower()))
                    {
                        existingBrowserCount = Driver.WindowHandles.Count;
                        Driverlist.Add(Driver);
                    }
                }
                catch (Exception e)
                {
                    KryptonException.Writeexception(e);
                }

                if (_browser == null || existingBrowserCount == 0)
                {
                    _browser = new Browser(_errorCaptureAs, browserName, deleteCookie);

                    _browser.SetBrowserFocus();
                    _browser.NavigationUrl(url);
                    if (deleteCookie && !browserName.ToLower().Equals(KryptonConstants.BROWSER_FIREFOX))
                    {
                        _browser.DeleteAllCookies();
                        _browser.Refresh();
                        _browser.NavigationUrl(url);
                    }
                }
                else
                {
                    //If there are browsers already open, launch with cookies instead
                    TestObject testObject = new TestObject();
                    testObject.ExecuteStatement("window.open();"); // Opening a browser using JavaScript
                    _browser.SetBrowserFocus();
                    if (IsBrowserDimendion)
                        Driver.Manage().Window.Size = new System.Drawing.Size(Width, Height);

                    _browser.NavigationUrl(url);

                    if (deleteCookie)
                    {
                        _browser.DeleteAllCookies();
                        _browser.NavigationUrl(url);
                    }
                }
                return _browser;
            }
            catch (WebDriverException e)
            {
                if (e.Message.Contains(Exceptions.ERROR_NORESPONSEURL))
                {
                    throw new WebDriverException(Utility.GetCommonMsgVariable("KRYPTONERRCODE0007") + ":" + e.Message);

                }
                throw new Exception(Utility.GetCommonMsgVariable("KRYPTONERRCODE0054") + ":" + e.Message);
            }
            catch (Exception e)
            {
                throw e;
            }
        }