I'm using DefaultWait while waiting for an WebElement to be clickable. Although TargetInvocationException is one of the exceptions in my list of exceptions to be ingnored during waiting, I still have tests failing with this exception before TimeOut period is reached. This is not what I expected.
public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver)
{
Timeout = TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
PollingInterval = TimeSpan.FromMilliseconds(500)
};
fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
webelement.Click();
}
> Selenium WebDriver supports various programming languages like Java, Python, C#, Ruby, Perl, PHP, JavaScript, R, Objective-C and Haskell.
C# is being widely used as a language for Selenium Test Automation because you can easily write your test scripts with the help of Visual Studio, which offers access to features like, IntelliSense, debugging, basic refactors, etc.
C# is useful for automation testing because it allows the automation test engineer to develop an application with the help of Visual Studio on the . Net framework. C# is another programming language that also supports the binding with Selenium. And this language binding will be updated along with the java program.
When you start with Selenium I would say JAVA would be a better option for many reasons: There are a lot of opportunities for Selenium with Java is higher in the market as there are very fewer opportunities in C# with Selenium.
Try using WebDriverWait
instead of DefaultWait<IWebDriver>
, which is basically the same thing.
public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
WebDriverWait fluentWait = new WebDriverWait(driver,Configuration.WaitTime.TotalSeconds);
//No need to set PollingInterval, default is already 500ms
fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
webelement.Click();
}
I see no need to use Interface when there is a predefined concrete class for exactly the same reason (waiting with the webDriver). Report back if the problem still persists.
Update: if it does not solve your problem, use lamba expression to deligate the function needed for Until() (public TResult Until<TResult>(Func<T, TResult> condition);
)
fluentWait.Until(driver =>
{
try
{
driver.FindElement(/*By Locator*/).Click();
}
catch (Exception ex)
{
Type exType = ex.GetType();
if (exType == typeof(TargetInvocationException) ||
exType == typeof(NoSuchElementException) ||
exType == typeof(InvalidOperationException))
{
return false; //By returning false, wait will still rerun the func.
}
else
{
throw; //Rethrow exception if it's not ignore type.
}
}
return true;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With