Tired of setting up all the waits in your selenium project? This will make things easier for you.
Original ExpectedConditions split into ExpectedConditionsSearchContext and ExpectedConditionsWebDriver
Based on DotNetSeleniumExtras
- ExpectedConditionsSearchContext contains the conditions that can be applied to both WebElement and WebDriver context
- ExpectedConditionsWebDriver contains the conditions that can only be applied to the WebDriver context
Normally you have to do the following:
var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(30));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("testId")));Now you can do:
var element = _webDriver.WaitUntil(ExpectedConditionsSearchContext.ElementIsVisible(By.Id("testId")));Notice that the ExpectedConditions changed to ExpectedConditionsSearchContext more on that later...
Selenium provides the abillity to wait in the context of the WebDriver by WebDriverWait. There is no such thing to wait in de WebElement context. Maybe you want to have your focus on a specific part of the page in your PageObject. You can do this by using the DefaultWait:
var parentElement = _webDriver.FindElement(By.Id("testId"));
var wait = new DefaultWait<IWebElement>(parentElement, new SystemClock());
wait.Timeout = TimeSpan.FromSeconds(30);
var element = wait.Until(ExpectedConditions.ElementExists(By.Id("waitForThisElement")));Now you can do:
var parentElement = _webDriver.FindElement(By.Id("testId"));
var wait = new WebElementWait(parentElement, TimeSpan.FromSeconds(30));
var element = wait.Until(ExpectedConditionsSearchContext.ElementExists(By.Id("waitForThisElement")));And with the extra extension on WebElement you can even do:
var parentElement = _webDriver.FindElement(By.Id("testId"));
var element = parentElement.WaitUntil(ExpectedConditionsSearchContext.ElementExists(By.Id("waitForThisElement")));Extension overload on FindElement and FindElements. You can direct pick your choice on what the state of the element you are looking for needs to be:
_webDriver.FindElement(By.Id("testId"), WaitForElement.Visible);
_webDriver.FindElement(By.Id("testId"), WaitForElement.Exists);
_webDriver.FindElement(By.Id("testId"), WaitForElement.Clickable);
_webDriver.FindElements(By.Id("testId"), WaitForElements.Visible);
_webDriver.FindElements(By.Id("testId"), WaitForElements.Exists);But can be altered as well as the pollingtime and exceptions to ignore:
