9

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();

    }

2 Answers 2

3

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;
        });
Sign up to request clarification or add additional context in comments.

2 Comments

As you probably already expected, using the concrete class WebDriverWait instead of the Interface DefaultWait makes no difference. Some of my tests still failing because of an InvalidOperationException after this change. On the other hand, your solution using lambda expression, given in your update, is working fine (after some minor modifications to make it syntactically correct within the context of my original method).
@Frank Glad I could help!
0

Thanks to Thodoris Koskinopoulos, this solution is working fine for me:

    public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {
        var fluentWait = new WebDriverWait(driver, Configuration.WaitTime);
        fluentWait.Until(webDriver =>
        {
            try
            {
                webelement.Click();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException ||
                    ex is NoSuchElementException ||
                    ex is InvalidOperationException)
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }
            return true;
        });
    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.