/// <summary> /// RecoverFromBrowsers handles browser level recovery. /// </summary> public static void RecoverFromBrowsers(DataSet recoverDataset = null, DataSet brRecoverData = null, DataSet orDataSet = null, TestObject tObject = null) { //This indicates if an actual recovery was performed if (recoverDataset != null && brRecoverData != null && orDataSet != null && tObject != null) { _recoverData = recoverDataset; _datasetRecoverBrowser = brRecoverData; _datasetOr = orDataSet; _objTestObject = tObject; _noOfTimes = new int[brRecoverData.Tables[0].Rows.Count]; } try { for (int i = 0; i < _datasetRecoverBrowser.Tables[0].Rows.Count; i++) { Property.IsRecoveryRunning = true; if (_noOfTimes[i].Equals(Property.RecoveryCount)) { break; } string keyword = _datasetRecoverBrowser.Tables[0].Rows[i]["Recovery_Keyword"].ToString().Trim(); string action = _datasetRecoverBrowser.Tables[0].Rows[i]["Action"].ToString().Trim(); string recoverDetails = _datasetRecoverBrowser.Tables[0].Rows[i]["Recovery_Details"].ToString().Trim(); string data = string.Empty; try { data = _datasetRecoverBrowser.Tables[0].Rows[i]["Data"].ToString().Trim(); } catch (Exception) { // ignored } switch (keyword.ToLower()) { case "object_presence": string[] recoverContents = recoverDetails.Split('|'); string parent = recoverContents[0].Trim(); string testObject = recoverContents[1].Trim(); _objDataRow = Utility.GetTestOrData(parent, testObject, _datasetOr); if (_objDataRow.Count.Equals(0)) { continue; } try { TestObject.Attribute = _objDataRow[KryptonConstants.WHAT]; TestObject.AttributeType = _objDataRow[KryptonConstants.HOW]; //Will Add more actions later, for now click is in demand. switch (action.ToLower()) { case "click": _objTestObject.Click(); Console.WriteLine("Recovery Active: Click on " + parent + " | " + testObject); break; case "fireevent": _objTestObject.FireEvent(data); Console.WriteLine("Recovery Active: FireEvent: " + data + " on " + parent + " | " + testObject); break; default: break; } //KRYPTON0419. Browser.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3.0)); _noOfTimes[i]++; } catch (Exception) { _noOfTimes[i] = 0; continue; } break; case "browser_title": break; case "browser_url": break; } } //Resetting attribute and attributeType. TestObject.Attribute = _cachedAttribute; TestObject.AttributeType = _cachedAttributeType; Property.IsRecoveryRunning = false; } catch (Exception e) { Property.IsRecoveryRunning = false; } }
/// <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>> /// <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(Common.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) { Common.Utility.SetVariable("FirefoxProfilePath", Common.Utility.GetParameter("FirefoxProfilePath")); firefoxProfilePath = Common.Utility.GetParameter("FirefoxProfilePath"); } if (Common.Property.parameterdic.ContainsKey(KryptonConstants.CHROME_PROFILE_PATH)) { if (!Path.IsPathRooted(Common.Property.parameterdic[KryptonConstants.CHROME_PROFILE_PATH])) { Common.Property.parameterdic[KryptonConstants.CHROME_PROFILE_PATH] = string.Concat(Common.Property.IniPath, Common.Property.parameterdic[KryptonConstants.CHROME_PROFILE_PATH]); Common.Utility.SetVariable(KryptonConstants.CHROME_PROFILE_PATH, Common.Property.parameterdic[KryptonConstants.CHROME_PROFILE_PATH] = Path.GetFullPath(Common.Property.parameterdic[KryptonConstants.CHROME_PROFILE_PATH])); } } //Determine which Driver will be initiated. string prevBrowser = driverName; switch (browserName.ToLower()) { case KryptonConstants.BROWSER_FIREFOX: Browser.firefoxProfilePath = ProfilePath; driverName = KryptonConstants.FIREFOX_DRIVER; break; case KryptonConstants.BROWSER_IE: driverName = KryptonConstants.INTERNET_EXPLORER_DRIVER; break; case KryptonConstants.BROWSER_CHROME: Browser.chromeProfilePath = Common.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. Browser.isRemoteExecution = isRemoteExecution; //Get Remote machine ip or name for remote execution. Browser.remoteUrl = remoteUrl; // Variable to determine whether to use firefox addons or not Browser.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 { } if (browser == null || ExistingBrowserCount == 0) { browser = new Browser(errorCaptureAs, browserName, deleteCookie); browser.setBrowserFocus(); browser.NavigationUrl(url); if (deleteCookie && !browserName.ToLower().Equals(KryptonConstants.BROWSER_FIREFOX)) { try { browser.DeleteAllCookies(); } catch { } 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(Common.exceptions.ERROR_NORESPONSEURL)) { throw new WebDriverException(Common.Utility.GetCommonMsgVariable("KRYPTONERRCODE0007") + ":" + e.Message); } throw new Exception(Common.Utility.GetCommonMsgVariable("KRYPTONERRCODE0054") + ":" + e.Message); } catch (Exception e) { throw e; } }