/// <summary> /// Clears an element in the web page. Uses retry logic. /// </summary> /// <param name="driver"></param> public static void SafeClear(RemoteWebDriver driver, string elementXPath, int safeWait = 1000, int maxRetry = 30) { IWebElement element; int retryCount = 1; while (retryCount < maxRetry) { try { element = driver.FindElementByXPath(elementXPath); Highlight(driver, element, safeWait / 2); element.Clear(); return; } catch { Thread.Sleep(safeWait); retryCount++; continue; } } string errorMessage = string.Format("SendKeys action failed for element at XPath: {0}", elementXPath); RetryException error = new RetryException(errorMessage); throw error; }
/// <summary> /// Gets value from an element in the web page. Uses retry logic for a specified amount of tries. One second buffer between tries. /// </summary> /// <param name="driver"></param> /// <param name="elementXPath"></param> /// <param name="attribute"></param> /// <returns></returns> public static dynamic SafeGetValue(RemoteWebDriver driver, string elementXPath, string attribute, int safeWait = 1000, int maxRetry = 30) { IWebElement element; int retryCount = 1; while (retryCount < maxRetry) { try { element = driver.FindElementByXPath(elementXPath); var value = element.GetAttribute(attribute); Highlight(driver, element, safeWait / 2); return(value); } catch { Thread.Sleep(safeWait); retryCount++; continue; } } string errorMessage = string.Format("GetValue action failed for element at XPath: {0}", elementXPath); RetryException error = new RetryException(errorMessage); throw error; }
/// <summary> /// Clicks an element in the web page. Uses retry logic for a specified amount of tries. One second buffer between tries. /// </summary> /// <param name="driver"></param> /// <param name="elementXPath"></param> public static void SafeClick(RemoteWebDriver driver, string elementXPath, int safeWait = 1000, int maxRetry = 30) { IWebElement element; int retryCount = 1; while (retryCount < maxRetry) { try { element = driver.FindElementByXPath(elementXPath); Highlight(driver, element, safeWait / 2); element.Click(); break; } catch (Exception e) { Thread.Sleep(safeWait); retryCount++; continue; } } // If succeeded, write to performance log if (retryCount < maxRetry) { // Get caller test function name StackTrace stackTrace = new StackTrace(); string testName = stackTrace.GetFrame(1).GetMethod().Name; WriteToPerformanceLog(testName, "SafeClick", retryCount); return; } // Otherwise throw execption else { string errorMessage = string.Format("Click action failed for element at XPath: {0}", elementXPath); RetryException error = new RetryException(errorMessage); throw error; } }