public IWebElement FindElement(WebElementInfo elementInfo, ILogger log) { log = log?.CreateChildLogger($"Find {elementInfo}"); log?.INFO($"Description: {elementInfo.Description}"); var search = elementInfo.GetWebSearch(); var searchStack = new Stack <WebSearchInfo>(); searchStack.Push(search); while (search.ParentSearch != null) { searchStack.Push(search.ParentSearch); search = search.ParentSearch; } var searchLog = log?.CreateChildLogger("Search element"); searchLog?.INFO($"Search contains: {searchStack.Count} items"); IWebElement foundElement = null; var counter = 0; while (searchStack.Count > 0) { var currentSearch = searchStack.Pop(); searchLog?.INFO($"Search for {++counter} item"); searchLog?.INFO($"Locator type: {currentSearch.LocatorType}"); searchLog?.INFO($"Locator value: {currentSearch.LocatorValue}"); var by = GetBy(currentSearch); try { if (foundElement == null) { foundElement = FindElement(by); } else { foundElement = FindElementRelativeTo(foundElement, by); } } catch (Exception ex) { var err = new SaKiWebElementException(nameof(FindElement), "WebElement could't be found", elementInfo, ex); log?.ERROR(err.Message, err); throw err; } } log?.INFO("Element found successfully"); return(foundElement); }
public IWebElement WaitForElementState(WebElementInfo elementInfo, WebElementState elementState, ILogger log, int timeoutInSec = 0, int pollingInMiliSec = 0) { if (timeoutInSec == 0) { timeoutInSec = DefaultTimeoutForSearchInSec; } if (pollingInMiliSec == 0) { pollingInMiliSec = DefaultTimeoutForPollingInSec; } log = log?.CreateChildLogger($"Wait for state: {elementState} for {elementInfo}"); log?.INFO($"With timeout in {timeoutInSec} seconds and with polling interval in {pollingInMiliSec} seconds"); var flags = Enum.GetValues(typeof(WebElementState)).Cast <WebElementState>(); var expectedFlags = flags.Where(f => elementState.HasFlag(f)).ToList(); try { GetDriver().Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(DefaultTimeoutForTrySearchInMilisec); var sw = Stopwatch.StartNew(); long mseconds = 0; while (sw.Elapsed.TotalSeconds <= timeoutInSec) { mseconds = sw.ElapsedMilliseconds; var state = GetElementState(elementInfo, out var element, null, false); if (expectedFlags.All(f => state.HasFlag(f))) { return(element); } var toSleep = pollingInMiliSec * 1000 - (sw.ElapsedMilliseconds - mseconds); if (toSleep > 0) { Thread.Sleep((int)toSleep); } } var err = SaKiWebElementException.TimeoutDuring($"waiting for element state: {elementState}", timeoutInSec, elementInfo); log?.ERROR(err.Message, err); throw err; } finally { GetDriver().Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(DefaultTimeoutForSearchInSec); } }
public void Type(string text, WebElementInfo elementInfo, ILogger log) { try { log = log?.CreateChildLogger($"Type text '{text}' to {elementInfo}"); LogElementPath(elementInfo, log); var element = WaitForElementState(elementInfo, WebElementState.ReadyForAction, log); element.SendKeys(text); } catch (Exception ex) { if (IsScreenshotOnErrorEnabled) { TryLogScreenShot($"Failed to Type text '{text}' to {elementInfo}", log); } throw SaKiWebElementException.ErrorDuring("Type to element", elementInfo, ex); } }
public void Click(WebElementInfo elementInfo, ILogger log) { try { log = log?.CreateChildLogger($"Click on {elementInfo}"); LogElementPath(elementInfo, log); var element = WaitForElementState(elementInfo, WebElementState.ReadyForAction, log); element.Click(); } catch (Exception ex) { if (IsScreenshotOnErrorEnabled) { TryLogScreenShot($"Failed to Click on {elementInfo}", log); } throw SaKiWebElementException.ErrorDuring("Click on element", elementInfo, ex); } }