/// <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="webappDriver"></param> /// <param name="elementXPath"></param> /// <param name="attribute"></param> /// <returns></returns> public static dynamic SafeGetValue(RemoteWebDriver webappDriver, string elementXPath, string attribute) { IWebElement element; int retryCount = 1; while (retryCount < maxRetryCount) { try { element = webappDriver.FindElementByXPath(elementXPath); Highlight(webappDriver, element); var value = element.GetAttribute(attribute); return(value); } catch (Exception e) { Thread.Sleep(1000); retryCount++; continue; } } string errorMessage = string.Format("SendKeys action failed for element at XPath: {0}", elementXPath); RetryException error = new RetryException(errorMessage); throw error; }
/// <summary> /// Sends keys to an element in the web page. Uses retry logic for a specified amount of tries. One second buffer between tries. /// </summary> /// <param name="webappDriver"></param> /// <param name="elementXPath"></param> /// <param name="KeysToSend"></param> public static void SafeSendKeys(RemoteWebDriver webappDriver, string elementXPath, string KeysToSend) { IWebElement element; int retryCount = 1; while (retryCount < maxRetryCount) { try { element = webappDriver.FindElementByXPath(elementXPath); Highlight(webappDriver, element); element.SendKeys(KeysToSend); return; } catch (Exception e) { Thread.Sleep(1000); retryCount++; continue; } } string errorMessage = string.Format("SendKeys 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="webappDriver"></param> /// <param name="elementXPath"></param> public static void SafeClick(RemoteWebDriver webappDriver, string elementXPath) { IWebElement element; int retryCount = 1; while (retryCount < maxRetryCount) { try { element = webappDriver.FindElementByXPath(elementXPath); Highlight(webappDriver, element); element.Click(); break; } catch (Exception e) { Thread.Sleep(1000); retryCount++; continue; } } // If succeeded, write to performance log if (retryCount < maxRetryCount) { // 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; } }